Struct Serialization

Source
pub struct Serialization {}

Implementations§

Source§

impl Serialization

Source

pub fn serialize( root_ser: Arc<Serializator>, root_dp: Arc<DynProcedure>, ) -> SerializationRes<SerDynValue>

Performs a serialization of the collected data from the config which is stored in the DynProcedure. The returned result is prepared to be serialized and deserialized into structures which can be generated by static_scheme/generator.rs.

§Arguments
  • root_ser - a Serializator structure which contains all necessary information about how the data should be serialized.

  • root_dp - contains data obtained from the config file in form of the DynProcedure.

§Returns

A SerializationRes is returned with:

Examples found in repository?
examples/struct_to_scheme/vector_arg/struct2scheme.rs (line 49)
28pub fn main()
29{
30    let mut curdir = std::env::current_dir().unwrap();
31    curdir.push("examples/struct_to_scheme/vector_arg/test_vec1.shm");
32    println!("{}", curdir.display());
33
34    let schm = init::SchemeInit::new_with_path("examples/struct_to_scheme/vector_arg").unwrap();
35
36    let res = schm.run_from_file("test_vec1.shm", None).unwrap();
37
38    let resser = res.get("test10").unwrap().clone();
39
40    println!("Static: \n{:?}\n", res);
41
42
43    let mut curdir = std::env::current_dir().unwrap();
44    curdir.push("examples/struct_to_scheme/vector_arg/test_vec1_data.shm");
45
46
47    let (_, dynres) = environment::DynEnvironment::from_file(&curdir, resser.clone()).unwrap();
48
49    let ret = serializator::Serialization::serialize(resser.clone(), dynres.clone()).unwrap();
50
51    let serialized = serde_json::to_string(&ret).unwrap();
52
53
54    let n: CommonLevels = serde_json::from_str(&serialized).unwrap();
55
56    let resreser = from_struct(n, resser);
57    
58    if resreser.is_err() == true
59    {
60        panic!("{}", resreser.err().unwrap());
61    }
62    else
63    {
64        println!("{}", resreser.unwrap());
65    }
66}
More examples
Hide additional examples
examples/test10.rs (line 39)
16pub fn main()
17{
18    let mut curdir = std::env::current_dir().unwrap();
19    curdir.push("examples/test4.shm");
20    println!("{}", curdir.display());
21
22    let lex = lexer::Lexer::from_file(curdir).unwrap();
23    let schm = init::SchemeInit::new().unwrap();
24
25    let res = schm.run(None, &lex, None).unwrap();
26
27    let resser = res.get("test10").unwrap().clone();
28
29    println!("Static: \n{:?}\n", res);
30
31    let mut curdir = std::env::current_dir().unwrap();
32    curdir.push("examples/test11.shm");
33
34
35    let (_, dynres) = environment::DynEnvironment::from_file(&curdir, resser.clone()).unwrap();
36
37    println!("Dynamic: \n{:?}\n", dynres);
38
39    let ret = serializator::Serialization::serialize(resser, dynres.clone()).unwrap();
40
41    let serialized = serde_json::to_string(&ret).unwrap();
42
43
44    let tr = CommonLevels{lvl_a: -2..8, lvl_b: 4..=5};
45
46    let res = serde_json::to_string(&tr).unwrap();
47
48    println!("control: {}", res);
49
50    let deser: CommonLevels = serde_json::from_str(&serialized).unwrap();
51
52    println!("{:?}", deser);
53
54    if deser != tr 
55    {
56        println!("did not match!");
57    }
58}
examples/struct_to_scheme/enum_proc/struct2scheme.rs (line 44)
24pub fn main()
25{
26    let mut curdir = std::env::current_dir().unwrap();
27    curdir.push("examples/struct_to_scheme/enum_proc/init_use1.shm");
28    println!("{}", curdir.display());
29
30    let schm = init::SchemeInit::new_with_path("examples/struct_to_scheme/enum_proc").unwrap();
31
32    let res = schm.run_from_file("init_use1.shm", None).unwrap();
33
34    let resser = res.get("use1").unwrap().clone();
35
36    println!("Static: \n{:?}\n", res);
37
38
39    let mut curdir = std::env::current_dir().unwrap();
40    curdir.push("examples/struct_to_scheme/enum_proc/init_use_dyn1.shm");
41
42    let (_, dynres) = environment::DynEnvironment::from_file(&curdir, resser.clone()).unwrap();
43
44    let ret = serializator::Serialization::serialize(resser.clone(), dynres.clone()).unwrap();
45
46    let serialized = serde_json::to_string(&ret).unwrap();
47
48
49    let n: TestUseEnum = serde_json::from_str(&serialized).unwrap();
50
51    let resser = from_struct(n, resser);
52    
53    println!("result:");
54    if resser.is_err() == true
55    {
56        panic!("{}", resser.err().unwrap());
57    }
58    else
59    {
60        println!("{}", resser.unwrap());
61    }
62}
examples/complex_example_1/init_networking1.rs (line 32)
10fn main()
11{
12    let curdir = std::env::current_dir().unwrap();
13
14    println!("{}", curdir.display());
15
16    let schm = init::SchemeInit::new_with_path(curdir).unwrap();
17
18    let res = schm.run_from_file("examples/complex_example_1/init_networking.shm", None).unwrap();
19
20    let resser = res.get("networking").unwrap().clone();
21
22    let anres = resser.analyze().unwrap();
23
24    println!("errors:\n{}", anres);
25
26    let mut curdir = std::env::current_dir().unwrap();
27
28    curdir.push("examples/complex_example_1/init_networking_dyn1.shm");
29
30    let (_, dyn_res) = environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap();
31
32    let ret = serializator::Serialization::serialize(resser.clone(), dyn_res.clone()).unwrap();
33
34    let serialized = serde_json::to_string(&ret).unwrap();
35
36    println!("Result:\n{}", serialized);
37
38    let mut rc = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
39
40    println!("Structures: ");
41    match resser.generate_rust_code(&mut rc)
42    {
43        Ok(_) => 
44        {
45            println!("{}", rc);
46        },
47        Err(e) => 
48        {
49            println!("{}", e);
50        }
51    }
52
53}
examples/scheme_extensions/test_exts.rs (line 39)
15pub fn main()
16{
17    let mut curdir = std::env::current_dir().unwrap();
18    curdir.push("examples/scheme_extensions/test_exts.shm");
19    println!("{}", curdir.display());
20
21    let lex = lexer::Lexer::from_file(curdir).unwrap();
22    let schm = init::SchemeInit::new().unwrap();
23
24    let res = schm.run(None, &lex, None).unwrap();
25
26    let resser = res.get("test_exts").unwrap().clone();
27
28    println!("Static: \n{:?}\n", res);
29
30
31    let mut curdir = std::env::current_dir().unwrap();
32    curdir.push("examples/scheme_extensions/test_exts_data.shm");
33
34
35    let (_, dynres) = environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap();
36
37    println!("Dynamic: \n{:?}\n", dynres);
38
39    let ret = serializator::Serialization::serialize(resser, dynres.clone()).unwrap();
40
41    let serialized = serde_json::to_string(&ret).unwrap();
42
43
44    let tr = ExtTest{ var_name: "var1".to_string(), entity: "od".to_string(), title: "model123".to_string() };
45
46    let res = serde_json::to_string(&tr).unwrap();
47
48    println!("control: {}", res);
49
50    let deser: ExtTest = serde_json::from_str(&serialized).unwrap();
51
52    println!("{:?}", deser);
53
54    if deser != tr 
55    {
56        panic!("did not match!");
57    }
58    else
59    {
60        println!("matched!");
61    }
62}
examples/struct_to_scheme3/struct2scheme3.rs (line 202)
183pub fn main()
184{
185
186// static
187    let mut curdir = std::env::current_dir().unwrap();
188    curdir.push("examples/struct_to_scheme3");
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_scheme3/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}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.