Skip to content

Commit

Permalink
Better support for custom licenses
Browse files Browse the repository at this point in the history
Makes it possible to just add a file in `./licenses` and have it
available.
  • Loading branch information
sean-clayton committed Jul 15, 2021
1 parent 1ab4f1a commit e8d1c80
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
80 changes: 80 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ license = "Unlicense"

[dependencies]
license = "1.1.8"
include_dir = "0.6.1"
23 changes: 17 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use include_dir::{include_dir, Dir};
use std::collections::HashMap;
use std::env;
use std::io::{stdout, Error, ErrorKind, Write};

const LICENSE_DIR: Dir = include_dir!("./licenses");

fn main() -> std::io::Result<()> {
let args: Vec<String> = env::args().collect();
let id = args.get(1);
Expand Down Expand Up @@ -32,19 +35,18 @@ fn print_license_contents(stdout: &mut dyn Write, contents: String) -> std::io::
return write!(stdout, "{}", contents);
}

fn build_custom_licenses() -> HashMap<String, &'static [u8]> {
fn build_custom_licenses(directory: &'static Dir) -> HashMap<String, &'static [u8]> {
let mut custom_licenses = HashMap::<String, &[u8]>::new();

custom_licenses.insert(
"LicenseRef-ACAB-1.0".to_string(),
include_bytes!("../licenses/LicenseRef-ACAB-1.0"),
);
for license in directory.files() {
custom_licenses.insert(license.path.to_string(), license.contents());
}

custom_licenses
}

fn license_text(id: &str) -> std::result::Result<String, ()> {
let custom_licenses = build_custom_licenses();
let custom_licenses = build_custom_licenses(&LICENSE_DIR);

return create_custom_license_text(custom_licenses, id).or(create_license_text(id));
}
Expand Down Expand Up @@ -125,4 +127,13 @@ mod tests {

assert!(contents.is_ok());
}

#[test]
fn should_build_custom_licenses() {
const DIRECTORY: Dir = include_dir!("./test-data/licenses");

let license_map = build_custom_licenses(&DIRECTORY);

assert!(license_map.get("LicenseRef-Test").unwrap() == &"Test license".as_bytes());
}
}

0 comments on commit e8d1c80

Please sign in to comment.