docchi_intf/imp/structs/
root_source.rs

1use crate::imp::structs::source_builder::SourceBuilder;
2use crate::imp::to_member_source::MemberSource;
3use docchi_core::structs::ParamType;
4
5/// Contains information to generate the source code to access RootObject
6pub struct RootSource{
7    members : Vec<MemberSource>,
8}
9impl RootSource{
10    pub(crate) fn new(members : Vec<MemberSource>) -> RootSource{
11        RootSource{ members }
12    }
13    pub(crate) fn members(&self) -> &[MemberSource]{
14        &self.members
15    }
16
17    /// Generates the source code to access RootObject
18    pub fn to_string(&self) -> String{
19        let mut sb = SourceBuilder::new();
20
21        sb.push(0,     "\
22use docchi::core::intf::*;
23use docchi::core::structs::*;
24unsafe impl Send for RootIntf {}
25unsafe impl Sync for RootIntf {}
26#[derive(Debug)]
27pub struct RootIntf{
28    root : Box<RootObject>,
29    ptr : RootObjectPtr,
30}
31impl RootIntf{
32    pub fn new(obj : RootObject) -> RootIntf{
33		let mut root = Box::new(obj);
34		let ptr = RootObjectPtr::new(root.as_mut());
35		RootIntf { root, ptr }
36	}
37    pub fn root_obj_ref(&self) -> &RootObject{ self.root.as_ref() }
38    pub fn root_obj_ref_mut(&mut self) -> &mut RootObject{ self.root.as_mut() }
39    pub fn deconstruct(self) -> RootObject{ *self.root }
40");
41        for mem in self.members() {
42            match mem{
43                MemberSource::Param(param) =>{
44                    match param.param_type() {
45                        ParamType::Binary | ParamType::String | ParamType::IntArray | ParamType::FloatArray=> {
46                            //sb.push_without_newline(1, &param.get("root"));
47                            sb.push_without_newline(1, &param.get_def_immutable("root"));
48                            sb.push_without_newline(1, &param.get_immutable("root"));
49                            sb.push_without_newline(1, &param.get_mutable("root"));
50                            sb.push_without_newline(1, &param.set("root"));
51                        }
52                        _ => {
53                            sb.push_without_newline(1, &param.get("root"));
54                            sb.push_without_newline(1, &param.get_def("root"));
55                            sb.push_without_newline(1, &param.set("root"));
56                        }
57                    }
58
59                },
60                MemberSource::Table(data) =>{
61                    sb.push_without_newline(1, &data.get());
62                },
63                MemberSource::CList(l) =>{
64                    sb.push_without_newline(1, &l.get());
65                },
66                MemberSource::MList(m) =>{
67                    sb.push_without_newline(1, &m.get());
68                },
69                MemberSource::Cil(_) =>{},
70                MemberSource::Mil(_) =>{},
71            }
72        }
73        sb.push(0, "}");
74
75        for mem in self.members(){
76            match mem{
77                MemberSource::Table(data) =>{
78                    sb.push(0, &data.to_string())
79                },
80                MemberSource::CList(l) =>{
81                    sb.push(0, &l.to_string())
82                },
83                MemberSource::MList(m) =>{
84                    sb.push(0, &m.to_string())
85                },
86                MemberSource::Param(_) =>{},
87                MemberSource::Cil(_) =>{},
88                MemberSource::Mil(_)=>{},
89            }
90        }
91
92        sb.to_string()
93    }
94
95    pub fn to_string_with_cfg_test(&self) -> String{
96        let mut sb = SourceBuilder::new();
97        sb.push(0, &format!("#[cfg(test)] pub mod test{{"));
98        sb.push(1, &self.to_string());
99        sb.push(0, "}");
100        sb.to_string()
101    }
102}