1
1
use anyhow:: Result ;
2
- use cairo_lang_compiler:: project:: { AllCratesConfig , ProjectConfig , ProjectConfigContent } ;
3
- use cairo_lang_filesystem:: cfg:: { Cfg , CfgSet } ;
4
- use cairo_lang_filesystem:: db:: { CrateSettings , Edition , ExperimentalFeaturesConfig , FilesGroup } ;
5
2
use clap:: Parser ;
6
- use scarb_metadata:: {
7
- CompilationUnitComponentMetadata , CompilationUnitMetadata , Metadata , MetadataCommand ,
8
- PackageId , PackageMetadata ,
9
- } ;
3
+
4
+ use scarb_metadata:: MetadataCommand ;
10
5
use scarb_ui:: args:: PackagesFilter ;
11
- use smol_str:: { SmolStr , ToSmolStr } ;
12
- use std:: collections:: HashMap ;
13
- use std:: path:: PathBuf ;
14
6
15
7
use cairo_lang_compiler:: db:: RootDatabase ;
16
- use cairo_lang_utils:: ordered_hash_map:: OrderedHashMap ;
8
+ use cairo_lang_filesystem:: db:: FilesGroup ;
9
+ use cairo_lang_filesystem:: ids:: CrateLongId ;
17
10
18
- use cairo_lang_defs :: db :: DefsGroup ;
19
- use cairo_lang_defs :: ids :: { LookupItemId , ModuleItemId , NamedLanguageElementId } ;
11
+ use compilation :: get_project_config ;
12
+ use types :: Crate ;
20
13
21
- use cairo_lang_filesystem :: ids :: { CrateLongId , Directory } ;
22
- use cairo_lang_semantic :: db :: SemanticGroup ;
14
+ mod compilation ;
15
+ mod types ;
23
16
24
17
#[ derive( Parser , Debug ) ]
25
18
#[ command( version, about, long_about = None ) ]
@@ -28,6 +21,16 @@ struct Args {
28
21
packages_filter : PackagesFilter ,
29
22
}
30
23
24
+ macro_rules! print_names {
25
+ ( $label: expr, $var: expr) => {
26
+ println!(
27
+ "{}: {:?}" ,
28
+ $label,
29
+ $var. iter( ) . map( |x| & x. item_data. name) . collect:: <Vec <_>>( )
30
+ ) ;
31
+ } ;
32
+ }
33
+
31
34
fn main ( ) -> Result < ( ) > {
32
35
let args = Args :: parse ( ) ;
33
36
@@ -42,166 +45,38 @@ fn main() -> Result<()> {
42
45
b. build ( ) ?
43
46
} ;
44
47
45
- let main_crate_id = db. intern_crate ( CrateLongId :: Real ( package_metadata. name . into ( ) ) ) ;
46
-
47
- let crate_modules = db. crate_modules ( main_crate_id) ;
48
-
49
- let mut item_documentation = HashMap :: new ( ) ;
50
-
51
- for module_id in crate_modules. iter ( ) {
52
- let module_items = db. module_items ( * module_id) . unwrap ( ) ;
53
-
54
- for item in module_items. iter ( ) {
55
- let item_doc = db. get_item_documentation ( LookupItemId :: ModuleItem ( * item) ) ;
56
- item_documentation. insert ( LookupItemId :: ModuleItem ( * item) , item_doc) ;
57
-
58
- if let ModuleItemId :: Trait ( trait_id) = * item {
59
- let trait_items_names = db. trait_required_item_names ( trait_id) . unwrap ( ) ;
48
+ let main_crate_id = db. intern_crate ( CrateLongId :: Real ( package_metadata. name . clone ( ) . into ( ) ) ) ;
49
+ let crate_ = Crate :: new ( db, main_crate_id) ;
60
50
61
- for trait_item_name in trait_items_names. into_iter ( ) {
62
- let trait_item_id = db
63
- . trait_item_by_name ( trait_id, trait_item_name)
64
- . unwrap ( )
65
- . unwrap ( ) ;
66
-
67
- let doc = db. get_item_documentation ( LookupItemId :: TraitItem ( trait_item_id) ) ;
68
- item_documentation. insert ( LookupItemId :: TraitItem ( trait_item_id) , doc) ;
69
- }
70
- }
71
- }
72
- }
73
-
74
- for ( item_id, doc) in item_documentation. iter ( ) {
75
- let name = match item_id {
76
- LookupItemId :: ModuleItem ( item_id) => item_id. name ( db) ,
77
- LookupItemId :: TraitItem ( item_id) => item_id. name ( db) ,
78
- LookupItemId :: ImplItem ( item_id) => item_id. name ( db) ,
79
- } ;
80
- println ! ( "{:?}: {:?} -> {:?}" , name, item_id, doc) ;
81
- }
51
+ print_module ( & crate_. root_module ) ;
82
52
83
53
Ok ( ( ) )
84
54
}
85
55
86
- fn get_project_config ( metadata : & Metadata , package_metadata : & PackageMetadata ) -> ProjectConfig {
87
- let compilation_unit_metadata =
88
- package_lib_compilation_unit ( metadata, package_metadata. id . clone ( ) )
89
- . expect ( "failed to find compilation unit for package" ) ;
90
- let corelib = get_corelib ( compilation_unit_metadata) ;
91
- let dependencies = get_dependencies ( compilation_unit_metadata) ;
92
- let crates_config = get_crates_config ( metadata, compilation_unit_metadata) ;
93
-
94
- ProjectConfig {
95
- base_path : package_metadata. root . clone ( ) . into ( ) ,
96
- corelib : Some ( Directory :: Real ( corelib. source_root ( ) . into ( ) ) ) ,
97
- content : ProjectConfigContent {
98
- crate_roots : dependencies,
99
- crates_config,
100
- } ,
56
+ fn print_module ( module : & types:: Module ) {
57
+ println ! ( "Module: {}" , module. full_path) ;
58
+ println ! (
59
+ "Submodules : {:?}" ,
60
+ module
61
+ . submodules
62
+ . iter( )
63
+ . map( |x| & x. full_path)
64
+ . collect:: <Vec <_>>( )
65
+ ) ;
66
+ print_names ! ( "Constants " , module. constants) ;
67
+ print_names ! ( "Uses " , module. uses) ;
68
+ print_names ! ( "Free Functions " , module. free_functions) ;
69
+ print_names ! ( "Structs " , module. structs) ;
70
+ print_names ! ( "Enums " , module. enums) ;
71
+ print_names ! ( "Type Aliases " , module. type_aliases) ;
72
+ print_names ! ( "Impl Aliases " , module. impl_aliases) ;
73
+ print_names ! ( "Traits " , module. traits) ;
74
+ print_names ! ( "Impls " , module. impls) ;
75
+ print_names ! ( "Extern Types " , module. extern_types) ;
76
+ print_names ! ( "Extern Functions" , module. extern_functions) ;
77
+
78
+ for submodule in & module. submodules {
79
+ println ! ( ) ;
80
+ print_module ( submodule) ;
101
81
}
102
82
}
103
-
104
- fn package_lib_compilation_unit (
105
- metadata : & Metadata ,
106
- package_id : PackageId ,
107
- ) -> Option < & CompilationUnitMetadata > {
108
- metadata
109
- . compilation_units
110
- . iter ( )
111
- . find ( |m| m. package == package_id && m. target . kind == LIB_TARGET_KIND )
112
- }
113
-
114
- fn get_corelib (
115
- compilation_unit_metadata : & CompilationUnitMetadata ,
116
- ) -> & CompilationUnitComponentMetadata {
117
- compilation_unit_metadata
118
- . components
119
- . iter ( )
120
- . find ( |du| du. name == CORELIB_CRATE_NAME )
121
- . expect ( "Corelib could not be found" )
122
- }
123
-
124
- fn get_dependencies (
125
- compilation_unit_metadata : & CompilationUnitMetadata ,
126
- ) -> OrderedHashMap < SmolStr , PathBuf > {
127
- compilation_unit_metadata
128
- . components
129
- . iter ( )
130
- . filter ( |du| du. name != CORELIB_CRATE_NAME )
131
- . map ( |cu| {
132
- (
133
- cu. name . to_smolstr ( ) ,
134
- cu. source_root ( ) . to_owned ( ) . into_std_path_buf ( ) ,
135
- )
136
- } )
137
- . collect ( )
138
- }
139
-
140
- fn get_crates_config (
141
- metadata : & Metadata ,
142
- compilation_unit_metadata : & CompilationUnitMetadata ,
143
- ) -> AllCratesConfig {
144
- let crates_config: OrderedHashMap < SmolStr , CrateSettings > = compilation_unit_metadata
145
- . components
146
- . iter ( )
147
- . map ( |component| {
148
- let pkg = metadata. get_package ( & component. package ) . unwrap_or_else ( || {
149
- panic ! (
150
- "failed to find = {} package" ,
151
- & component. package. to_string( )
152
- )
153
- } ) ;
154
- (
155
- SmolStr :: from ( & component. name ) ,
156
- get_crate_settings_for_package (
157
- pkg,
158
- component. cfg . as_ref ( ) . map ( |cfg_vec| build_cfg_set ( cfg_vec) ) ,
159
- ) ,
160
- )
161
- } )
162
- . collect ( ) ;
163
-
164
- AllCratesConfig {
165
- override_map : crates_config,
166
- ..Default :: default ( )
167
- }
168
- }
169
-
170
- fn get_crate_settings_for_package (
171
- package : & PackageMetadata ,
172
- cfg_set : Option < CfgSet > ,
173
- ) -> CrateSettings {
174
- let edition = package
175
- . edition
176
- . clone ( )
177
- . map_or ( Edition :: default ( ) , |edition| {
178
- let edition_value = serde_json:: Value :: String ( edition) ;
179
- serde_json:: from_value ( edition_value) . unwrap ( )
180
- } ) ;
181
-
182
- let experimental_features = ExperimentalFeaturesConfig {
183
- negative_impls : package
184
- . experimental_features
185
- . contains ( & String :: from ( "negative_impls" ) ) ,
186
- coupons : package
187
- . experimental_features
188
- . contains ( & String :: from ( "coupons" ) ) ,
189
- } ;
190
-
191
- CrateSettings {
192
- edition,
193
- cfg_set,
194
- experimental_features,
195
- }
196
- }
197
-
198
- fn build_cfg_set ( cfg : & [ scarb_metadata:: Cfg ] ) -> CfgSet {
199
- CfgSet :: from_iter ( cfg. iter ( ) . map ( |cfg| {
200
- serde_json:: to_value ( cfg)
201
- . and_then ( serde_json:: from_value :: < Cfg > )
202
- . expect ( "Cairo's `Cfg` must serialize identically as Scarb Metadata's `Cfg`." )
203
- } ) )
204
- }
205
-
206
- const LIB_TARGET_KIND : & str = "lib" ;
207
- const CORELIB_CRATE_NAME : & str = "core" ;
0 commit comments