mod.sbn 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. {{~ for mod in __mod.sub_mods ~}}
  2. pub mod {{mod.name}};
  3. {{~end~}}
  4. {{~ if __mod.name != '' ~}}
  5. use super::*;
  6. {{~end~}}
  7. use serde::Deserialize;
  8. {{~ for enum in __mod.enums ~}}
  9. {{~ if enum.comment != '' ~}}
  10. ///{{enum.comment | html.escape}}
  11. {{~end~}}
  12. {{~ if enum.is_flags ~}}
  13. bitflags::bitflags!{
  14. #[derive(Debug, Hash, Eq, PartialEq)]
  15. pub struct {{enum.name}} : u32 {
  16. {{~ for item in enum.items ~}}
  17. {{~if item.comment_or_alias != '' ~}}
  18. ///{{item.comment_or_alias | html.escape}}
  19. {{~end~}}
  20. const {{item.name}} = {{item.int_value}};
  21. {{~end~}}
  22. }
  23. }
  24. {{~else~}}
  25. #[derive(Deserialize, Debug, Hash, Eq, PartialEq, macros::EnumFromNum)]
  26. pub enum {{enum.name}} {
  27. {{~ for item in enum.items ~}}
  28. {{~if item.comment_or_alias != '' ~}}
  29. ///{{item.comment_or_alias | html.escape}}
  30. {{~end~}}
  31. {{item.name}} = {{item.value}},
  32. {{~end~}}
  33. {{~if enum.items.empty?~}}
  34. None
  35. {{~end~}}
  36. }
  37. impl From<i32> for {{enum.name}} {
  38. fn from(value: i32) -> Self {
  39. match value {
  40. {{~ for item in enum.items ~}}
  41. {{item.value}} => {{enum.name}}::{{item.name}},
  42. {{~end~}}
  43. _ => panic!("Invalid value for {{enum.name}}:{}", value),
  44. }
  45. }
  46. }
  47. {{~end~}}
  48. {{~end~}}
  49. {{~ for bean in __mod.beans
  50. hierarchy_fields = bean.hierarchy_export_fields
  51. ~}}
  52. #[derive(Debug)]
  53. {{~if bean.parent != ''~}}
  54. #[derive(macros::TryIntoBase)]
  55. {{~end~}}
  56. pub struct {{bean.name}} {
  57. {{~ for field in hierarchy_fields ~}}
  58. {{~if field.comment != '' ~}}
  59. /// {{escape_comment field.comment}}
  60. {{~end~}}
  61. pub {{field.name}}: {{declaring_type_name field.ctype}},
  62. {{~end~}}
  63. }
  64. {{~if bean.is_abstract_type~}}
  65. impl {{bean.name}} {
  66. pub fn new(json: &serde_json::Value) -> Result<std::sync::Arc<AbstractBase>, LubanError> {
  67. let type_id = json["$type"].as_str().unwrap();
  68. match type_id {
  69. {{~for child in bean.hierarchy_not_abstract_children~}}
  70. "{{impl_data_type child bean}}" => Ok(std::sync::Arc::new({{full_name child}}::new(json)?)),
  71. {{~end~}}
  72. _ => Err(LubanError::Bean(format!("Invalid type for {{bean.name}}:{}", type_id)))
  73. }
  74. }
  75. }
  76. pub trait T{{bean.name}} {
  77. {{~ for field in hierarchy_fields ~}}
  78. fn {{getter_name field.name}}(&self) -> &{{declaring_type_name field.ctype}};
  79. {{~end~}}
  80. }
  81. {{~for child in bean.hierarchy_not_abstract_children~}}
  82. impl {{base_trait_name bean}} for {{full_name child}} {
  83. {{~ for field in hierarchy_fields ~}}
  84. fn {{getter_name field.name}}(&self) -> &{{declaring_type_name field.ctype}} {
  85. &self.{{field.name}}
  86. }
  87. {{~end~}}
  88. }
  89. {{~end~}}
  90. impl<'a> GetBase<'a, &'a dyn {{base_trait_name bean}}> for AbstractBase {
  91. fn get_base(&'a self) -> Result<&'a dyn {{base_trait_name bean}}, LubanError> {
  92. {{~for child in bean.hierarchy_not_abstract_children~}}
  93. let base: Result<&{{full_name child}}, _> = self.try_into();
  94. if let Ok(r) = base {
  95. return Ok(r);
  96. }
  97. {{~end~}}
  98. Err(LubanError::Polymorphic(format!("Invalid type for {{bean.name}}")))
  99. }
  100. }
  101. {{~else~}}
  102. impl {{bean.name}}{
  103. pub fn new(json: &serde_json::Value) -> Result<{{bean.name}}, LubanError> {
  104. {{~ for field in hierarchy_fields ~}}
  105. {{deserialize_field field.name 'json' field.ctype}}
  106. {{~end~}}
  107. Ok({{bean.name}} { {{ for field in hierarchy_fields }}{{field.name}}, {{end}}})
  108. }
  109. }
  110. {{~end~}}
  111. {{~end~}}
  112. {{~ for table in __mod.tables
  113. key_type = declaring_type_name table.key_ttype
  114. if table.value_ttype.def_bean.is_abstract_type
  115. value_type = declaring_type_name table.value_ttype
  116. else
  117. value_type = "std::sync::Arc<"+declaring_type_name table.value_ttype+">"
  118. end
  119. name = table.name
  120. map_type = "std::collections::HashMap<"+key_type+", "+value_type+">"
  121. ~}}
  122. {{
  123. func index_type_name
  124. ret (declaring_type_name $0.type)
  125. end
  126. func table_union_map_type_name
  127. ret 'std::collections::HashMap<(' + (array.each table.index_list @index_type_name | array.join ', ') + '), ' + value_type + '>'
  128. end
  129. func table_key_list
  130. varName = $0
  131. indexList = table.index_list |array.each do; ret varName + '.' + (format_property_name __code_style $0.index_field.name); end;
  132. ret array.join indexList ', '
  133. end
  134. func table_param_def_list
  135. paramList = table.index_list |array.each do; ret (declaring_type_name $0.type) + ' ' + $0.index_field.name; end
  136. ret array.join paramList ', '
  137. end
  138. func table_param_name_list
  139. paramList = table.index_list |array.each do; ret $0.index_field.name; end
  140. ret array.join paramList ', '
  141. end
  142. }}
  143. {{~ if table.comment != '' ~}}
  144. ///{{table.comment | html.escape}}
  145. {{~end~}}
  146. #[derive(Debug)]
  147. pub struct {{table.name}} {
  148. {{~if table.is_map_table ~}}
  149. pub data_list: Vec<{{value_type}}>,
  150. pub data_map: {{map_type}},
  151. {{~else if table.is_list_table ~}}
  152. pub data_list: Vec<{{value_type}}>,
  153. {{~if table.is_union_index~}}
  154. pub data_map_union: {{table_union_map_type_name}},
  155. {{~else if !table.index_list.empty?~}}
  156. {{~for idx in table.index_list~}}
  157. pub data_map_{{idx.index_field.name}}: std::collections::HashMap<{{declaring_type_name idx.type}}, {{value_type}}>,
  158. {{~end~}}
  159. {{~end~}}
  160. {{~else~}}
  161. pub data: {{declaring_type_name table.value_ttype}},
  162. {{~end~}}
  163. }
  164. {{~if table.is_map_table ~}}
  165. impl {{name}} {
  166. pub fn new(json: &serde_json::Value) -> Result<std::sync::Arc<{{name}}>, LubanError> {
  167. let mut data_map: {{map_type}} = Default::default();
  168. let mut data_list: Vec<{{value_type}}> = vec![];
  169. for x in json.as_array().unwrap() {
  170. {{~ if table.value_ttype.is_bean ~}}
  171. {{deserialize_row "row" "x" table.value_ttype}}
  172. {{~else~}}
  173. let row: {{value_type}} = std::sync::Arc::new(serde_json::from_value(x.clone()).unwrap());
  174. {{~end~}}
  175. data_list.push(row.clone());
  176. {{~ if table.value_ttype.def_bean.is_abstract_type ~}}
  177. let key = <AbstractBase as GetBase<&dyn {{base_trait_name table.value_ttype.def_bean}}>>::get_base(std::ops::Deref::deref(&row))?;
  178. data_map.insert(key.{{getter_name table.index_field.name}}().clone(), row.clone());
  179. {{~else~}}
  180. data_map.insert(row.{{table.index_field.name}}.clone(), row.clone());
  181. {{~end~}}
  182. }
  183. Ok(std::sync::Arc::new({{name}} { data_map, data_list }))
  184. }
  185. pub fn get(&self, key: &{{key_type}}) -> Option<{{value_type}}> {
  186. self.data_map.get(key).map(|x| x.clone())
  187. }
  188. }
  189. impl std::ops::Index<{{key_type}}> for {{name}} {
  190. type Output = {{value_type}};
  191. fn index(&self, index: {{key_type}}) -> &Self::Output {
  192. &self.data_map.get(&index).unwrap()
  193. }
  194. }
  195. {{~else if table.is_list_table ~}}
  196. impl {{name}} {
  197. pub fn new(json: &serde_json::Value) -> Result<std::sync::Arc<{{name}}>, LubanError> {
  198. let mut data_list: Vec<{{value_type}}> = vec![];
  199. for x in json.as_array().unwrap() {
  200. {{~ if table.value_ttype.is_bean ~}}
  201. {{deserialize_row "row" "x" table.value_ttype}}
  202. {{~else~}}
  203. let row: {{value_type}} = std::sync::Arc::new(serde_json::from_value(x.clone()).unwrap());
  204. {{~end~}}
  205. data_list.push(row.clone());
  206. }
  207. {{~if table.is_union_index~}}
  208. let mut data_map_union: {{table_union_map_type_name}} = Default::default();
  209. for x in &data_list {
  210. data_map_union.insert(({{table_key_list "x"}}.clone()), x.clone());
  211. }
  212. {{~else if !table.index_list.empty?~}}
  213. {{~for idx in table.index_list~}}
  214. let mut data_map_{{idx.index_field.name}}: std::collections::HashMap<{{declaring_type_name idx.index_field.ctype}}, {{value_type}}> = Default::default();
  215. {{~end~}}
  216. for x in &data_list {
  217. {{~for idx in table.index_list~}}
  218. data_map_{{idx.index_field.name}}.insert(x.{{format_property_name __code_style idx.index_field.name}}.clone(), x.clone());
  219. {{~end~}}
  220. }
  221. {{~end~}}
  222. Ok(std::sync::Arc::new({{table.name}} {
  223. data_list,
  224. {{~if table.is_union_index~}}
  225. data_map_union,
  226. {{~else if !table.index_list.empty?~}}
  227. {{~for idx in table.index_list~}}
  228. data_map_{{idx.index_field.name}},
  229. {{~end~}}
  230. {{~end~}}
  231. }))
  232. }
  233. {{~if table.is_union_index~}}
  234. pub fn get(&self, key: &({{array.each table.index_list @index_type_name | array.join ', '}})) -> Option<{{value_type}}> {
  235. self.data_map_union.get(key).map(|x| x.clone())
  236. }
  237. {{~else if !table.index_list.empty? ~}}
  238. {{~for idx in table.index_list
  239. index = format_property_name __code_style idx.index_field.name
  240. ~}}
  241. pub fn get_by_{{index}}(&self, key: &{{declaring_type_name idx.type}}) -> Option<{{value_type}}> {
  242. self.data_map_{{index}}.get(key).map(|x| x.clone())
  243. }
  244. {{~end~}}
  245. {{~end~}}
  246. }
  247. {{~else~}}
  248. impl {{name}} {
  249. pub fn new(json: &serde_json::Value) -> Result<std::sync::Arc<{{name}}>, LubanError> {
  250. let json = json.as_array().unwrap();
  251. let n = json.len();
  252. if n != 1 { return Err(LubanError::Table(format!("table mode=one, but size != 1"))); }
  253. let data = {{full_name table.value_ttype.def_bean}}::new(&json[0])?;
  254. Ok(std::sync::Arc::new({{name}} { data }))
  255. }
  256. }
  257. {{~end~}}
  258. {{~end~}}