summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjelemux <jeremias.weber@protonmail.com>2020-10-19 22:46:45 +0200
committerjelemux <jeremias.weber@protonmail.com>2020-10-19 22:46:45 +0200
commit3a0a55362fd98c74bfb14df320bcacafd0c57835 (patch)
treead7f91af95df638bfbc58c48c97f34340ae37a8c /src
parent84c2dab4200c37c818d83c95f85445ee00d83bf6 (diff)
downloadwasm-card-3a0a55362fd98c74bfb14df320bcacafd0c57835.tar.gz
wasm-card-3a0a55362fd98c74bfb14df320bcacafd0c57835.tar.bz2
break things down into their most basic parts
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs2
-rw-r--r--src/model.rs76
-rw-r--r--src/pdfgen.rs5
-rw-r--r--src/validation.rs37
-rw-r--r--src/view.rs72
5 files changed, 17 insertions, 175 deletions
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<Name>,
- nickname: Option<String>,
- label: Option<TypedProperty<String>>,
- address: Option<TypedProperty<Address>>,
- emails: Option<Vec<TypedProperty<String>>>,
- title: Option<String>,
- role: Option<String>,
- organization: Option<String>,
- urls: Option<Vec<TypedProperty<String>>>,
- telephones: Option<Vec<TypedProperty<String>>>,
- revision: Option<NaiveDateTime>,
-}
-
-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<String>,
- first_name: Option<String>,
- middle_name: Option<String>,
- family_name: Option<String>,
- suffix: Option<String>,
-}
-
-impl Validation for Name {
- fn validate(&self) -> std::result::Result<(), ValidationError> { todo!() }
-}
-
-pub enum WorkHomeType {
- Home,
- Work,
-}
-
-pub struct TypedProperty<T> {
- p_type: Option<WorkHomeType>,
- value: T,
-}
-
-pub struct Address {
- street: Option<String>,
- city: Option<String>,
- locality: Option<String>,
- postal_code: Option<String>,
- country: Option<String>,
-} \ 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<u8> {
+pub fn genpdf() -> Vec<u8> {
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<u8> {
// TODO fill doc with real data
let mut buf: Vec<u8> = 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<String>,
-}
-
-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<Self>,
- 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 {
- Self { link }
+ fn create(_props: Self::Properties, _link: ComponentLink<Self>) -> 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! {
- <BCardForm/>
- }
- }
-}
-
-// example
-
-struct Model {
- link: ComponentLink<Self>,
- value: i64,
-}
-
-enum Msg {
- AddOne,
- Input(BCard),
-}
-
-impl Component for Model {
- type Message = Msg;
- type Properties = ();
-
- fn create(_: Self::Properties, link: ComponentLink<Self>) -> 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! {
- <div>
- <button onclick=self.link.callback(|_| Msg::AddOne)>{ "+1" }</button>
- <p>{ self.value }</p>
- </div>
+ 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!{
+ <a href=href download="demo.pdf" >
+ { "Download PDF" }
+ </a>
}
}
}
#[wasm_bindgen(start)]
pub fn run_app() {
- App::<Model>::new().mount_to_body();
+ App::<Form>::new().mount_to_body();
} \ No newline at end of file