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_INT: &'static str = "Int"

source

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

source

pub fn new( deriv_enum: &'static [&'static str], deriv_struct: &'static [&'static str] ) -> Self

Examples found in repository?
examples/define_test.rs (line 21)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
fn main()
{
    let mut curdir = std::env::current_dir().unwrap();
    curdir.push("examples/defines_init.shm");

    println!("{}", curdir.display());
    
    let lex = lexer::Lexer::from_file(curdir).unwrap();
    let schm = init::SchemeInit::new().unwrap();

    let res = schm.run(&lex, None).unwrap();

    println!("{:?}", res);

    let resser = res.get("defines").unwrap().clone();

    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);
        }
    }
}
More examples
Hide additional examples
examples/complex_example_1/init_networking1.rs (line 41)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
fn main()
{
    let curdir = std::env::current_dir().unwrap();

    println!("{}", curdir.display());

    let schm = init::SchemeInit::new_with_path(curdir).unwrap();

    let res = schm.run_from_file("examples/complex_example_1/init_networking.shm", None).unwrap();

    let resser = res.get("networking").unwrap().clone();

    let anres = resser.analyze().unwrap();

    println!("errors:\n{}", anres);

    let mut curdir = std::env::current_dir().unwrap();

    curdir.push("examples/complex_example_1/init_networking_dyn1.shm");

    let lex = lexer::Lexer::from_file(curdir).unwrap();

    let dynenv = environment::DynEnvironment::new_root(resser.clone());

    let dyn_res = environment::DynEnvironment::run(&lex, dynenv).unwrap();

    let ret = serializator::Serialization::serialize(resser.clone(), dyn_res.clone()).unwrap();

    let serialized = serde_json::to_string(&ret).unwrap();

    println!("Result:\n{}", serialized);

    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);
        }
    }

}
examples/scheme_vector_proc/vector_proc.rs (line 41)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
fn main()
{
    let curdir = std::env::current_dir().unwrap();

    println!("{}", curdir.display());

    let schm = init::SchemeInit::new_with_path(curdir).unwrap();

    let res = schm.run_from_file("examples/scheme_vector_proc/init.shm", None).unwrap();

    let resser = res.get("use1").unwrap().clone();

    let mut curdir = std::env::current_dir().unwrap();

    curdir.push("examples/scheme_vector_proc/dyn.shm");

   // let lex = lexer::Lexer::from_file(curdir).unwrap();
    let (_dynenv, dyn_res) = 
        environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap();
   // let dynenv = environment::DynEnvironment::new_root(resser.clone());

    //let dyn_res = environment::DynEnvironment::run(&lex, dynenv).unwrap();

    let ret = serializator::Serialization::serialize(resser.clone(), dyn_res.clone()).unwrap();

    let serialized = serde_json::to_string(&ret).unwrap();

    println!("Result:\n{}", serialized);

    /*let deserialized: TestUseEnum = serde_json::from_str(&serialized).unwrap();

    println!("Deserialized result: \n{:?}", deserialized);*/

    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);
        }
    }

}
examples/scheme_proc_enum/init_use1.rs (line 52)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
fn main()
{
    let curdir = std::env::current_dir().unwrap();

    println!("{}", curdir.display());

    let schm = init::SchemeInit::new_with_path(curdir).unwrap();

    let res = schm.run_from_file("examples/scheme_proc_enum/init_use1.shm", None).unwrap();

    let resser = res.get("use1").unwrap().clone();

    let mut curdir = std::env::current_dir().unwrap();

    curdir.push("examples/scheme_proc_enum/init_use_dyn1.shm");

   // let lex = lexer::Lexer::from_file(curdir).unwrap();
    let (_dynenv, dyn_res) = 
        environment::DynEnvironment::from_file(curdir, resser.clone()).unwrap();
   // let dynenv = environment::DynEnvironment::new_root(resser.clone());

    //let dyn_res = environment::DynEnvironment::run(&lex, dynenv).unwrap();

    let ret = serializator::Serialization::serialize(resser.clone(), dyn_res.clone()).unwrap();

    let serialized = serde_json::to_string(&ret).unwrap();

    println!("Result:\n{}", serialized);

    let deserialized: TestUseEnum = serde_json::from_str(&serialized).unwrap();

    println!("Deserialized result: \n{:?}", deserialized);

    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);
        }
    }

}
examples/scheme_enum/enumtest.rs (line 84)
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
fn main()
{
    let curdir = std::env::current_dir().unwrap();
    //curdir.push("examples/test3.shm");
    println!("{}", curdir.display());
   // let lex = lexer::Lexer::from_file(curdir).unwrap();
    let schm = init::SchemeInit::new_with_path(curdir).unwrap();

    let res = schm.run_from_file("examples/scheme_enum/init_enum.shm", None).unwrap();

    let resser = res.get("test1").unwrap().clone();

    println!("{:?}", res);

    let mut curdir = std::env::current_dir().unwrap();
    curdir.push("examples/scheme_enum/enum.shm");

    let lex = lexer::Lexer::from_file(curdir).unwrap();

    let dynenv = environment::DynEnvironment::new_root(resser.clone());

    let dyn_res = environment::DynEnvironment::run(&lex, dynenv).unwrap();

    let ret = serializator::Serialization::serialize(resser.clone(), dyn_res.clone()).unwrap();

    //println!("dynproc:\n{:?}\n", dyn_res);

    let serialized = serde_json::to_string(&ret).unwrap();

    println!("Result:\n{}", serialized);

    let _deserialized: CommonLevels = serde_json::from_str(&serialized).unwrap();

    let lvls = 
        CommonLevels
        {
            enum1: MyEnum::Item1,
            enum2: OurEnum::Item2,
        };

    let serialized_nat = serde_json::to_string(&lvls).unwrap();

    println!("nat:{}", serialized_nat);
    println!("ser:{}", serialized);

    if serialized != serialized_nat
    {
        println!("not equal");
    }

    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);
        }
    }
}
examples/test3.rs (line 86)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
fn main()
{
    let curdir = std::env::current_dir().unwrap();
    //curdir.push("examples/test3.shm");
    println!("{}", curdir.display());
   // let lex = lexer::Lexer::from_file(curdir).unwrap();
    let schm = init::SchemeInit::new_with_path(curdir).unwrap();

    let res = schm.run_from_file("examples/test3.shm", None).unwrap();

    let resser = res.get("test1").unwrap().clone();

    println!("{:?}", res);

    let mut curdir = std::env::current_dir().unwrap();
    curdir.push("examples/test2.shm");

    let lex = lexer::Lexer::from_file(curdir).unwrap();

    let dynenv = environment::DynEnvironment::new_root(resser.clone());

    let dyn_res = environment::DynEnvironment::run(&lex, dynenv).unwrap();

    let ret = serializator::Serialization::serialize(resser.clone(), dyn_res.clone()).unwrap();

    //println!("dynproc:\n{:?}\n", dyn_res);

    let serialized = serde_json::to_string(&ret).unwrap();

    println!("Result:\n{}", serialized);

    let _deserialized: CommonLevels = serde_json::from_str(&serialized).unwrap();

    let lvls = 
        Levels
        {
            name: "testing1234".to_string(),
            ch_a: Some(100),
            ch_b: 2,
            g_type: vec![GeomType::Line(4, 6, 5), GeomType::Ray{l: 56, w: 44}, GeomType::Test],
        };

    let cl = CommonLevels{lvl: lvls};

    let serialized_nat = serde_json::to_string(&cl).unwrap();

    println!("nat:{}", serialized_nat);
    println!("ser:{}", serialized);

    if serialized != serialized_nat
    {
        println!("not equal");
    }

    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);
        }
    }
}
source

