diff --git a/kloud/Cargo.toml b/kloud/Cargo.toml index 3670537..01add48 100644 --- a/kloud/Cargo.toml +++ b/kloud/Cargo.toml @@ -23,8 +23,10 @@ uuid = { version = "0.8", features = ["serde", "v4"] } app_tools = { version = "0.1.0", path = "../../utils/app_tools" } app_env = { version = "0.1.0", path = "../app_env" } +kloud_entries_macro_derive = { path = "../../macros/kloud_entries_macro_derive" } [dev-dependencies] envmnt = "0.9.0" pretty_assertions = "0.7.2" +lib_tests = { version = "0.1.0", path = "../../lib_tests" } # test-case = "1.1.0" diff --git a/kloud/src/kloud.rs b/kloud/src/kloud.rs new file mode 100644 index 0000000..f961dca --- /dev/null +++ b/kloud/src/kloud.rs @@ -0,0 +1,71 @@ +use async_graphql::{SimpleObject}; +use std::collections::BTreeMap; +use serde::{Serialize,Deserialize}; +use anyhow::{Context, Result }; + +use crate::datacontext::{DataContext, set_data_ctx}; +use crate::{set_data_uid}; + +trait KloudEntries { + fn load_content_data(content: &str, frmt: &str) -> Vec; + fn data_map_to_content(data_map: BTreeMap, frmt: &str) -> Result; +} +/// +/// +#[derive(Clone, Debug, Serialize, Deserialize, Default, SimpleObject, KloudEntries)] +pub struct Kloud { + #[serde(default = "set_data_uid")] + pub id: String, + pub name: String, + #[serde(default = "set_data_ctx")] + pub ctx: DataContext, +} + +impl Kloud { + pub fn entries(content: &str, frmt: &str) -> BTreeMap { + let mut data_map: BTreeMap = BTreeMap::new(); + for itm in Kloud::load_content_data(content,frmt) { + data_map.insert(itm.id.to_owned(), itm.to_owned()); + } + data_map + } + pub fn dump_entries(data: BTreeMap, frmt: &str) -> String { + Kloud::data_map_to_content(data,frmt).unwrap_or_else(|e| { println!("{}",e); String::from("")}) + } +} + +#[cfg(test)] +extern crate pretty_assertions; +#[cfg(test)] +mod tests { + use super::*; + use anyhow::{Result, anyhow}; + use lib_tests::{load_fs_data_content}; + + #[test] + /// Use environment vars: DATA_PATH DATA_TARGET DATA_FORMAT DATA_TOTAL DATA_OUT_FORMAT + /// data_test.sh kloud/ta/data ta_data.yaml yaml 106 toml + fn load_data() -> Result<()> { + let content=load_fs_data_content().unwrap_or_else(|e| {eprint!("{}",e); String::from("")}); + assert_ne!(content.is_empty(), true); + + if !envmnt::exists("DATA_TOTAL") { + return Err(anyhow!("DATA TOTAL not set in enviroment")); + } + let data_len: usize = envmnt::get_u8("DATA_TOTAL",1).into(); // .parse().expect("Wrong number format!"); + println!("Expected {} items", &data_len); + let data_frmt = &envmnt::get_or("DATA_FORMAT", "yaml"); + let data_map = Kloud::entries(&content,&data_frmt); + println!("Loaded {} items", &data_map.len()); + assert_eq!(data_map.len(), data_len); + + let out_format = &envmnt::get_or("DATA_OUT_FORMAT", ""); + if ! out_format.is_empty() { + println!("Out to {}: ", &out_format); + let str_data= Kloud::dump_entries(data_map, &out_format); + println!("{}",&str_data); + assert_ne!(str_data.is_empty(),true) + } + Ok(()) + } +} diff --git a/kloud/src/lib.rs b/kloud/src/lib.rs index a758c91..55dbbe8 100644 --- a/kloud/src/lib.rs +++ b/kloud/src/lib.rs @@ -8,6 +8,10 @@ pub mod datacontext; pub mod defs; pub mod utils; pub mod lang; +pub mod kloud; + +#[macro_use] +extern crate kloud_entries_macro_derive; use std::fmt; use serde::{Serialize,Deserialize};