struct_to_scheme/advanced3/
struct2scheme.rs

1use std::borrow::Borrow;
2use std::hash::{Hash, Hasher};
3
4
5use serde::{Serialize, Deserialize};
6
7use shm_rs::scheme_composer::composer::from_struct;
8use shm_rs::serializator::serializator;
9
10use shm_rs::static_scheme::init;
11use shm_rs::dynamic_scheme::environment;
12
13
14#[derive(Clone, Debug, Serialize, Deserialize)]
15pub enum Mode 
16{ 
17    Attempts, 
18
19    Instant, 
20
21    Scoring
22}
23
24
25#[derive(Clone, Debug, Serialize, Deserialize)]
26pub struct LdFilterOverrides 
27{ 
28    pub search_win: Option<i64>,
29    pub retry_cnt: Option<u64>,
30    pub max_score: Option<u64>,
31    pub ban_time: Option<i64>,
32    pub ign_hosts: Option<Vec<String>>,
33    pub ign_users: Option<Vec<String>>
34}
35
36
37#[derive(Clone, Debug, Serialize, Deserialize)]
38pub struct LdFilterDefs 
39{ 
40    pub key: String,
41    pub val: String
42}
43
44
45#[derive(Clone, Debug, Serialize, Deserialize)]
46pub struct LdFilterSets 
47{ 
48    pub title: String,
49    pub list: std::collections::HashSet<String>
50}
51
52
53/// An enum for the auto type 
54#[derive(Clone, Debug, Serialize, Deserialize)]
55pub enum ShmTypeAnyField 
56{ 
57    /// An integer data type
58    Int(i64), 
59
60    /// An unsigned integer type
61    UInt(u64), 
62
63    /// A boolean type
64    Bool(bool), 
65
66    /// A string type
67    String(String), 
68
69    /// An entity type
70    Entity(String), 
71
72    /// A variable type
73    Variable(String)
74}
75
76
77#[derive(Clone, Debug, Serialize, Deserialize)]
78pub struct LdMonFilActsOver 
79{ 
80    pub key: String,
81    pub val: ShmTypeAnyField
82}
83
84
85#[derive(Clone, Debug, Serialize, Deserialize)]
86pub struct LdMonFilActs 
87{ 
88    pub action_name: String,
89    pub override_map: Option<Vec<LdMonFilActsOver>>
90}
91
92
93#[derive(Clone, Debug, Serialize, Deserialize)]
94pub enum RuleGroupType 
95{ 
96    Any(Vec<RuleGroupType>), 
97
98    Not(Vec<RuleGroupType>), 
99
100    All(Vec<RuleGroupType>), 
101
102    Regex{ regex: String, score: Option<u64>, descr: Option<String> }, 
103
104    Literal{ literal: String, score: Option<u64>, descr: Option<String> }
105}
106
107
108#[derive(Clone, Debug, Serialize, Deserialize)]
109pub struct LdFilterRuleset 
110{ 
111    pub ruleset_name: String,
112    pub sub_appnames: Option<std::collections::HashSet<String>>,
113    pub actions: Option<Vec<LdMonFilActs>>,
114    pub rules: Vec<RuleGroupType>
115}
116
117
118#[derive(Clone, Debug, Serialize, Deserialize)]
119pub struct LdFilter 
120{ 
121    pub mode: Mode,
122    pub overr_vars: Option<LdFilterOverrides>,
123    pub defmap: Option<Vec<LdFilterDefs>>,
124    pub decsets: Option<std::collections::HashSet<LdFilterSets>>,
125    pub usesets: Option<String>,
126    pub appname: Option<String>,
127    pub rulesets: std::collections::HashSet<LdFilterRuleset>
128}
129
130impl Eq for LdFilterSets {}
131
132impl PartialEq for LdFilterSets
133{
134    fn eq(&self, other: &Self) -> bool 
135    {
136        self.title == other.title
137    }
138}
139
140impl Hash for LdFilterSets 
141{
142    fn hash<H: Hasher>(&self, state: &mut H) 
143    {
144        self.title.hash(state);
145    }
146}
147
148impl Borrow<String> for LdFilterSets
149{
150    fn borrow(&self) -> &String 
151    {
152        &self.title
153    }
154}
155
156impl Eq for LdFilterRuleset {}
157
158impl PartialEq for LdFilterRuleset
159{
160    fn eq(&self, other: &Self) -> bool 
161    {
162        self.ruleset_name == other.ruleset_name
163    }
164}
165
166impl Hash for LdFilterRuleset 
167{
168    fn hash<H: Hasher>(&self, state: &mut H) 
169    {
170        self.ruleset_name.hash(state);
171    }
172}
173
174impl Borrow<String> for LdFilterRuleset
175{
176    fn borrow(&self) -> &String 
177    {
178        &self.ruleset_name
179    }
180}
181
182
183pub fn main()
184{
185
186// static
187    let mut curdir = std::env::current_dir().unwrap();
188    curdir.push("examples/struct_to_scheme/advanced3");
189    println!("{}", curdir.display());
190
191    let schm = init::SchemeInit::new_with_path(curdir).unwrap();
192    let res = schm.run_from_file("filters.shm", None).unwrap();
193    let resser = res.get("filters").unwrap().clone();
194
195// dynamic
196    let mut curdir = std::env::current_dir().unwrap();
197    curdir.push("examples/struct_to_scheme/advanced3/nginx.shm");
198    let (_, dynres) = 
199        environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap();
200
201// serialize dyn
202    let ret = serializator::Serialization::serialize(resser.clone(), dynres.clone()).unwrap();
203
204    let serialized = serde_json::to_string(&ret).unwrap();
205
206/*
207    let mut rc = RustCode::new(&["Clone", "Debug", "Serialize", "Deserialize"], &["Clone", "Debug", "Serialize", "Deserialize"]);
208    println!("Structures: ");
209    match resser.generate_rust_structs(&mut rc)
210    {
211        Ok(_) => 
212        {
213            println!("{}", rc);
214        },
215        Err(e) => 
216        {
217            println!("{}", e);
218        }
219    }
220*/
221
222    let n: LdFilter = serde_json::from_str(&serialized).unwrap();
223
224    let resser = from_struct(n, resser);
225    
226    if resser.is_err() == true
227    {
228        panic!("{}", resser.err().unwrap());
229    }
230    else
231    {
232        println!("{}", resser.unwrap());
233    }
234}