generated from ElnuDev/rust-project
parent
81fed4c8ac
commit
1e646e0ac0
@ -1,53 +1,10 @@
|
||||
pub mod svg;
|
||||
use svg::*;
|
||||
|
||||
use askama::Template;
|
||||
use derive_more::From;
|
||||
use lazy_static::lazy_static;
|
||||
use std::{fs::OpenOptions, io::Write};
|
||||
mod render;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "document.xml")]
|
||||
struct DocumentTemplate<'a> {
|
||||
dimensions: &'a DocumentDimensions,
|
||||
keyboard: kle_serial::Keyboard,
|
||||
}
|
||||
|
||||
struct DocumentDimensions {
|
||||
width: SVGMeasure,
|
||||
height: SVGMeasure,
|
||||
margin: SVGMeasure,
|
||||
}
|
||||
|
||||
fn kle_font_units(kle_font_units: &usize) -> SVGMeasure {
|
||||
SVGMeasure::new((6 + kle_font_units * 2) as f64, SVGUnit::Pixel)
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref LETTER_LANDSCAPE: DocumentDimensions = DocumentDimensions {
|
||||
width: SVGMeasure::new(11.0, SVGUnit::Inch),
|
||||
height: SVGMeasure::new(8.5, SVGUnit::Inch),
|
||||
margin: SVGMeasure::new(0.25, SVGUnit::Inch),
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(From, Debug)]
|
||||
enum Error {
|
||||
Io(std::io::Error),
|
||||
Template(askama::Error),
|
||||
LayoutParse(serde_json::Error),
|
||||
}
|
||||
use render::{render_file, Result};
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.create(true)
|
||||
.open("output.svg")?;
|
||||
let document = DocumentTemplate {
|
||||
dimensions: &LETTER_LANDSCAPE,
|
||||
keyboard: serde_json::from_str(&std::fs::read_to_string("layout.json")?)?,
|
||||
};
|
||||
write!(file, "{}", document.render()?)?;
|
||||
fn main() -> Result<()> {
|
||||
render_file("layout.json", "output.svg")?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
use std::path::Path;
|
||||
|
||||
use askama::Template;
|
||||
use derive_more::From;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use crate::svg::{SVGMeasure, SVGUnit};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "document.xml")]
|
||||
struct DocumentTemplate<'a> {
|
||||
dimensions: &'a DocumentDimensions,
|
||||
keyboard: kle_serial::Keyboard,
|
||||
}
|
||||
|
||||
struct DocumentDimensions {
|
||||
width: SVGMeasure,
|
||||
height: SVGMeasure,
|
||||
margin: SVGMeasure,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref LETTER_LANDSCAPE: DocumentDimensions = DocumentDimensions {
|
||||
width: SVGMeasure::new(11.0, SVGUnit::Inch),
|
||||
height: SVGMeasure::new(8.5, SVGUnit::Inch),
|
||||
margin: SVGMeasure::new(0.25, SVGUnit::Inch),
|
||||
};
|
||||
}
|
||||
|
||||
mod filters {
|
||||
use crate::svg::{SVGMeasure, SVGUnit};
|
||||
|
||||
pub fn kle_font_units(kle_font_units: &usize) -> askama::Result<SVGMeasure> {
|
||||
Ok(SVGMeasure::new((6 + kle_font_units * 2) as f64, SVGUnit::Pixel))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(From, Debug)]
|
||||
pub enum Error {
|
||||
Io(std::io::Error),
|
||||
Template(askama::Error),
|
||||
LayoutParse(serde_json::Error),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub fn render_file<P, Q>(input_path: P, output_path: Q) -> Result<()>
|
||||
where P: AsRef<Path>, Q: AsRef<Path> {
|
||||
use std::{fs::{self, OpenOptions}, io::Write};
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.create(true)
|
||||
.open(output_path)?;
|
||||
write!(file, "{}", render(&fs::read_to_string(input_path)?)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn render(input: &str) -> Result<String> {
|
||||
let document = DocumentTemplate {
|
||||
dimensions: &LETTER_LANDSCAPE,
|
||||
keyboard: serde_json::from_str(input)?,
|
||||
};
|
||||
Ok(document.render()?)
|
||||
}
|
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 3.8 KiB |
Loading…
Reference in new issue