1
- use anyhow:: Error ;
1
+ use anyhow:: { Context , Error } ;
2
2
use std:: collections:: { BTreeMap , BTreeSet } ;
3
3
use std:: io:: Write ;
4
4
use std:: path:: { Path , PathBuf } ;
5
5
6
6
mod cargo_metadata;
7
- mod licenses;
8
7
9
8
/// The entry point to the binary.
10
9
///
@@ -71,7 +70,7 @@ fn main() -> Result<(), Error> {
71
70
render_deps ( collected_cargo_metadata. iter ( ) , & mut buffer, & mut license_set) ?;
72
71
73
72
// Now we've rendered the tree, we can fetch all the license texts we've just referred to
74
- let license_map = download_licenses ( license_set) ?;
73
+ let license_map = load_licenses ( license_set) ?;
75
74
76
75
writeln ! ( buffer) ?;
77
76
writeln ! ( buffer, "## License Texts" ) ?;
@@ -208,7 +207,7 @@ fn render_deps<'a, 'b>(
208
207
}
209
208
210
209
/// Download licenses from SPDX Github
211
- fn download_licenses ( license_set : BTreeSet < String > ) -> Result < BTreeMap < String , String > , Error > {
210
+ fn load_licenses ( license_set : BTreeSet < String > ) -> Result < BTreeMap < String , String > , Error > {
212
211
let mut license_map = BTreeMap :: new ( ) ;
213
212
for license_string in license_set {
214
213
let mut licenses = Vec :: new ( ) ;
@@ -220,8 +219,8 @@ fn download_licenses(license_set: BTreeSet<String>) -> Result<BTreeMap<String, S
220
219
}
221
220
for license in licenses {
222
221
if !license_map. contains_key ( license) {
223
- let text = licenses :: get ( license) ?;
224
- license_map. insert ( license. to_owned ( ) , text. to_owned ( ) ) ;
222
+ let text = get_license_text ( license) ?;
223
+ license_map. insert ( license. to_owned ( ) , text) ;
225
224
}
226
225
}
227
226
}
@@ -272,6 +271,16 @@ struct License {
272
271
copyright : Vec < String > ,
273
272
}
274
273
274
+ /// Fetch a license text
275
+ pub fn get_license_text ( name : & str ) -> Result < String , anyhow:: Error > {
276
+ let license_path =
277
+ PathBuf :: from ( format ! ( "./src/tools/generate-copyright/licenses/{}.txt" , name) ) ;
278
+ let contents = std:: fs:: read_to_string ( & license_path) . with_context ( || {
279
+ format ! ( "Cannot open {:?} from CWD {:?}" , license_path, std:: env:: current_dir( ) )
280
+ } ) ?;
281
+ Ok ( contents)
282
+ }
283
+
275
284
/// Grab an environment variable as a PathBuf, or fail nicely.
276
285
fn env_path ( var : & str ) -> Result < PathBuf , Error > {
277
286
if let Some ( var) = std:: env:: var_os ( var) {
0 commit comments