pub fn overide_serde_path(&mut self, ser_path: &'static str)

source

pub fn overide_serde_crate_path(&mut self, ser_crate_path: &'static str)

source

pub fn add_derive_override(&mut self, item: RustCodeDerivRepl)

source§

impl RustCode

source

pub fn serach_common( common_file_usepath: &'static str, items: Vec<&mut RustCode>, deriv_enum: &'static [&'static str], deriv_struct: &'static [&'static str] ) -> 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 37)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
fn main()
{
    let curdir = std::env::current_dir().unwrap();

    println!("{}", curdir.display());

    // init parser instance
    let schm = init::SchemeInit::new_with_path(curdir).unwrap();

    // read a static scheme which describes our data structures i.e files filters.shm and logsmon.shm
    let mut res = schm.run_from_file("examples/common_search/filters.shm", None).unwrap();
    res.merge(schm.run_from_file("examples/common_search/logsmon.shm", None).unwrap());

    // get our serializator instance which was parsed from static file
    let resser = res.get("filters").unwrap().clone();

    // creating new `RustCode` instance where the data structs from serializator `filters` will be placed
    let mut rc1 = RustCode::new(&["Clone", "Debug", "Serialize", "Deserialize"], &["Clone", "Debug", "Serialize", "Deserialize"]);
    resser.generate_rust_structs(&mut rc1).unwrap();

    // creating new `RustCode` instance where the data structs from serializator `logsmon` will be placed
    let resser = res.get("logsmon").unwrap().clone();
    let mut rc2 = RustCode::new(&["Clone", "Debug", "Serialize", "Deserialize"], &["Clone", "Debug", "Serialize", "Deserialize"]);
    resser.generate_rust_structs(&mut rc2).unwrap();

    println!("after removing commons");

    // search for common structs/enums between RustCode instances for filter.shm and logsmon.shm
    let t = std::time::Instant::now();
    let com_res = RustCode::serach_common("super::structs::struct_common", vec!(&mut rc1, &mut rc2), &["Clone", "Debug", "Serialize", "Deserialize"], &["Clone", "Debug", "Serialize", "Deserialize"]);
    let s = t.elapsed();
    println!("time taken: {:?}", s);
    println!("{}", rc1);

    println!("{}", rc2);
    
    // if something was found, then it will be stored in inner type of Option
    if com_res.is_some() == true
    {
        println!("Common structs: \n\n{}", com_res.unwrap());
    }
}

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.