Struct RustCode

Source
pub struct RustCode { /* private fields */ }
Expand description

A root instance which contains generated items and static information to generate the rust structs/enums.

Implementations§

Source§

impl RustCode

Source

pub const SERDE_PATH: &'static str = "serde::{Serialize, Deserialize}"

Source

pub const ANY_TYPE_ENUM: &'static str = "ShmTypeAnyField"

Source

pub const ANY_TYPE_ENUM_STR: &'static str = "String"

Source

pub const ANY_TYPE_ENUM_ENTITY: &'static str = "Entity"

Source

pub const ANY_TYPE_ENUM_VARIABLE: &'static str = "Variable"

Source

pub const ANY_TYPE_ENUM_UINT: &'static str = "UInt"

Source

pub const ANY_TYPE_ENUM_LONGUINT: &'static str = "LongUInt"

Source

pub const ANY_TYPE_ENUM_INT: &'static str = "Int"

Source

pub const ANY_TYPE_ENUM_LONGINT: &'static str = "LongInt"

Source

pub const ANY_TYPE_ENUM_BOOL: &'static str = "Bool"

Source

pub fn new( any_type_derives: Vec<RustCodeDerives>, any_type_jails: Vec<String>, serde_use_path: Option<String>, ) -> Self

Examples found in repository?
examples/define_test.rs (line 22)
6fn main()
7{
8    let mut curdir = std::env::current_dir().unwrap();
9    curdir.push("examples/defines_init.shm");
10
11    println!("{}", curdir.display());
12    
13    let lex = lexer::Lexer::from_file(curdir).unwrap();
14    let schm = init::SchemeInit::new().unwrap();
15
16    let res = schm.run(&lex, None).unwrap();
17
18    println!("{:?}", res);
19
20    let resser = res.get("defines").unwrap().clone();
21
22    let mut rc = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
23
24    println!("Structures: ");
25    match resser.generate_rust_code(&mut rc)
26    {
27        Ok(_) => 
28        {
29            println!("{}", rc);
30        },
31        Err(e) => 
32        {
33            println!("{}", e);
34        }
35    }
36}
More examples
Hide additional examples
examples/another/another.rs (line 21)
9fn main()
10{
11    let curdir = std::env::current_dir().unwrap();
12
13    println!("{}", curdir.display());
14
15    let schm = init::SchemeInit::new_with_path(curdir).unwrap();
16
17    let res = schm.run_from_file("examples/another/init_l.shm", None).unwrap();
18
19    let resser = res.get("logsmon").unwrap().clone();
20
21    let mut rc = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
22    println!("Structures: ");
23    match resser.generate_rust_code(&mut rc)
24    {
25        Ok(_) => 
26        {
27            println!("{}", rc);
28        },
29        Err(e) => 
30        {
31            println!("{}", e);
32        }
33    }
34
35    let anres = resser.analyze().unwrap();
36
37    println!("errors:\n{}", anres);
38
39    let mut curdir = std::env::current_dir().unwrap();
40
41    curdir.push("examples/another/data_l.shm");
42
43    let (_, _dyn_res) = environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap();
44
45    let resser = res.get("logsmon").unwrap().clone();
46
47    let anres = resser.analyze().unwrap();
48
49    println!("errors:\n{}", anres);
50
51
52
53}
examples/complex_example_1/init_networking1.rs (line 38)
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_enum/enumtest.rs (line 53)
39fn main()
40{
41    let curdir = std::env::current_dir().unwrap();
42    //curdir.push("examples/test3.shm");
43    println!("{}", curdir.display());
44   // let lex = lexer::Lexer::from_file(curdir).unwrap();
45    let schm = init::SchemeInit::new_with_path(curdir).unwrap();
46
47    let res = schm.run_from_file("examples/scheme_enum/init_enum.shm", None).unwrap();
48
49    let resser = res.get("test1").unwrap().clone();
50
51    println!("{:?}", res);
52
53    let mut rc = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
54
55    println!("Structures: ");
56    match resser.generate_rust_code(&mut rc)
57    {
58        Ok(_) => 
59        {
60            println!("{}", rc);
61        },
62        Err(e) => 
63        {
64            panic!("{}", e);
65        }
66    }
67
68    let mut curdir = std::env::current_dir().unwrap();
69    curdir.push("examples/scheme_enum/enum.shm");
70
71    let (_, dyn_res) = environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap();
72
73    let ret = serializator::Serialization::serialize(resser.clone(), dyn_res.clone()).unwrap();
74
75    //println!("dynproc:\n{:?}\n", dyn_res);
76
77    let serialized = serde_json::to_string(&ret).unwrap();
78
79    println!("Result:\n{}", serialized);
80
81    let _deserialized: CommonLevels = serde_json::from_str(&serialized).unwrap();
82
83    let lvls = 
84        CommonLevels
85        {
86            enum1: MyEnum::Item1,
87            enum2: OurEnum::Item2,
88            enum3: MyEnum::Item2,
89            enum4: vec![OurEnum::HItem1, OurEnum::Item2, OurEnum::CItem],
90        };
91
92    let serialized_nat = serde_json::to_string(&lvls).unwrap();
93
94    println!("nat:{}", serialized_nat);
95    println!("ser:{}", serialized);
96
97    if serialized != serialized_nat
98    {
99        panic!("not equal");
100    }
101
102
103}
examples/scheme_proc_enum/init_use1.rs (line 69)
20fn main()
21{
22    let curdir = std::env::current_dir().unwrap();
23
24    println!("{}", curdir.display());
25
26    let schm = init::SchemeInit::new_with_path(curdir).unwrap();
27
28    let res = schm.run_from_file("examples/scheme_proc_enum/init_use1.shm", None).unwrap();
29
30    let resser = res.get("use1").unwrap().clone();
31
32    let mut curdir = std::env::current_dir().unwrap();
33
34    curdir.push("examples/scheme_proc_enum/init_use_dyn1.shm");
35
36   // let lex = lexer::Lexer::from_file(curdir).unwrap();
37    let (_dynenv, dyn_res) = 
38        environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap();
39   // let dynenv = environment::DynEnvironment::new_root(resser.clone());
40
41    //let dyn_res = environment::DynEnvironment::run(&lex, dynenv).unwrap();
42
43    let ret = serializator::Serialization::serialize(resser.clone(), dyn_res.clone()).unwrap();
44
45    let serialized = serde_json::to_string(&ret).unwrap();
46
47    println!("Result:\n{}", serialized);
48    let local_inst = 
49        TestUseEnum::ColorRgbaProfile
50        {
51            profile_name: "profile1".to_string(),
52            col_r: 6,
53            col_g: 5,
54            col_b: 64,
55            col_a: 100
56        };
57
58    let local_inst_ser = serde_json::to_string(&local_inst).unwrap();
59
60    assert_eq!(local_inst_ser.as_str(), serialized.as_str());
61
62
63    
64
65    let deserialized: TestUseEnum = serde_json::from_str(&serialized).unwrap();
66
67    println!("Deserialized result: \n{:?}", deserialized);
68
69    let mut rc = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
70
71    println!("Structures: ");
72    match resser.generate_rust_code(&mut rc)
73    {
74        Ok(_) => 
75        {
76            println!("{}", rc);
77        },
78        Err(e) => 
79        {
80            panic!("{}", e);
81        }
82    }
83
84}
examples/test3.rs (line 87)
32fn main()
33{
34    let curdir = std::env::current_dir().unwrap();
35    //curdir.push("examples/test3.shm");
36    println!("{}", curdir.display());
37   // let lex = lexer::Lexer::from_file(curdir).unwrap();
38    let schm = init::SchemeInit::new_with_path(curdir).unwrap();
39
40    let res = schm.run_from_file("examples/test3.shm", None).unwrap();
41
42    println!("{:?}", res);
43
44    let resser = res.get("test1").unwrap().clone();
45
46
47
48    let mut curdir = std::env::current_dir().unwrap();
49    curdir.push("examples/test2.shm");
50
51
52    let dynenv = environment::DynEnvironment::new_root(resser.clone()).unwrap();
53
54    let dyn_res = environment::DynEnvironment::run_from_file(curdir, dynenv).unwrap();
55
56    let ret = serializator::Serialization::serialize(resser.clone(), dyn_res.clone()).unwrap();
57
58    //println!("dynproc:\n{:?}\n", dyn_res);
59
60    let serialized = serde_json::to_string(&ret).unwrap();
61
62    println!("Result:\n{}", serialized);
63
64    let _deserialized: CommonLevels = serde_json::from_str(&serialized).unwrap();
65
66    let lvls = 
67        Levels
68        {
69            name: "testing1234".to_string(),
70            ch_a: Some(100),
71            ch_b: 2,
72            g_type: vec![GeomType::Line(4, 6, 5), GeomType::Ray{l: 56, w: 44}, GeomType::Test],
73        };
74
75    let cl = CommonLevels{lvl: lvls};
76
77    let serialized_nat = serde_json::to_string(&cl).unwrap();
78
79    println!("nat:{}", serialized_nat);
80    println!("ser:{}", serialized);
81
82    if serialized != serialized_nat
83    {
84        println!("not equal");
85    }
86
87    let mut rc = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
88
89    println!("Structures: ");
90    match resser.generate_rust_code(&mut rc)
91    {
92        Ok(_) => 
93        {
94            println!("{}", rc);
95        },
96        Err(e) => 
97        {
98            println!("{}", e);
99        }
100    }
101}
Source

