From 84c2dab4200c37c818d83c95f85445ee00d83bf6 Mon Sep 17 00:00:00 2001 From: jelemux Date: Sun, 18 Oct 2020 22:40:57 +0200 Subject: initial commit --- .gitignore | 2 + Cargo.toml | 16 ++++++++ fonts/fira-sans.bold-italic.ttf | Bin 0 -> 447536 bytes fonts/fira-sans.bold.ttf | Bin 0 -> 438028 bytes fonts/fira-sans.italic.ttf | Bin 0 -> 411640 bytes fonts/fira-sans.regular.ttf | Bin 0 -> 403924 bytes src/lib.rs | 4 ++ src/model.rs | 76 ++++++++++++++++++++++++++++++++++++ src/pdfgen.rs | 48 +++++++++++++++++++++++ src/validation.rs | 37 ++++++++++++++++++ src/view.rs | 83 ++++++++++++++++++++++++++++++++++++++++ static/index.html | 14 +++++++ 12 files changed, 280 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 fonts/fira-sans.bold-italic.ttf create mode 100644 fonts/fira-sans.bold.ttf create mode 100644 fonts/fira-sans.italic.ttf create mode 100644 fonts/fira-sans.regular.ttf create mode 100644 src/lib.rs create mode 100644 src/model.rs create mode 100644 src/pdfgen.rs create mode 100644 src/validation.rs create mode 100644 src/view.rs create mode 100644 static/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2eaf0bf --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "bcard-wasm-webapp" +version = "0.1.0" +authors = ["jelemux "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +yew = "0.17.3" # react-like frontend framework +wasm-bindgen = "0.2.68" # enables interaction with js +chrono = "0.4.19" # date & time +genpdf = { path = "../genpdf-rs" } # pdf generation \ No newline at end of file diff --git a/fonts/fira-sans.bold-italic.ttf b/fonts/fira-sans.bold-italic.ttf new file mode 100644 index 0000000..97b52ef Binary files /dev/null and b/fonts/fira-sans.bold-italic.ttf differ diff --git a/fonts/fira-sans.bold.ttf b/fonts/fira-sans.bold.ttf new file mode 100644 index 0000000..95e1660 Binary files /dev/null and b/fonts/fira-sans.bold.ttf differ diff --git a/fonts/fira-sans.italic.ttf b/fonts/fira-sans.italic.ttf new file mode 100644 index 0000000..f5e4914 Binary files /dev/null and b/fonts/fira-sans.italic.ttf differ diff --git a/fonts/fira-sans.regular.ttf b/fonts/fira-sans.regular.ttf new file mode 100644 index 0000000..d9fdc0e Binary files /dev/null and b/fonts/fira-sans.regular.ttf differ diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0b497a1 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,4 @@ +mod view; +mod model; +mod validation; +mod pdfgen; \ No newline at end of file diff --git a/src/model.rs b/src/model.rs new file mode 100644 index 0000000..cac0820 --- /dev/null +++ b/src/model.rs @@ -0,0 +1,76 @@ +use chrono::NaiveDateTime; +use crate::validation::{self, *}; + +pub struct BCard { + name: Option, + nickname: Option, + label: Option>, + address: Option>, + emails: Option>>, + title: Option, + role: Option, + organization: Option, + urls: Option>>, + telephones: Option>>, + revision: Option, +} + +impl BCard { + fn new() -> Self { + Self { + name: None, + nickname: None, + label: None, + address: None, + emails: None, + title: None, + role: None, + organization: None, + urls: None, + telephones: None, + revision: None, + } + } +} + +impl Validation for BCard { + fn validate(&self) -> Result<(), ValidationError> { + let mut result = Ok(()); + result = match &self.name { + Some(n) => validation::add_results(result, n.validate()), + None => Err( ValidationError{ messages: vec![String::from("Name cannot be empty")] } ), + }; + // TODO add some more validation + result + } +} + +pub struct Name { + prefix: Option, + first_name: Option, + middle_name: Option, + family_name: Option, + suffix: Option, +} + +impl Validation for Name { + fn validate(&self) -> std::result::Result<(), ValidationError> { todo!() } +} + +pub enum WorkHomeType { + Home, + Work, +} + +pub struct TypedProperty { + p_type: Option, + value: T, +} + +pub struct Address { + street: Option, + city: Option, + locality: Option, + postal_code: Option, + country: Option, +} \ No newline at end of file diff --git a/src/pdfgen.rs b/src/pdfgen.rs new file mode 100644 index 0000000..b8c7f7d --- /dev/null +++ b/src/pdfgen.rs @@ -0,0 +1,48 @@ +use genpdf::Element as _; +use genpdf::{elements, style, fonts}; +use crate::model::BCard; + +pub fn genpdf(bcard: BCard) -> Vec { + let regular_bytes = include_bytes!("../fonts/fira-sans.regular.ttf"); + let regular_font_data = fonts::FontData::new(regular_bytes.to_vec(), None).expect("font data should be correct"); + + let bold_bytes = include_bytes!("../fonts/fira-sans.bold.ttf"); + let bold_font_data = fonts::FontData::new(bold_bytes.to_vec(), None).expect("font data should be correct"); + + let italic_bytes = include_bytes!("../fonts/fira-sans.italic.ttf"); + let italic_font_data = fonts::FontData::new(italic_bytes.to_vec(), None).expect("font data should be correct"); + + let bold_italic_bytes = include_bytes!("../fonts/fira-sans.bold-italic.ttf"); + let bold_italic_font_data = fonts::FontData::new(bold_italic_bytes.to_vec(), None).expect("font data should be correct"); + + let font_family = fonts::FontFamily{ regular: regular_font_data, bold: bold_font_data, italic: italic_font_data, bold_italic: bold_italic_font_data }; + + let mut doc = genpdf::Document::new(font_family); + + doc.set_title("BCard test"); + doc.set_minimal_conformance(); + doc.set_margins(10); + doc.set_line_spacing(1.25); + + #[cfg(feature = "hyphenation")] + { + use hyphenation::Load; + + doc.set_hyphenator( + hyphenation::Standard::from_embedded(hyphenation::Language::EnglishUS) + .expect("Failed to load hyphenation data"), + ); + } + + doc.push( + elements::Paragraph::new("genpdf Demo Document") + .aligned(elements::Alignment::Center) + .styled(style::Style::new().bold().with_font_size(20)), + ); + + // TODO fill doc with real data + + let mut buf: Vec = Vec::new(); + doc.render(&mut buf); + buf +} \ No newline at end of file diff --git a/src/validation.rs b/src/validation.rs new file mode 100644 index 0000000..715b472 --- /dev/null +++ b/src/validation.rs @@ -0,0 +1,37 @@ + + +pub trait Validation { + fn validate(&self) -> Result<(), ValidationError>; +} + +#[derive(Debug)] +pub struct ValidationError { + messages: Vec, +} + +impl std::fmt::Display for ValidationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for msg in &self.messages { + write!(f, "{}\n", msg)?; + } + Ok(()) + } +} + +impl std::error::Error for ValidationError { } + +pub fn add_results(first: Result<(), ValidationError>, second: Result<(), ValidationError>) -> Result<(), ValidationError> { + if first.is_ok() && second.is_ok() { + Ok(()) + } else if first.is_ok() && second.is_err() { + second + } else if first.is_err() && second.is_ok() { + first + } else { + let mut first = first.err().unwrap(); + let mut second = second.err().unwrap(); + first.messages.append(&mut second.messages); + + Err( ValidationError{ messages: first.messages } ) + } +} \ No newline at end of file diff --git a/src/view.rs b/src/view.rs new file mode 100644 index 0000000..81634ed --- /dev/null +++ b/src/view.rs @@ -0,0 +1,83 @@ +use crate::model::BCard; +use wasm_bindgen::prelude::*; +use yew::prelude::*; + +struct Form { + link: ComponentLink, + bcard: BCard, +} + +impl Component for Form { // probably not necessary but who knows + type Message = (); + type Properties = (); + + fn create(_: Self::Properties, link: ComponentLink) -> Self { + Self { link } + } + + fn update(&mut self, _: Self::Message) -> ShouldRender { + false + } + + fn change(&mut self, _: Self::Properties) -> ShouldRender { + false + } + + fn view(&self) -> Html { + html! { + + } + } +} + +// example + +struct Model { + link: ComponentLink, + value: i64, +} + +enum Msg { + AddOne, + Input(BCard), +} + +impl Component for Model { + type Message = Msg; + type Properties = (); + + fn create(_: Self::Properties, link: ComponentLink) -> Self { + Self { + link, + value: 0, + } + } + + fn update(&mut self, msg: Self::Message) -> ShouldRender { + match msg { + Msg::AddOne => self.value += 1 + } + true + } + + fn change(&mut self, _props: Self::Properties) -> ShouldRender { + // Should only return "true" if new properties are different to + // previously received properties. + // This component has no properties so we will always return "false". + false + } + + fn view(&self) -> Html { + html! { +
+ +

{ self.value }

+
+ } + } +} + +#[wasm_bindgen(start)] +pub fn run_app() { + App::::new().mount_to_body(); +} \ No newline at end of file diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..b0f1634 --- /dev/null +++ b/static/index.html @@ -0,0 +1,14 @@ + + + + + BCard Wasm Web App + + + + + + \ No newline at end of file -- cgit v1.2.3