frontend: data: definition of the used strutures within the frontend
* structure 'PolicyCheck': used in policycheck_view The widget offering users to test for validity of a given 'policy_code'. * structure 'PolicyList': used in policylist_view imported lists of policy data collections * structure 'PolicyData': used in policydata_view PolicyData is a structure of 'PolicyElemnts'. The latter will store the basic datatype elements (field: 'policy_code'), as a dynamic growable vector. Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
140
frontend/src/data.rs
Normal file
140
frontend/src/data.rs
Normal file
@@ -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<E>(self, s: &str) -> Result<Self::Value, E>
|
||||
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<NaiveDateTime, D::Error>
|
||||
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<PolicyData>,
|
||||
}
|
||||
|
||||
/// 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<NaiveDateTime>,
|
||||
pub isvalid: bool
|
||||
}
|
||||
|
||||
/// Structure grouping valid policy data elemnts
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct PolicyData {
|
||||
pub date_valid_until: Option<NaiveDateTime>,
|
||||
pub policy_elements: Vec<PolicyElement>,
|
||||
pub name: String,
|
||||
pub selected: bool
|
||||
}
|
||||
|
||||
/// implements helper methods, that manage the policy data collections.
|
||||
impl PolicyData {
|
||||
pub fn new(name: impl Into<String>) -> 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);
|
||||
Reference in New Issue
Block a user