pub fn contains(&self, item_name: &str) -> bool

Examples found in repository?
examples/common_search/common_search.rs (line 52)
9fn main()
10{
11    let curdir = std::env::current_dir().unwrap();
12
13    println!("{}", curdir.display());
14
15    // init parser instance
16    let schm = init::SchemeInit::new_with_path(curdir).unwrap();
17
18    // read a static scheme which describes our data structures i.e files filters.shm and logsmon.shm
19    let mut res = schm.run_from_file("examples/common_search/filters.shm", None).unwrap();
20    res.merge(schm.run_from_file("examples/common_search/logsmon.shm", None).unwrap());
21
22    // get our serializator instance which was parsed from static file
23    let resser = res.get("filters").unwrap().clone();
24
25    // creating new `RustCode` instance where the data structs from serializator `filters` will be placed
26    let mut rc1 = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
27    resser.generate_rust_code(&mut rc1).unwrap();
28
29    // creating new `RustCode` instance where the data structs from serializator `logsmon` will be placed
30    let resser = res.get("logsmon").unwrap().clone();
31    let mut rc2 = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
32    resser.generate_rust_code(&mut rc2).unwrap();
33
34    println!("after removing commons");
35
36    // search for common structs/enums between RustCode instances for filter.shm and logsmon.shm
37    let t = std::time::Instant::now();
38    let com_res = RustCode::search_common("super::structs::struct_common", vec!(&mut rc1, &mut rc2), vec![RustCodeDerives::Debug], vec![], None);
39    let s = t.elapsed();
40    println!("time taken: {:?}", s);
41    println!("{}", rc1);
42
43    println!("{}", rc2);
44    
45    // if something was found, then it will be stored in inner type of Option
46    if com_res.is_some() == true
47    {
48        let com = com_res.unwrap();
49        
50        println!("Common structs: \n\n{}", com);
51
52        assert_eq!(com.contains("LdFilterSets"), true);
53    }
54}
Source§

