From 3a0a55362fd98c74bfb14df320bcacafd0c57835 Mon Sep 17 00:00:00 2001 From: jelemux Date: Mon, 19 Oct 2020 22:46:45 +0200 Subject: break things down into their most basic parts --- src/lib.rs | 2 -- src/model.rs | 76 ------------------------------------------------------- src/pdfgen.rs | 5 ++-- src/validation.rs | 37 --------------------------- src/view.rs | 72 +++++++++++----------------------------------------- 5 files changed, 17 insertions(+), 175 deletions(-) delete mode 100644 src/model.rs delete mode 100644 src/validation.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 0b497a1..2e54410 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,2 @@ mod view; -mod model; -mod validation; mod pdfgen; \ No newline at end of file diff --git a/src/model.rs b/src/model.rs deleted file mode 100644 index cac0820..0000000 --- a/src/model.rs +++ /dev/null @@ -1,76 +0,0 @@ -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 index b8c7f7d..410bb99 100644 --- a/src/pdfgen.rs +++ b/src/pdfgen.rs @@ -1,8 +1,7 @@ use genpdf::Element as _; use genpdf::{elements, style, fonts}; -use crate::model::BCard; -pub fn genpdf(bcard: BCard) -> Vec { +pub fn genpdf() -> 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"); @@ -43,6 +42,6 @@ pub fn genpdf(bcard: BCard) -> Vec { // TODO fill doc with real data let mut buf: Vec = Vec::new(); - doc.render(&mut buf); + doc.render(&mut buf).expect("should render pdf"); buf } \ No newline at end of file diff --git a/src/validation.rs b/src/validation.rs deleted file mode 100644 index 715b472..0000000 --- a/src/validation.rs +++ /dev/null @@ -1,37 +0,0 @@ - - -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 index 81634ed..9b95691 100644 --- a/src/view.rs +++ b/src/view.rs @@ -1,83 +1,41 @@ -use crate::model::BCard; +use crate::pdfgen; use wasm_bindgen::prelude::*; +use js_sys; use yew::prelude::*; struct Form { - link: ComponentLink, - bcard: BCard, } -impl Component for Form { // probably not necessary but who knows +impl Component for Form { type Message = (); type Properties = (); - fn create(_: Self::Properties, link: ComponentLink) -> Self { - Self { link } + fn create(_props: Self::Properties, _link: ComponentLink) -> Self { + Self { } } - fn update(&mut self, _: Self::Message) -> ShouldRender { + fn update(&mut self, _msg: 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 }

-
+ let pdf_raw = /*include_bytes!("../demo.pdf"); // this works */pdfgen::genpdf(); // this doesn't work + let pdf = std::str::from_utf8(&pdf_raw).expect("should be able to convert to string"); + let uri_component: String = js_sys::encode_uri_component(pdf).into(); + let href = format!{"data:application/pdf;charset=utf-8,{}", uri_component }; + html!{ + + { "Download PDF" } + } } } #[wasm_bindgen(start)] pub fn run_app() { - App::::new().mount_to_body(); + App::
::new().mount_to_body(); } \ No newline at end of file -- cgit v1.2.3