use std::borrow::Borrow; use std::hash::{Hash, Hasher}; use serde::{Serialize, Deserialize}; use shm_rs::scheme_composer::composer::from_struct; use shm_rs::serializator::serializator; use shm_rs::static_scheme::init; use shm_rs::dynamic_scheme::environment; #[derive(Clone, Debug, Serialize, Deserialize)] pub enum Mode { Attempts, Instant, Scoring } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct LdFilterOverrides { pub search_win: Option, pub retry_cnt: Option, pub max_score: Option, pub ban_time: Option, pub ign_hosts: Option>, pub ign_users: Option> } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct LdFilterDefs { pub key: String, pub val: String } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct LdFilterSets { pub title: String, pub list: std::collections::HashSet } /// An enum for the auto type #[derive(Clone, Debug, Serialize, Deserialize)] pub enum ShmTypeAnyField { /// An integer data type Int(i64), /// An unsigned integer type UInt(u64), /// A boolean type Bool(bool), /// A string type String(String), /// An entity type Entity(String), /// A variable type Variable(String) } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct LdMonFilActsOver { pub key: String, pub val: ShmTypeAnyField } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct LdMonFilActs { pub action_name: String, pub override_map: Option> } #[derive(Clone, Debug, Serialize, Deserialize)] pub enum RuleGroupType { Any(Vec), Not(Vec), All(Vec), Regex{ regex: String, score: Option, descr: Option }, Literal{ literal: String, score: Option, descr: Option } } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct LdFilterRuleset { pub ruleset_name: String, pub sub_appnames: Option>, pub actions: Option>, pub rules: Vec } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct LdFilter { pub mode: Mode, pub overr_vars: Option, pub defmap: Option>, pub decsets: Option>, pub usesets: Option, pub appname: Option, pub rulesets: std::collections::HashSet } impl Eq for LdFilterSets {} impl PartialEq for LdFilterSets { fn eq(&self, other: &Self) -> bool { self.title == other.title } } impl Hash for LdFilterSets { fn hash(&self, state: &mut H) { self.title.hash(state); } } impl Borrow for LdFilterSets { fn borrow(&self) -> &String { &self.title } } impl Eq for LdFilterRuleset {} impl PartialEq for LdFilterRuleset { fn eq(&self, other: &Self) -> bool { self.ruleset_name == other.ruleset_name } } impl Hash for LdFilterRuleset { fn hash(&self, state: &mut H) { self.ruleset_name.hash(state); } } impl Borrow for LdFilterRuleset { fn borrow(&self) -> &String { &self.ruleset_name } } pub fn main() { // static let mut curdir = std::env::current_dir().unwrap(); curdir.push("examples/struct_to_scheme/advanced3"); println!("{}", curdir.display()); let schm = init::SchemeInit::new_with_path(curdir).unwrap(); let res = schm.run_from_file("filters.shm", None).unwrap(); let resser = res.get("filters").unwrap().clone(); // dynamic let mut curdir = std::env::current_dir().unwrap(); curdir.push("examples/struct_to_scheme/advanced3/nginx.shm"); let (_, dynres) = environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap(); // serialize dyn let ret = serializator::Serialization::serialize(resser.clone(), dynres.clone()).unwrap(); let serialized = serde_json::to_string(&ret).unwrap(); /* let mut rc = RustCode::new(&["Clone", "Debug", "Serialize", "Deserialize"], &["Clone", "Debug", "Serialize", "Deserialize"]); println!("Structures: "); match resser.generate_rust_structs(&mut rc) { Ok(_) => { println!("{}", rc); }, Err(e) => { println!("{}", e); } } */ let n: LdFilter = serde_json::from_str(&serialized).unwrap(); let resser = from_struct(n, resser); if resser.is_err() == true { panic!("{}", resser.err().unwrap()); } else { println!("{}", resser.unwrap()); } }