struct_to_scheme/advanced4/
struct2scheme.rs

1
2
3
4
5use serde::{Serialize, Deserialize};
6use shm_rs::Lexer;
7
8use shm_rs::scheme_composer::composer::from_struct;
9use shm_rs::serializator::serializator;
10
11use shm_rs::static_scheme::init;
12use shm_rs::dynamic_scheme::environment;
13
14
15pub const STATUS: u64 = 5;
16
17pub const SHA1: &'static str = "SCRAM-SHA-1";
18
19pub const RELOAD: u64 = 2;
20
21pub const LOOKUP: u64 = 4;
22
23pub const SHA512: &'static str = "SCRAM-SHA-512";
24
25pub const FAST: u64 = 3;
26
27pub const LOAD: u64 = 3;
28
29pub const UNBAN: u64 = 1;
30
31pub const SHA256: &'static str = "SCRAM-SHA-256";
32
33pub const BEST: u64 = 7;
34
35pub const WITHOUT: u64 = 0;
36
37pub const BAN: u64 = 0;
38
39#[derive(Clone, Debug, Serialize, Deserialize)]
40pub struct LdGlobal 
41{ 
42    pub searchwin: i64,
43    pub retrycnt: u64,
44    pub maxscore: u64,
45    pub bantime: i64,
46    pub ignhosts: Vec<String>,
47    pub ignusers: Vec<String>
48}
49
50
51#[derive(Clone, Debug, Serialize, Deserialize)]
52pub struct LdJail 
53{ 
54    pub pwuser: Option<String>,
55    pub chroot: Option<bool>,
56    pub capsicum_flag: Option<bool>,
57    pub sigguard_max: Option<u32>
58}
59
60
61#[derive(Clone, Debug, Serialize, Deserialize)]
62pub struct LdActionThreadExec 
63{ 
64    pub thr_worker: u64,
65    pub thr_block: Option<u64>,
66    pub par_acts: Option<u64>
67}
68
69
70#[derive(Clone, Debug, Serialize, Deserialize)]
71pub enum SocketType 
72{ 
73    Unix{ path: String, chmod: u16 }, 
74
75    Tcp{ addr: String, port: u16 }
76}
77
78
79#[derive(Clone, Debug, Serialize, Deserialize)]
80pub struct LdCtrlPassword 
81{ 
82    pub shatype: String,
83    pub salted_password_b64: String,
84    pub salt_b64: String,
85    pub iterations: u32
86}
87
88
89#[derive(Clone, Debug, Serialize, Deserialize)]
90pub enum LdAclEntity 
91{ 
92    Cidr(String), 
93
94    Lfs(String)
95}
96
97
98#[derive(Clone, Debug, Serialize, Deserialize)]
99pub struct LdCtrlAcl 
100{ 
101    pub aclname: String,
102    pub caplist: Vec<u16>,
103    pub entity: LdAclEntity,
104    pub limit: u64
105}
106
107
108#[derive(Clone, Debug, Serialize, Deserialize)]
109pub struct LdControl 
110{ 
111    pub enabled: bool,
112    pub source: SocketType,
113    pub limitconn: u16,
114    pub readtimeout: u64,
115    pub servname: String,
116    pub password: Option<LdCtrlPassword>,
117    pub force_acl: bool,
118    pub enablelog: bool,
119    pub acls: Option<Vec<LdCtrlAcl>>
120}
121
122
123#[derive(Clone, Debug, Serialize, Deserialize)]
124pub enum DestinationType 
125{ 
126    Network{ addr: String, port: u16 }, 
127
128    Local{ path: String, chmod: u16 }
129}
130
131
132#[derive(Clone, Debug, Serialize, Deserialize)]
133pub struct MysqlSslPkcs 
134{ 
135    pub path_to_cert: String,
136    pub password: String
137}
138
139
140#[derive(Clone, Debug, Serialize, Deserialize)]
141pub struct MysqlSsl 
142{ 
143    pub pkcs12: Option<MysqlSslPkcs>,
144    pub path_to_ca: Option<String>,
145    pub accpet_inv_certs: bool,
146    pub skip_dom_validation: bool
147}
148
149
150#[derive(Clone, Debug, Serialize, Deserialize)]
151pub enum Database 
152{ 
153    None, 
154
155    File{ dir_path: String, rotate_size: Option<u64>, compress: Option<bool> }, 
156
157    Sqlite{ dir_path: String }, 
158
159    Mysql{ dest: DestinationType, dbname: String, username: String, password: Option<String>, compr: Option<u64>, ssl: Option<MysqlSsl> }
160}
161
162
163#[derive(Clone, Debug, Serialize, Deserialize)]
164pub struct LdEntityWorker 
165{ 
166    pub ramlimit: u64,
167    pub log_facilities: Option<Vec<String>>,
168    pub database: Database
169}
170
171
172#[derive(Clone, Debug, Serialize, Deserialize)]
173pub struct LdSockThread 
174{ 
175    pub threads: u64
176}
177
178
179#[derive(Clone, Debug, Serialize, Deserialize)]
180pub struct LdParser 
181{ 
182    pub log_facilities: Option<Vec<String>>,
183    pub database: Database
184}
185
186
187#[derive(Clone, Debug, Serialize, Deserialize)]
188pub struct LdLogDaemon 
189{ 
190    pub global: LdGlobal,
191    pub jail: LdJail,
192    pub action_exec: LdActionThreadExec,
193    pub control: LdControl,
194    pub ent_worker: LdEntityWorker,
195    pub sockthread: LdSockThread,
196    pub parser: LdParser
197}
198
199pub fn main()
200{
201
202// static
203    let mut curdir = std::env::current_dir().unwrap();
204    curdir.push("examples/struct_to_scheme/advanced4");
205    println!("{}", curdir.display());
206
207    let schm = init::SchemeInit::new_with_path(curdir).unwrap();
208    let res = schm.run_from_file("init_logdaemon.shm", None).unwrap();
209    let resser = res.get("logdaemon").unwrap().clone();
210
211// dynamic
212    let mut curdir = std::env::current_dir().unwrap();
213    curdir.push("examples/struct_to_scheme/advanced4/logdaemon.shm");
214    let (_, dynres) = 
215        environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap();
216
217// serialize dyn
218    let ret = serializator::Serialization::serialize(resser.clone(), dynres.clone()).unwrap();
219
220    let serialized = serde_json::to_string(&ret).unwrap();
221
222/*
223    let mut rc = RustCode::new(&["Clone", "Debug", "Serialize", "Deserialize"], &["Clone", "Debug", "Serialize", "Deserialize"]);
224    println!("Structures: ");
225    match resser.generate_rust_structs(&mut rc)
226    {
227        Ok(_) => 
228        {
229            println!("{}", rc);
230        },
231        Err(e) => 
232        {
233            println!("{}", e);
234        }
235    }
236*/
237
238
239    let n: LdLogDaemon = serde_json::from_str(&serialized).unwrap();
240
241    let scmp = from_struct(n, resser.clone());
242    
243    if scmp.is_err() == true
244    {
245        panic!("{}", scmp.err().unwrap());
246    }
247    else
248    {
249        let out_res = scmp.unwrap();
250        println!("{}", out_res);
251
252        let lex_out = Lexer::from_str(&out_res.to_string(), "test").unwrap();
253
254        let dyn_root = environment::DynEnvironment::new_root(resser.clone()).unwrap();
255
256        let dyn_res =
257            environment::DynEnvironment::run(lex_out, dyn_root);
258
259        if dyn_res.is_err() == true
260        {
261            println!("{}", dyn_res.err().unwrap());
262        }
263
264
265    }
266    
267}
268