diff --git a/frontend/src/data.rs b/frontend/src/data.rs new file mode 100644 index 0000000..3abd727 --- /dev/null +++ b/frontend/src/data.rs @@ -0,0 +1,140 @@ +use orbtk::prelude::*; + +use chrono::NaiveDateTime; +//use serde::de; +use serde::{Deserialize, Serialize}; +//use std::fmt; + +/* +struct NaiveDateTimeVisitor; + +impl<'de> de::Visitor<'de> for NaiveDateTimeVisitor { + type Value = NaiveDateTime; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "a string represents chrono::NaiveDateTime") + } + + fn visit_str(self, s: &str) -> Result + where + E: de::Error, + { + match NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S.%f") { + Ok(t) => Ok(t), + Err(_) => Err(de::Error::invalid_value(de::Unexpected::Str(s), &self)), + } + } +} + +fn from_timestamp<'de, D>(d: D) -> Result +where + D: de::Deserializer<'de>, +{ + d.deserialize_str(NaiveDateTimeVisitor) +} +*/ + +/// Structure used to verify a policy data element. +#[derive(Default, Debug, Clone, Deserialize, Serialize)] +pub struct PolicyCheck { + pub policy_code: String, + pub isvalid: bool +} + +#[derive(Default, Clone, Debug, Serialize, Deserialize)] +pub struct PolicyList { + pub policy_list: Vec, +} + +/// implements helper methods, that manage lists of policy data collections +impl PolicyList { + pub fn get(&self, index: usize) -> Option<&PolicyData> { + self.policy_list.get(index) + } + + pub fn get_mut(&mut self, index: usize) -> Option<&mut PolicyData> { + self.policy_list.get_mut(index) + } + + pub fn insert_front(&mut self, policy_list: PolicyData) { + self.policy_list.insert(0, policy_list); + } + + pub fn is_empty(&self) -> bool { + self.policy_list.is_empty() + } + + pub fn len(&self) -> usize { + self.policy_list.len() + } + + pub fn push(&mut self, policy_list: PolicyData) { + self.policy_list.push(policy_list); + } + pub fn remove(&mut self, index: usize) -> PolicyData { + self.policy_list.remove(index) + } +} + +/// Structure representing a policy data element. +#[derive(Default, Debug, Clone, Deserialize, Serialize)] +pub struct PolicyElement { + // is String16 a better default-type? + pub policy_code: String, + pub date_inserted: Option, + pub isvalid: bool +} + +/// Structure grouping valid policy data elemnts +#[derive(Default, Debug, Clone, Deserialize, Serialize)] +pub struct PolicyData { + pub date_valid_until: Option, + pub policy_elements: Vec, + pub name: String, + pub selected: bool +} + +/// implements helper methods, that manage the policy data collections. +impl PolicyData { + pub fn new(name: impl Into) -> Self { + // the new inserted element will be active by default + PolicyData { + name: name.into(), + date_valid_until: None, + selected: true, + ..Default::default() + } + } + + pub fn push(&mut self, policy_element: PolicyElement) { + self.list.push(policy_element); + } + + pub fn insert_front(&mut self, policy_element: PolicyElement) { + self.list.insert(0, policy_element); + } + + pub fn remove(&mut self, index: usize) -> PolicyElement { + self.list.remove(index) + } + + pub fn get(&self, index: usize) -> Option<&PolicyElement> { + self.list.get(index) + } + + pub fn get_mut(&mut self, index: usize) -> Option<&mut PolicyElement> { + self.list.get_mut(index) + } + + pub fn len(&self) -> usize { + self.list.len() + } + + pub fn is_empty(&self) -> bool { + self.list.is_empty() + } +} + +into_property_source!(PolicyCheck); +into_property_source!(PolicyData); +into_property_source!(PolicyList);