@@ -3,25 +3,33 @@ mod r#static {
33 use serde_json:: Value ;
44 use std:: { fs, io, path:: Path } ;
55
6- fn dir_tree_to_list ( dir : impl AsRef < Path > ) -> ( String , String ) {
6+ fn dir_tree_to_list ( dir : impl AsRef < Path > ) -> ( String , String , String ) {
77 let info_path = dir. as_ref ( ) . join ( "info.json" ) ;
8- let info_dot_json = match info_path. exists ( ) {
8+ let node_dir = match info_path. exists ( ) {
99 true => {
1010 let info = fs:: read_to_string ( & info_path) . unwrap ( ) ;
1111 let info: Value = serde_json:: from_str ( info. as_str ( ) ) . unwrap ( ) ;
1212 let mut info = info. as_object ( ) . unwrap ( ) . clone ( ) ;
1313 info. remove ( "$schema" ) ;
14- Some ( Value :: Object ( info) )
14+
15+ let path = dir. as_ref ( ) . display ( ) . to_string ( ) ;
16+ Some ( (
17+ path. split ( "_data/" ) . last ( ) . unwrap_or ( & path) . to_string ( ) ,
18+ Value :: Object ( info) ,
19+ ) )
1520 }
1621 false => None ,
1722 } ;
18- let this_node = match & info_dot_json {
19- Some ( info) => {
23+
24+ let this_node = match & node_dir {
25+ Some ( ( _, info) ) => {
2026 let path = dir. as_ref ( ) . display ( ) . to_string ( ) ;
2127 let path = path. split ( "_data/" ) . last ( ) . unwrap_or ( & path) ;
28+ let code = path. split ( '/' ) . next_back ( ) . unwrap_or ( path) . to_lowercase ( ) ;
2229
2330 format ! (
2431 r#"const {}: Node = Node {{
32+ code: "{}",
2533 name: NodeName {{
2634 en: {},
2735 ar: {},
@@ -31,6 +39,7 @@ mod r#static {
3139}};
3240"# ,
3341 path. replace( '/' , "_" ) . to_uppercase( ) ,
42+ code,
3443 info. get( "name" ) . unwrap( ) . get( "en" ) . unwrap( ) ,
3544 info. get( "name" ) . unwrap( ) . get( "ar" ) . unwrap( ) ,
3645 info. get( "name" ) . unwrap( ) . get( "fr" ) . unwrap( ) ,
@@ -50,7 +59,7 @@ mod r#static {
5059
5160 match ty. as_str( ) {
5261 "Specialty" | "Sector" => format!(
53- r#"NodeType::{}{{
62+ r#"NodeType::{} {{
5463 terms: NodeTerms {{
5564 per_year: 2,
5665 slots: &[7, 8, 9, 10],
@@ -69,10 +78,8 @@ mod r#static {
6978 None => String :: new ( ) ,
7079 } ;
7180
72- let this_match = match & info_dot_json {
73- Some ( _) => {
74- let path = dir. as_ref ( ) . display ( ) . to_string ( ) ;
75- let path = path. split ( "_data/" ) . last ( ) . unwrap_or ( & path) ;
81+ let this_match = match & node_dir {
82+ Some ( ( path, _) ) => {
7683 format ! (
7784 " \" {}\" => Some(&{}),\n " ,
7885 path,
@@ -83,12 +90,22 @@ mod r#static {
8390 } ;
8491
8592 let sub_dirs = fs:: read_dir ( & dir) . unwrap ( ) ;
86- let mut children: Vec < ( String , String ) > = sub_dirs
93+ let mut children_names = Vec :: new ( ) ;
94+ let mut children: Vec < ( String , String , String ) > = sub_dirs
8795 . filter_map ( |entry| {
8896 let entry = entry. unwrap ( ) ;
8997 let ty = entry. file_type ( ) . unwrap ( ) ;
9098 if ty. is_dir ( ) {
91- Some ( dir_tree_to_list ( entry. path ( ) ) )
99+ let path = entry. path ( ) ;
100+ children_names. push (
101+ path. display ( )
102+ . to_string ( )
103+ . split ( "_data/" )
104+ . last ( )
105+ . map ( |s| s. replace ( '/' , "_" ) . to_uppercase ( ) )
106+ . unwrap ( ) ,
107+ ) ;
108+ Some ( dir_tree_to_list ( path) )
92109 } else {
93110 None
94111 }
@@ -97,16 +114,36 @@ mod r#static {
97114 // to ensure deterministic output on different platforms
98115 children. sort ( ) ;
99116
117+ let mut children_names = children_names
118+ . iter ( )
119+ . map ( |name| format ! ( "&{}" , name) )
120+ . collect :: < Vec < String > > ( ) ;
121+ // to ensure deterministic output on different platforms
122+ children_names. sort ( ) ;
123+
124+ let this_children_match = format ! (
125+ r#" "{}" => vec![{}],
126+ "# ,
127+ match & node_dir {
128+ Some ( ( path, _) ) => path,
129+ None => "" ,
130+ } ,
131+ children_names. join( ", " )
132+ ) ;
133+
100134 let mut constants = String :: new ( ) ;
101135 let mut matches = String :: new ( ) ;
102- for ( c, m) in children {
103- constants. push_str ( & c) ;
104- matches. push_str ( & m) ;
136+ let mut children_matches = String :: new ( ) ;
137+ for ( c, m, chm) in & children {
138+ constants. push_str ( c) ;
139+ matches. push_str ( m) ;
140+ children_matches. push_str ( chm) ;
105141 }
106142
107143 (
108144 format ! ( "{}{}" , this_node, constants) ,
109145 format ! ( "{}{}" , this_match, matches) ,
146+ format ! ( "{}{}" , this_children_match, children_matches) ,
110147 )
111148 }
112149
@@ -123,8 +160,15 @@ pub fn get_node_by_path(path: &str) -> Option<&Node> {{
123160 match path {{
124161{} _ => None,
125162 }}
126- }}"## ,
127- string_tree. 0 , string_tree. 1
163+ }}
164+
165+ pub fn get_node_children_by_path(path: &str) -> Vec<&Node> {{
166+ match path {{
167+ {} _ => vec![],
168+ }}
169+ }}
170+ "## ,
171+ string_tree. 0 , string_tree. 1 , string_tree. 2
128172 ) ;
129173 fs:: create_dir_all ( "./src/static/_auto_generated" ) ?;
130174 fs:: write ( "./src/static/_auto_generated/data.rs" , data) ?;
0 commit comments