1- use crate :: { error:: Result , web:: extractors:: rustdoc:: RustdocParams } ;
2- use anyhow:: { Context , bail} ;
3- use docs_rs_types:: { KrateName , ReqVersion , Version , VersionReq } ;
4- use rustwide:: { Toolchain , Workspace , cmd:: Command } ;
1+ use anyhow:: { Context , Result } ;
2+ use docs_rs_types:: { Version , VersionReq } ;
53use serde:: { Deserialize , Serialize } ;
64use std:: collections:: HashMap ;
7- use std:: path:: Path ;
85
9- pub ( crate ) struct CargoMetadata {
6+ pub struct CargoMetadata {
107 root : Package ,
118}
129
1310impl CargoMetadata {
14- pub ( crate ) fn load_from_rustwide (
15- workspace : & Workspace ,
16- toolchain : & Toolchain ,
17- source_dir : & Path ,
18- ) -> Result < Self > {
19- let res = Command :: new ( workspace, toolchain. cargo ( ) )
20- . args ( & [ "metadata" , "--format-version" , "1" ] )
21- . cd ( source_dir)
22- . log_output ( false )
23- . run_capture ( ) ?;
24- let [ metadata] = res. stdout_lines ( ) else {
25- bail ! ( "invalid output returned by `cargo metadata`" )
26- } ;
27- Self :: load_from_metadata ( metadata)
28- }
29-
30- #[ cfg( test) ]
31- pub ( crate ) fn load_from_host_path ( source_dir : & Path ) -> Result < Self > {
11+ #[ cfg( feature = "testing" ) ]
12+ pub fn load_from_host_path ( source_dir : & std:: path:: Path ) -> Result < Self > {
3213 let res = std:: process:: Command :: new ( "cargo" )
3314 . args ( [ "metadata" , "--format-version" , "1" , "--offline" ] )
3415 . current_dir ( source_dir)
3516 . output ( ) ?;
3617 let status = res. status ;
3718 if !status. success ( ) {
3819 let stderr = std:: str:: from_utf8 ( & res. stderr ) . unwrap_or ( "" ) ;
39- bail ! ( "error returned by `cargo metadata`: {status}\n {stderr}" )
20+ anyhow :: bail!( "error returned by `cargo metadata`: {status}\n {stderr}" )
4021 }
4122 Self :: load_from_metadata ( std:: str:: from_utf8 ( & res. stdout ) ?)
4223 }
4324
44- pub ( crate ) fn load_from_metadata ( metadata : & str ) -> Result < Self > {
25+ pub fn load_from_metadata ( metadata : & str ) -> Result < Self > {
4526 let metadata = serde_json:: from_str :: < DeserializedMetadata > ( metadata) ?;
4627 let root = metadata. resolve . root ;
4728 Ok ( CargoMetadata {
@@ -53,26 +34,26 @@ impl CargoMetadata {
5334 } )
5435 }
5536
56- pub ( crate ) fn root ( & self ) -> & Package {
37+ pub fn root ( & self ) -> & Package {
5738 & self . root
5839 }
5940}
6041
6142#[ derive( Debug , Deserialize , Serialize ) ]
62- pub ( crate ) struct Package {
63- pub ( crate ) id : String ,
64- pub ( crate ) name : String ,
65- pub ( crate ) version : Version ,
66- pub ( crate ) license : Option < String > ,
67- pub ( crate ) repository : Option < String > ,
68- pub ( crate ) homepage : Option < String > ,
69- pub ( crate ) description : Option < String > ,
70- pub ( crate ) documentation : Option < String > ,
71- pub ( crate ) dependencies : Vec < Dependency > ,
72- pub ( crate ) targets : Vec < Target > ,
73- pub ( crate ) readme : Option < String > ,
74- pub ( crate ) keywords : Vec < String > ,
75- pub ( crate ) features : HashMap < String , Vec < String > > ,
43+ pub struct Package {
44+ pub id : String ,
45+ pub name : String ,
46+ pub version : Version ,
47+ pub license : Option < String > ,
48+ pub repository : Option < String > ,
49+ pub homepage : Option < String > ,
50+ pub description : Option < String > ,
51+ pub documentation : Option < String > ,
52+ pub dependencies : Vec < Dependency > ,
53+ pub targets : Vec < Target > ,
54+ pub readme : Option < String > ,
55+ pub keywords : Vec < String > ,
56+ pub features : HashMap < String , Vec < String > > ,
7657}
7758
7859impl Package {
@@ -82,15 +63,15 @@ impl Package {
8263 . find ( |target| target. crate_types . iter ( ) . any ( |kind| kind != "bin" ) )
8364 }
8465
85- pub ( crate ) fn is_library ( & self ) -> bool {
66+ pub fn is_library ( & self ) -> bool {
8667 self . library_target ( ) . is_some ( )
8768 }
8869
8970 fn normalize_package_name ( & self , name : & str ) -> String {
9071 name. replace ( '-' , "_" )
9172 }
9273
93- pub ( crate ) fn package_name ( & self ) -> String {
74+ pub fn package_name ( & self ) -> String {
9475 self . library_name ( ) . unwrap_or_else ( || {
9576 self . targets
9677 . first ( )
@@ -99,25 +80,25 @@ impl Package {
9980 } )
10081 }
10182
102- pub ( crate ) fn library_name ( & self ) -> Option < String > {
83+ pub fn library_name ( & self ) -> Option < String > {
10384 self . library_target ( )
10485 . map ( |target| self . normalize_package_name ( & target. name ) )
10586 }
10687}
10788
10889#[ derive( Debug , Deserialize , Serialize ) ]
109- pub ( crate ) struct Target {
110- pub ( crate ) name : String ,
111- #[ cfg( not( test ) ) ]
90+ pub struct Target {
91+ pub name : String ,
92+ #[ cfg( not( feature = "testing" ) ) ]
11293 crate_types : Vec < String > ,
113- #[ cfg( test ) ]
114- pub ( crate ) crate_types : Vec < String > ,
115- pub ( crate ) src_path : Option < String > ,
94+ #[ cfg( feature = "testing" ) ]
95+ pub crate_types : Vec < String > ,
96+ pub src_path : Option < String > ,
11697}
11798
11899impl Target {
119- #[ cfg( test ) ]
120- pub ( crate ) fn dummy_lib ( name : String , src_path : Option < String > ) -> Self {
100+ #[ cfg( feature = "testing" ) ]
101+ pub fn dummy_lib ( name : String , src_path : Option < String > ) -> Self {
121102 Target {
122103 name,
123104 crate_types : vec ! [ "lib" . into( ) ] ,
@@ -127,25 +108,12 @@ impl Target {
127108}
128109
129110#[ derive( Debug , Clone , Deserialize , Serialize , PartialEq ) ]
130- pub ( crate ) struct Dependency {
131- pub ( crate ) name : String ,
132- pub ( crate ) req : VersionReq ,
133- pub ( crate ) kind : Option < String > ,
134- pub ( crate ) rename : Option < String > ,
135- pub ( crate ) optional : bool ,
136- }
137-
138- impl Dependency {
139- pub ( crate ) fn rustdoc_params ( & self ) -> RustdocParams {
140- RustdocParams :: new (
141- // I validated in the database, which makes me assume that renames are
142- // handled before storing the deps into the column.
143- self . name
144- . parse :: < KrateName > ( )
145- . expect ( "we validated that the dep name is always a valid KrateName" ) ,
146- )
147- . with_req_version ( ReqVersion :: Semver ( self . req . clone ( ) ) )
148- }
111+ pub struct Dependency {
112+ pub name : String ,
113+ pub req : VersionReq ,
114+ pub kind : Option < String > ,
115+ pub rename : Option < String > ,
116+ pub optional : bool ,
149117}
150118
151119impl bincode:: Encode for Dependency {
@@ -172,7 +140,7 @@ impl bincode::Encode for Dependency {
172140}
173141
174142impl Dependency {
175- #[ cfg( test ) ]
143+ #[ cfg( feature = "testing" ) ]
176144 pub fn new ( name : String , req : VersionReq ) -> Dependency {
177145 Dependency {
178146 name,
@@ -183,7 +151,7 @@ impl Dependency {
183151 }
184152 }
185153
186- #[ cfg( test ) ]
154+ #[ cfg( feature = "testing" ) ]
187155 pub fn set_optional ( mut self , optional : bool ) -> Self {
188156 self . optional = optional;
189157 self
0 commit comments