1- use anyhow:: Error ;
1+ use anyhow:: { Context , Error } ;
22use std:: collections:: { BTreeMap , BTreeSet } ;
33use std:: io:: Write ;
44use std:: path:: { Path , PathBuf } ;
55
66mod cargo_metadata;
7- mod licenses;
87
98/// The entry point to the binary.
109///
@@ -71,7 +70,7 @@ fn main() -> Result<(), Error> {
7170 render_deps ( collected_cargo_metadata. iter ( ) , & mut buffer, & mut license_set) ?;
7271
7372 // 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) ?;
7574
7675 writeln ! ( buffer) ?;
7776 writeln ! ( buffer, "## License Texts" ) ?;
@@ -208,7 +207,7 @@ fn render_deps<'a, 'b>(
208207}
209208
210209/// 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 > {
212211 let mut license_map = BTreeMap :: new ( ) ;
213212 for license_string in license_set {
214213 let mut licenses = Vec :: new ( ) ;
@@ -220,8 +219,8 @@ fn download_licenses(license_set: BTreeSet<String>) -> Result<BTreeMap<String, S
220219 }
221220 for license in licenses {
222221 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) ;
225224 }
226225 }
227226 }
@@ -272,6 +271,16 @@ struct License {
272271 copyright : Vec < String > ,
273272}
274273
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+
275284/// Grab an environment variable as a PathBuf, or fail nicely.
276285fn env_path ( var : & str ) -> Result < PathBuf , Error > {
277286 if let Some ( var) = std:: env:: var_os ( var) {
0 commit comments