use md5::Context;
use std::fs::File;
use std::io;
use std::path::Path;
pub const CLOSED_LICENSE: &str = "CLOSED";
fn file_md5<P: AsRef<Path>>(license_file: P) -> Result<String, io::Error> {
let mut file = File::open(license_file)?;
let mut context = Context::new();
io::copy(&mut file, &mut context)?;
Ok(format!("{:x}", context.compute()))
}
pub fn file(crate_root: &Path, rel_dir: &Path, license_name: &str, single_license: bool) -> String {
if license_name == CLOSED_LICENSE {
return "".into();
}
let special_name = format!("LICENSE-{}", license_name);
let lic_path = Path::new(license_name);
let spec_path = Path::new(&special_name);
let simple_path = Path::new("LICENSE");
let lic_abs_path = crate_root.join(lic_path);
let spec_abs_path = crate_root.join(spec_path);
let simple_abs_path = crate_root.join(simple_path);
if lic_abs_path.exists() {
let md5sum = file_md5(lic_abs_path).unwrap_or_else(|_| String::from("generateme"));
format!(
"file://{};md5={} \\\n",
rel_dir.join(lic_path).display(),
md5sum
)
} else if spec_abs_path.exists() {
let md5sum = file_md5(spec_abs_path).unwrap_or_else(|_| String::from("generateme"));
format!(
"file://{};md5={} \\\n",
rel_dir.join(spec_path).display(),
md5sum
)
} else if simple_abs_path.exists() && single_license {
let md5sum = file_md5(simple_abs_path).unwrap_or_else(|_| String::from("generateme"));
format!(
"file://{};md5={} \\\n",
rel_dir.join(simple_path).display(),
md5sum
)
} else {
format!("file://{};md5=generateme \\\n", license_name)
}
}