chore: move librecloud::kloud to here

This commit is contained in:
Jesús Pérez Lorenzo 2021-09-01 18:40:39 +01:00
parent 5648bf1685
commit 82061f4a8d
3 changed files with 77 additions and 0 deletions

View File

@ -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"

71
kloud/src/kloud.rs Normal file
View File

@ -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<Kloud>;
fn data_map_to_content(data_map: BTreeMap<String,Kloud>, frmt: &str) -> Result<String>;
}
///
///
#[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<String,Kloud> {
let mut data_map: BTreeMap<String,Kloud> = 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<String,Kloud>, 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(())
}
}

View File

@ -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};