generated from ElnuDev/rust-project
parent
b7b441a933
commit
0ad5b4df3b
@ -1,2 +1,3 @@
|
||||
/target
|
||||
.direnv
|
||||
output.svg
|
@ -1,3 +1,33 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
pub mod svg;
|
||||
use svg::*;
|
||||
|
||||
use std::{fs::OpenOptions, io::Write};
|
||||
use askama::Template;
|
||||
use derive_more::From;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "document.xml")]
|
||||
struct DocumentTemplate {
|
||||
width: SVGMeasure,
|
||||
height: SVGMeasure,
|
||||
}
|
||||
|
||||
#[derive(From, Debug)]
|
||||
enum Error {
|
||||
Io(std::io::Error),
|
||||
Template(askama::Error),
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.create(true)
|
||||
.open("output.svg")?;
|
||||
let document = DocumentTemplate {
|
||||
width: SVGMeasure::new(8.5, SVGUnit::Inch),
|
||||
height: SVGMeasure::new(11.0,SVGUnit::Inch),
|
||||
};
|
||||
write!(file, "{}", document.render()?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -0,0 +1,52 @@
|
||||
use super::SVGUnit;
|
||||
use std::fmt;
|
||||
use std::{ops::{Add, Sub, Mul, Div}, fmt::Display};
|
||||
|
||||
pub struct SVGMeasure {
|
||||
pub measure: f64,
|
||||
pub unit: SVGUnit,
|
||||
}
|
||||
|
||||
impl SVGMeasure {
|
||||
pub fn new(measure: f64, unit: SVGUnit) -> Self {
|
||||
Self { measure, unit }
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for SVGMeasure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}{}", self.measure, self.unit)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for SVGMeasure {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: SVGMeasure) -> SVGMeasure {
|
||||
SVGMeasure::new(self.measure + other.measure, self.unit)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for SVGMeasure {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, other: SVGMeasure) -> SVGMeasure {
|
||||
SVGMeasure::new(self.measure - other.measure, self.unit)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f64> for SVGMeasure {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, scalar: f64) -> SVGMeasure {
|
||||
SVGMeasure::new(self.measure * scalar, self.unit)
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<f64> for SVGMeasure {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, scalar: f64) -> SVGMeasure {
|
||||
SVGMeasure::new(self.measure / scalar, self.unit)
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
mod unit;
|
||||
pub use unit::SVGUnit;
|
||||
|
||||
mod measure;
|
||||
pub use measure::SVGMeasure;
|
After Width: | Height: | Size: 164 B |
Loading…
Reference in new issue