Struct shm_rs::static_scheme::generator::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
impl RustCode
pub const SERDE_PATH: &'static str = "serde::{Serialize, Deserialize}"
pub const ANY_TYPE_ENUM: &'static str = "ShmTypeAnyField"
pub const ANY_TYPE_ENUM_STR: &'static str = "String"
pub const ANY_TYPE_ENUM_ENTITY: &'static str = "Entity"
pub const ANY_TYPE_ENUM_VARIABLE: &'static str = "Variable"
pub const ANY_TYPE_ENUM_UINT: &'static str = "UInt"
pub const ANY_TYPE_ENUM_INT: &'static str = "Int"
pub const ANY_TYPE_ENUM_BOOL: &'static str = "Bool"
sourcepub fn new(
deriv_enum: &'static [&'static str],
deriv_struct: &'static [&'static str]
) -> Self
pub fn new( deriv_enum: &'static [&'static str], deriv_struct: &'static [&'static str] ) -> Self
Examples found in repository?
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
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
pub fn overide_serde_path(&mut self, ser_path: &'static str)
pub fn overide_serde_crate_path(&mut self, ser_crate_path: &'static str)
pub fn add_derive_override(&mut self, item: RustCodeDerivRepl)
source§impl RustCode
impl RustCode
sourcepub fn serach_common(
common_file_usepath: &'static str,
items: Vec<&mut RustCode>,
deriv_enum: &'static [&'static str],
deriv_struct: &'static [&'static str]
) -> Option<RustCode>
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 wherecommon_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:
Examples found in repository?
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());
}
}