diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 221 |
1 files changed, 129 insertions, 92 deletions
@@ -1,7 +1,9 @@ #![recursion_limit="1024"] extern crate wee_alloc; extern crate console_error_panic_hook; +use std::collections::HashSet; use name::{NameView,Name}; +use address::{AddressView,Address,AddressType}; use genpdf::Element as _; use genpdf::{elements, style, fonts}; use qrcodegen::QrCode; @@ -12,6 +14,7 @@ use vcard::{VCard, VCardError}; use std::panic; mod name; +mod address; // Use `wee_alloc` as the global allocator. #[global_allocator] @@ -21,6 +24,7 @@ fn init() { panic::set_hook(Box::new(console_error_panic_hook::hook)); } +#[derive(Clone)] pub struct Download { pub file_name: String, pub content: String, @@ -36,16 +40,7 @@ impl Download { } } -impl Clone for Download { - fn clone(&self) -> Self { - Self { - file_name: self.file_name.clone(), - content: self.content.clone(), - mime_type: self.mime_type.clone(), - } - } -} - +#[derive(Clone, Copy)] pub enum MimeType { PDF, VCard, @@ -62,28 +57,28 @@ impl MimeType { } } -impl Clone for MimeType { - fn clone(&self) -> Self { - match self { - MimeType::PDF => MimeType::PDF, - MimeType::VCard => MimeType::VCard, - MimeType::SVG => MimeType::SVG, - } - } +#[derive(Clone, Copy)] +pub enum DownloadOption { + PDF, + VCard, + QrCode, } pub struct MainView { link: ComponentLink<Self>, error: Vec<String>, name: Name, + work_address: Address, + home_address: Address, download: Option<Download>, + selected_option: DownloadOption, } pub enum Msg { UpdateName(Name), - GenerateVCard, - GeneratePdf, - GenerateQrCode, + UpdateHomeAddress(Address), + UpdateWorkAddress(Address), + Generate(DownloadOption), Nope, } @@ -92,56 +87,85 @@ impl Component for MainView { type Properties = (); fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self { - MainView { link, error: vec![], name: Name::new(), download: None, } + MainView { + link, + error: vec![], + name: Name::new(), + work_address: Address::new_with_type(AddressType::Work), + home_address: Address::new_with_type(AddressType::Home), + download: None, + selected_option: DownloadOption::VCard + } } fn update(&mut self, msg: Self::Message) -> ShouldRender { self.error.clear(); match msg { - Msg::UpdateName(value) => self.name = value, - Msg::GenerateVCard => { - match self.generate_vcard() { - Ok(vcard) => self.download = Some( - Download { - file_name: format!("{}.vcs", self.name.formatted_name()), - content: vcard.to_string(), - mime_type: MimeType::VCard, - } - ), - Err(VCardError::FormatError(err)) => self.error.push(err.to_string()), - Err(VCardError::EmptyFormatName) => self.error.push(String::from("A VCard should have at least one formatted name.")), + Msg::UpdateName(value) => { + self.name = value; + self.link.send_message(Msg::Generate(self.selected_option)); + }, + Msg::UpdateHomeAddress(value) => { + self.home_address = value; + self.link.send_message(Msg::Generate(self.selected_option)); + }, + Msg::UpdateWorkAddress(value) => { + self.work_address = value; + self.link.send_message(Msg::Generate(self.selected_option)); + }, + Msg::Generate(option) => { + self.selected_option = option; + + let vcard_content = match self.generate_vcard() { + Ok(vcard) => Some(vcard.to_string()), + Err(VCardError::FormatError(err)) => { + self.error.push(err.to_string()); + None + } + Err(VCardError::EmptyFormatName) => { + self.error.push(String::from("At least one of the name fields should be filled out.")); + None + } }; - } - Msg::GeneratePdf => { - match self.generate_pdf() { - Ok(pdf) => self.download = Some( - Download { - file_name: format!("Visitenkarten {}.pdf", self.name.formatted_name()), - content: pdf, - mime_type: MimeType::PDF, + + match option { + DownloadOption::VCard => { + if vcard_content.is_some() { + self.download = Some( + Download { + file_name: format!("{}.vcs", self.name.formatted_name()), + content: vcard_content.unwrap().to_string(), + mime_type: MimeType::VCard, + } + ) } - ), - Err(_) => self.error.push(String::from("Unexpected error while generating the PDF. Please contact me about it.")), - } - } - Msg::GenerateQrCode => { - let mut vcard_content = None; - match self.generate_vcard() { - Ok(vcard) => vcard_content = Some(vcard.to_string()), - Err(VCardError::FormatError(err)) => self.error.push(err.to_string()), - Err(VCardError::EmptyFormatName) => self.error.push(String::from("A VCard should have at least one formatted name.")), - }; - if vcard_content.is_some() { - match QrCode::encode_text(vcard_content.as_ref().unwrap(), QrCodeEcc::Low) { - Ok(qr) => self.download = Some( - Download { - file_name: format!("QR-Code VCard {}.svg", self.name.formatted_name()), - content: qr.to_svg_string(4), - mime_type: MimeType::SVG, - } - ), - Err(_) => self.error.push(String::from("Sorry, VCard is too long!")), - }; + } + DownloadOption::QrCode => { + if vcard_content.is_some() { + match QrCode::encode_text(vcard_content.as_ref().unwrap(), QrCodeEcc::Low) { + Ok(qr) => self.download = Some( + Download { + file_name: format!("QR-Code VCard {}.svg", self.name.formatted_name()), + content: qr.to_svg_string(4), + mime_type: MimeType::SVG, + } + ), + Err(_) => self.error.push(String::from("Sorry, VCard is too long!")), + }; + } + } + DownloadOption::PDF => { + match self.generate_pdf() { + Ok(pdf) => self.download = Some( + Download { + file_name: format!("Visitenkarten {}.pdf", self.name.formatted_name()), + content: pdf, + mime_type: MimeType::PDF, + } + ), + Err(_) => self.error.push(String::from("Unexpected error while generating the PDF. Please contact me about it.")), + } + } } } Msg::Nope => return false, @@ -158,29 +182,12 @@ impl Component for MainView { fn view(&self) -> Html { - let download = self.download.clone(); - - let on_name_input = self.link.batch_callback(move |n: Name| - if download.is_some() { - vec![ - Msg::UpdateName(n), - match download.as_ref().unwrap().mime_type { - MimeType::PDF => Msg::GeneratePdf, - MimeType::SVG => Msg::GenerateQrCode, - MimeType::VCard => Msg::GenerateVCard, - } - ] - } else { - vec![Msg::UpdateName(n), Msg::GenerateVCard] - } - ); - let download_options = self.link.callback(|e: ChangeData| match e { ChangeData::Select(v) => match v.value().as_str() { - "vcard" => Msg::GenerateVCard, - "pdf" => Msg::GeneratePdf, - "qrcode" => Msg::GenerateQrCode, + "vcard" => Msg::Generate(DownloadOption::VCard), + "pdf" => Msg::Generate(DownloadOption::PDF), + "qrcode" => Msg::Generate(DownloadOption::QrCode), _ => Msg::Nope, }, _ => Msg::Nope, @@ -204,10 +211,14 @@ impl Component for MainView { { self.render_errors() } - <NameView oninput=on_name_input /> + <NameView oninput=self.link.callback(|n: Name| Msg::UpdateName(n)) /> - <div class="block"> - <div class="select"> + <AddressView address_type=AddressType::Home oninput=self.link.callback(|a: Address| Msg::UpdateHomeAddress(a)) /> + + <AddressView address_type=AddressType::Work oninput=self.link.callback(|a: Address| Msg::UpdateWorkAddress(a)) /> + + <div class="block level-left"> + <div class="select level-item"> <select id="download_options" onchange=download_options> <option value="vcard">{ "VCard (.vcf)" }</option> <option value="pdf">{ "Print-ready PDF" }</option> @@ -260,7 +271,7 @@ impl MainView { let download = self.download.as_ref().unwrap(); html!{ - <a href=download.as_data_link() download=download.file_name class="button is-primary" > + <a href=download.as_data_link() download=download.file_name class="button is-primary level-item" > { "Download" } </a> } @@ -276,9 +287,13 @@ impl MainView { MimeType::PDF => html!{ <iframe src=download.as_data_link() alt="PDF Preview" width="400" height="550"/> }, - MimeType::VCard => html!{ - <code> { download.content.clone() } </code> - }, + MimeType::VCard => { + html!{ + <pre> + <code> { download.content.clone() } </code> + </pre> + } + } MimeType::SVG => html!{ <img src=download.as_data_link() alt="Image Preview" class="image is-square" width="300" height="300"/> }, @@ -289,7 +304,29 @@ impl MainView { } fn generate_vcard(&self) -> Result<VCard, VCardError> { match VCard::from_formatted_name_str(&self.name.formatted_name()) { - Ok(vcard) => Ok(vcard), + Ok(vcard) => { + let mut vcard = vcard; + + let names = { + let mut names = HashSet::new(); + names.insert(self.name.to_vcard_name()); + + names + }; + + let addresses = { + let mut addresses = HashSet::new(); + addresses.insert(self.home_address.to_vcard_address()); + addresses.insert(self.work_address.to_vcard_address()); + + addresses + }; + + vcard.names = Some(vcard::Set::from_hash_set(names).unwrap()); + vcard.addresses = Some(vcard::Set::from_hash_set(addresses).unwrap()); + + Ok(vcard) + } Err(err) => Err(err), } } |