diff --git a/src/Luban.Job.Common/Source/TypeVisitors/RustTypeNameVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/RustTypeNameVisitor.cs index 548bc17..b560c73 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/RustTypeNameVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/RustTypeNameVisitor.cs @@ -10,25 +10,11 @@ namespace Luban.Job.Common.TypeVisitors { if (type.IsNullable) { - if (type.IsBean) - { - return $"std::option::Option>"; - } - else - { - return $"std::option::Option<{type.Apply(RustTypeUnderlyingNameVisitor.Ins)}>"; - } + return $"std::option::Option<{type.Apply(RustTypeUnderlyingNameVisitor.Ins)}>"; } else { - if (type.IsBean) - { - return $"std::rc::Rc<{type.Apply(RustTypeUnderlyingNameVisitor.Ins)}>"; - } - else - { - return type.Apply(RustTypeUnderlyingNameVisitor.Ins); - } + return type.Apply(RustTypeUnderlyingNameVisitor.Ins); } } } diff --git a/src/Luban.Server/Templates/config/rust_json/bean.tpl b/src/Luban.Server/Templates/config/rust_json/bean.tpl index 96ea41f..7a75cd1 100644 --- a/src/Luban.Server/Templates/config/rust_json/bean.tpl +++ b/src/Luban.Server/Templates/config/rust_json/bean.tpl @@ -11,6 +11,7 @@ * {{x.comment}} */ {{~end~}} +{{~if !x.is_abstract_type~}} #[allow(non_camel_case_types)] pub struct {{name}} { {{~for field in hierarchy_export_fields~}} @@ -20,12 +21,36 @@ pub {{field.rust_style_name}}: {{rust_define_type field.ctype}}, impl {{name}} { #[allow(dead_code)] - pub fn new(__js: &json::JsonValue) -> Result, LoadError> { + pub fn new(__js: &json::JsonValue) -> Result<{{name}}, LoadError> { let __b = {{name}} { {{~for field in hierarchy_export_fields~}} {{field.rust_style_name}}: {{rust_json_constructor ('__js["' + field.name + '"]') field.ctype}}, {{~end~}} }; - Ok(std::rc::Rc::new(__b)) + Ok(__b) } } +{{~else~}} +#[allow(non_camel_case_types)] +pub enum {{name}} { +{{~for child in x.hierarchy_not_abstract_children~}} + {{child.name}}(Box<{{child.rust_full_name}}>), +{{~end~}} +} + +impl {{name}} { + #[allow(dead_code)] + pub fn new(__js: &json::JsonValue) -> Result<{{name}}, LoadError> { + let __b = match __js["__type__"].as_str() { + Some(type_name) => match type_name { +{{~for child in x.hierarchy_not_abstract_children~}} + "{{child.name}}" => {{name}}::{{child.name}}(Box::new({{child.rust_full_name + '::new(&__js)?'}})), +{{~end~}} + _ => return Err(LoadError{}) + }, + None => return Err(LoadError{}) + }; + Ok(__b) + } +} +{{~end~}} diff --git a/src/Luban.Server/Templates/config/rust_json/table.tpl b/src/Luban.Server/Templates/config/rust_json/table.tpl index 7f19be1..6efb5c9 100644 --- a/src/Luban.Server/Templates/config/rust_json/table.tpl +++ b/src/Luban.Server/Templates/config/rust_json/table.tpl @@ -11,15 +11,15 @@ #[allow(non_camel_case_types)] pub struct {{name}} { {{~if x.is_map_table ~}} - data_list: Vec<{{rust_define_type value_type}}>, - data_map: std::collections::HashMap<{{rust_define_type key_type}}, {{rust_define_type value_type}}>, + data_list: Vec>, + data_map: std::collections::HashMap<{{rust_define_type key_type}}, std::rc::Rc<{{rust_define_type value_type}}>>, {{~else~}} - data: {{rust_define_type value_type}}, + data: {{rust_class_name value_type}}, {{~end~}} } impl {{name}}{ - pub fn new(__js: &json::JsonValue) -> Result, LoadError> { + pub fn new(__js: &json::JsonValue) -> Result<{{name}}, LoadError> { {{~if x.is_map_table ~}} if !__js.is_array() { return Err(LoadError{}); @@ -30,22 +30,30 @@ impl {{name}}{ }; for __e in __js.members() { - let __v = match {{rust_class_name value_type}}::new(__e) { + let __v = std::rc::Rc::new(match {{rust_class_name value_type}}::new(__e) { Ok(x) => x, Err(err) => return Err(err), - }; + }); let __v2 = std::rc::Rc::clone(&__v); t.data_list.push(__v); +{{~if !value_type.bean.is_abstract_type~}} t.data_map.insert(__v2.{{x.index_field.rust_style_name}}.clone(), __v2); +{{~else~}} + match &*__v2 { + {{~for child in value_type.bean.hierarchy_not_abstract_children~}} + {{rust_class_name value_type}}::{{child.name}}(__w__) => t.data_map.insert(__w__.{{x.index_field.rust_style_name}}.clone(), __v2), + {{~end~}} + }; +{{~end~}} } - Ok(std::rc::Rc::new(t)) + Ok(t) } #[allow(dead_code)] - pub fn get_data_map(self:&{{name}}) -> &std::collections::HashMap<{{rust_define_type key_type}}, {{rust_define_type value_type}}> { &self.data_map } + pub fn get_data_map(self:&{{name}}) -> &std::collections::HashMap<{{rust_define_type key_type}}, std::rc::Rc<{{rust_define_type value_type}}>> { &self.data_map } #[allow(dead_code)] - pub fn get_data_list(self:&{{name}}) -> &Vec<{{rust_define_type value_type}}> { &self.data_list } + pub fn get_data_list(self:&{{name}}) -> &Vec> { &self.data_list } #[allow(dead_code)] - pub fn get(self:&{{name}}, key: {{rust_define_type key_type}}) -> std::option::Option<&{{rust_define_type value_type}}> { self.data_map.get(&key) } + pub fn get(self:&{{name}}, key: {{rust_define_type key_type}}) -> std::option::Option<&std::rc::Rc<{{rust_define_type value_type}}>> { self.data_map.get(&key) } {{~else~}} if !__js.is_array() || __js.len() != 1 { return Err(LoadError{}); @@ -57,7 +65,7 @@ impl {{name}}{ let t = {{name}} { data: __v, }; - Ok(std::rc::Rc::new(t)) + Ok(t) } #[allow(dead_code)] pub fn get_data(self:&{{name}}) -> &{{rust_define_type value_type}} { &self.data } diff --git a/src/Luban.Server/Templates/config/rust_json/tables.tpl b/src/Luban.Server/Templates/config/rust_json/tables.tpl index aac8dc5..530d08c 100644 --- a/src/Luban.Server/Templates/config/rust_json/tables.tpl +++ b/src/Luban.Server/Templates/config/rust_json/tables.tpl @@ -15,18 +15,18 @@ pub struct {{name}} { * {{table.comment}} */ {{~end~}} - pub {{string.downcase table.name}}: std::rc::Rc<{{table.rust_full_name}}>, + pub {{string.downcase table.name}}: {{table.rust_full_name}}, {{~end~}} } impl {{name}} { #[allow(dead_code)] - pub fn new(loader: JsonLoader) -> std::result::Result, LoadError> { + pub fn new(loader: JsonLoader) -> std::result::Result { let tables = Tables { {{~for table in tables ~}} {{string.downcase table.name}}: {{table.rust_full_name}}::new(&loader("{{table.output_data_file}}")?)?, {{~end~}} }; - return Ok(std::rc::Rc::new(tables)); + return Ok(tables); } }