diff options
author | jelemux <jeremias.weber@protonmail.com> | 2021-02-04 13:30:15 +0100 |
---|---|---|
committer | jelemux <jeremias.weber@protonmail.com> | 2021-02-04 13:30:15 +0100 |
commit | 69ceb1f9ec68e41029759a6fcff3168adb6df373 (patch) | |
tree | 194f21d845da5617e1c6d7fadaab380d10e472f7 /src | |
parent | 51bf8e89ce07864b70d7138bbc3958faf499cc67 (diff) | |
download | wasm-card-69ceb1f9ec68e41029759a6fcff3168adb6df373.tar.gz wasm-card-69ceb1f9ec68e41029759a6fcff3168adb6df373.tar.bz2 |
minor refactoring + comments
Diffstat (limited to 'src')
-rw-r--r-- | src/view/address.rs | 19 | ||||
-rw-r--r-- | src/view/mod.rs | 6 | ||||
-rw-r--r-- | src/view/name.rs | 43 | ||||
-rw-r--r-- | src/view/telephone.rs | 18 | ||||
-rw-r--r-- | src/viewmodel/mod.rs | 15 | ||||
-rw-r--r-- | src/viewmodel/name.rs | 17 | ||||
-rw-r--r-- | src/viewmodel/vcard.rs | 9 |
7 files changed, 94 insertions, 33 deletions
diff --git a/src/view/address.rs b/src/view/address.rs index a16d93a..01c949b 100644 --- a/src/view/address.rs +++ b/src/view/address.rs @@ -1,15 +1,28 @@ use yew::services::ConsoleService; +use yew::prelude::*; +use yewtil::NeqAssign; use yewtil::ptr::Irc; use yewtil::ptr::Mrc; use super::WeakComponentLink; -use yew::prelude::*; -use yewtil::NeqAssign; use crate::viewmodel::address::*; use crate::viewmodel::VCardPropertyInputObject; use super::VCardPropertyInputComponent; use crate::viewmodel::Error; - +/// View Component for a `address` field +/// +/// # Examples +/// +/// ```compile_fail +/// let html = html!{ +/// <AddressView weak_link=some_weak_component_link +/// generated=self.link.callback( +/// |n: Irc<Address>| +/// Msg::GeneratedAddress(some_address) +/// ) +/// /> +/// }; +/// ``` pub struct AddressView { props: Props, value: Mrc<Address>, diff --git a/src/view/mod.rs b/src/view/mod.rs index 7a64fed..d97c6e4 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -9,10 +9,15 @@ pub mod name; pub mod address; pub mod telephone; +/// Trait for types that represent an input component for a vcard property. pub trait VCardPropertyInputComponent<T: VCardPropertyInputObject<Self>>: Component { + /// Returns the object containing the input data. fn get_input_object(&self) -> T; + /// Getter function for the title of the component fn get_title(&self) -> String; + /// Getter function for an eventual error. fn get_error(&self) -> Option<Error>; + /// Returns the error as `Html` fn render_error(&self) -> Html { html!{ <> @@ -32,6 +37,7 @@ pub trait VCardPropertyInputComponent<T: VCardPropertyInputObject<Self>>: Compon } } +/// Weak link; Useful for being able to have a list of subcomponents. pub struct WeakComponentLink<COMP: Component>(Rc<RefCell<Option<ComponentLink<COMP>>>>); impl<COMP: Component> Clone for WeakComponentLink<COMP> { diff --git a/src/view/name.rs b/src/view/name.rs index 5b64bae..d864e35 100644 --- a/src/view/name.rs +++ b/src/view/name.rs @@ -1,14 +1,28 @@ use yew::services::ConsoleService; +use yew::prelude::*; +use yewtil::NeqAssign; use yewtil::ptr::Irc; use yewtil::ptr::Mrc; use crate::viewmodel::Error; use crate::view::WeakComponentLink; -use yew::prelude::*; -use yewtil::NeqAssign; use crate::viewmodel::name::*; use crate::viewmodel::VCardPropertyInputObject; use super::VCardPropertyInputComponent; +/// View Component for a `name` field +/// +/// # Examples +/// +/// ```compile_fail +/// let html = html!{ +/// <NameView weak_link=some_weak_component_link +/// generated=self.link.callback( +/// |n: Irc<Name>| +/// Msg::GeneratedName(some_name) +/// ) +/// /> +/// }; +/// ``` pub struct NameView { props: Props, value: Mrc<Name>, @@ -62,26 +76,11 @@ impl Component for NameView { } fn update(&mut self, msg: <Self as yew::Component>::Message) -> bool { match msg { - Msg::UpdatePrefix(p) => match self.value.get_mut() { - Some(value) => value.prefix = p, - None => ConsoleService::info("Couldn't get mutable reference to name"), - }, - Msg::UpdateFirstName(f) => match self.value.get_mut() { - Some(value) => value.first_name = f, - None => ConsoleService::info("Couldn't get mutable reference to name"), - }, - Msg::UpdateMiddleName(m) => match self.value.get_mut() { - Some(value) => value.middle_name = m, - None => ConsoleService::info("Couldn't get mutable reference to name"), - }, - Msg::UpdateLastName(l) => match self.value.get_mut() { - Some(value) => value.last_name = l, - None => ConsoleService::info("Couldn't get mutable reference to name"), - }, - Msg::UpdateSuffix(s) => match self.value.get_mut() { - Some(value) => value.suffix = s, - None => ConsoleService::info("Couldn't get mutable reference to name"), - }, + Msg::UpdatePrefix(p) => self.value.prefix = p, + Msg::UpdateFirstName(f) => self.value.first_name = f, + Msg::UpdateMiddleName(m) => self.value.middle_name = m, + Msg::UpdateLastName(l) => self.value.last_name = l, + Msg::UpdateSuffix(s) => self.value.suffix = s, Msg::Generate => { self.props.generated.emit(self.value.irc()); }, diff --git a/src/view/telephone.rs b/src/view/telephone.rs index ca58538..63fdee7 100644 --- a/src/view/telephone.rs +++ b/src/view/telephone.rs @@ -1,14 +1,28 @@ +use yew::prelude::*; use yew::services::ConsoleService; use yewtil::ptr::Irc; use yewtil::ptr::Mrc; +use yewtil::NeqAssign; use crate::view::WeakComponentLink; use crate::viewmodel::Error; -use yew::prelude::*; -use yewtil::NeqAssign; use crate::viewmodel::telephone::*; use crate::viewmodel::VCardPropertyInputObject; use super::VCardPropertyInputComponent; +/// View Component for a `telephone` field +/// +/// # Examples +/// +/// ```compile_fail +/// let html = html!{ +/// <TelephoneView weak_link=some_weak_component_link +/// generated=self.link.callback( +/// |n: Irc<Telephone>| +/// Msg::GeneratedTelephone(some_telephone) +/// ) +/// /> +/// }; +/// ``` pub struct TelephoneView { props: Props, value: Mrc<Telephone>, diff --git a/src/viewmodel/mod.rs b/src/viewmodel/mod.rs index de433de..d5d0de3 100644 --- a/src/viewmodel/mod.rs +++ b/src/viewmodel/mod.rs @@ -7,11 +7,16 @@ pub mod name; pub mod telephone; pub mod utility; + +/// Trait for types that represent the data of a vcard property used inside of a `VCardPropertyInputComponent`. pub trait VCardPropertyInputObject<C: VCardPropertyInputComponent<Self>> - where Self: Sized + where Self: Sized { + /// Function for creating a new (and empty) `VCardPropertyInputObject`. fn new() -> Self; + /// Converts each field of the `VCardPropertyInputObject` to a VCardPropertyInputField and returns them as a vector. fn get_input_fields(&self, link: &ComponentLink<C>) -> Vec<VCardPropertyInputField>; + /// Returns a `Html` representation of the `VCardPropertyInputObject`. fn render(&self, link: &ComponentLink<C>) -> Html { html!{ <div class="columns is-mobile is-multiline"> @@ -23,14 +28,19 @@ pub trait VCardPropertyInputObject<C: VCardPropertyInputComponent<Self>> </div> } } + /// Convenience function for checking if the `VCardPropertyInputObject` is empty. fn is_empty(&self) -> bool; } +/// Type for saving error messages. +/// +/// More of a placeholder for something better later on. #[derive(Debug,Clone,PartialEq)] pub struct Error { pub msg: String, } +/// Type that represents the visiual appearance of an input field. pub enum VCardPropertyInputField { Text { label: String, @@ -48,6 +58,7 @@ pub enum VCardPropertyInputField { } impl VCardPropertyInputField { + /// Returns a `Html` representation of the `VCardPropertyInputField`. pub fn render(&self) -> Html { match self { Self::Text { @@ -65,6 +76,7 @@ impl VCardPropertyInputField { } => Self::checkbox_field_input(label, id, value, onclick), } } + /// Returns an `Html` representation of a text input field with the given parameters. fn text_field_input(label: &str, id: &Option<String>, placeholder: &Option<String>, oninput: &Callback<InputData>) -> Html { html!{ <div class="field column @@ -83,6 +95,7 @@ impl VCardPropertyInputField { </div> } } + /// Returns an `Html` representation of a checkbox input field with the given parameters. fn checkbox_field_input(label: &str, id: &Option<String>, checked: &bool, onclick: &Callback<MouseEvent>) -> Html { html!{ <div class="field column diff --git a/src/viewmodel/name.rs b/src/viewmodel/name.rs index a88e07f..b6ded60 100644 --- a/src/viewmodel/name.rs +++ b/src/viewmodel/name.rs @@ -1,6 +1,21 @@ use super::*; use crate::view::name::*; +/// Type that represents a vcard `name` property +/// +/// # Examples +/// ``` +/// # use bcard_wasm_webapp::viewmodel::name::Name; +/// # use crate::bcard_wasm_webapp::viewmodel::VCardPropertyInputObject; +/// let mut name = Name::new(); +/// name.prefix = String::from("Sir"); +/// name.first_name = String::from("Arthur"); +/// name.middle_name = String::from("Charles"); +/// name.last_name = String::from("Clarke"); +/// name.suffix = String::from("CBE FRAS"); +/// +/// assert_eq!(name.generate_fn(), String::from("Sir Arthur Charles Clarke, CBE FRAS")); +/// ``` #[derive(Clone, Debug)] pub struct Name { pub prefix: String, @@ -86,7 +101,7 @@ impl Name { full_name.push_str(&self.last_name); } if !self.suffix.is_empty() { - full_name.push_str(" "); + full_name.push_str(", "); full_name.push_str(&self.suffix); } diff --git a/src/viewmodel/vcard.rs b/src/viewmodel/vcard.rs index 2d415b7..0225590 100644 --- a/src/viewmodel/vcard.rs +++ b/src/viewmodel/vcard.rs @@ -3,6 +3,7 @@ use crate::viewmodel::telephone::Telephone; use crate::viewmodel::address::Address; use crate::viewmodel::name::Name; +/// Type that represents the data structure of a vcard. #[derive(Clone, Debug)] pub struct VCardData { pub names: Vec<Irc<Name>>, @@ -11,7 +12,7 @@ pub struct VCardData { } macro_rules! make_vec_adder_fn { - ( $fnname:ident $property:ident $($arg_name:ident : $arg_type:ty),* ) => { + ( fn $fnname:ident $property:ident => $($arg_name:ident : $arg_type:ty),* ) => { pub fn $fnname(&mut self, $( $arg_name : $arg_type ),*) { $(self.$property.push($arg_name);)* } @@ -26,7 +27,7 @@ impl VCardData { telephones: Vec::new(), } } - make_vec_adder_fn!( add_name names name: Irc<Name> ); - make_vec_adder_fn!( add_address addresses address: Irc<Address> ); - make_vec_adder_fn!( add_telephone telephones telephone: Irc<Telephone> ); + make_vec_adder_fn!( fn add_name names => name: Irc<Name> ); + make_vec_adder_fn!( fn add_address addresses => address: Irc<Address> ); + make_vec_adder_fn!( fn add_telephone telephones => telephone: Irc<Telephone> ); }
\ No newline at end of file |