impl RustCode

Source

pub fn search_common( common_file_usepath: &'static str, items: Vec<&mut RustCode>, any_type_derives: Vec<RustCodeDerives>, any_type_jails: Vec<String>, serde_use_path: Option<String>, ) -> Option<RustCode>

A static method which can be used to search for the common strucutures/enums in generated Rust code. A structures and enums are searched by its title and its content. All found common structures are removed from the instances and returned in RustCode instance. The RustCodeDerivRepl and serde_path and serde_crate_path are not derived. It is possible to set it later.

§Arguments
  • common_file_usepath - a str path to the location with common structures. i.e super::structs::common_structs where common_structs is a rs file.

  • items - a Vec with mut references to initialized instances of RustCode where the common structs will be searched.

  • deriv_enum - an array with items for #[derive] for enum.

  • deriv_struct - an array with items for #derive` for structs.

§Returns

An Option is returned where:

  • Some is returned with instance

  • None is returned if no repeating structures/enums where found.

Examples found in repository?
examples/common_search/common_search.rs (line 38)
9fn main()
10{
11    let curdir = std::env::current_dir().unwrap();
12
13    println!("{}", curdir.display());
14
15    // init parser instance
16    let schm = init::SchemeInit::new_with_path(curdir).unwrap();
17
18    // read a static scheme which describes our data structures i.e files filters.shm and logsmon.shm
19    let mut res = schm.run_from_file("examples/common_search/filters.shm", None).unwrap();
20    res.merge(schm.run_from_file("examples/common_search/logsmon.shm", None).unwrap());
21
22    // get our serializator instance which was parsed from static file
23    let resser = res.get("filters").unwrap().clone();
24
25    // creating new `RustCode` instance where the data structs from serializator `filters` will be placed
26    let mut rc1 = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
27    resser.generate_rust_code(&mut rc1).unwrap();
28
29    // creating new `RustCode` instance where the data structs from serializator `logsmon` will be placed
30    let resser = res.get("logsmon").unwrap().clone();
31    let mut rc2 = RustCode::new(vec![RustCodeDerives::Debug], vec![], None);
32    resser.generate_rust_code(&mut rc2).unwrap();
33
34    println!("after removing commons");
35
36    // search for common structs/enums between RustCode instances for filter.shm and logsmon.shm
37    let t = std::time::Instant::now();
38    let com_res = RustCode::search_common("super::structs::struct_common", vec!(&mut rc1, &mut rc2), vec![RustCodeDerives::Debug], vec![], None);
39    let s = t.elapsed();
40    println!("time taken: {:?}", s);
41    println!("{}", rc1);
42
43    println!("{}", rc2);
44    
45    // if something was found, then it will be stored in inner type of Option
46    if com_res.is_some() == true
47    {
48        let com = com_res.unwrap();
49        
50        println!("Common structs: \n\n{}", com);
51
52        assert_eq!(com.contains("LdFilterSets"), true);
53    }
54}

Trait Implementations§

Source§

impl Display for RustCode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.