index : license-api-rs.git

ascending towards madness

use std::path::Path;

use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct LicenseDatabase {
    pub licenses: Vec<License>,
}

pub trait Queryable {
	fn get_license(self: &Self, id: &str) -> Option<License>;
}

impl Queryable for LicenseDatabase {
	fn get_license(self: &Self, id: &str) -> Option<License> {
		let query: Option<License> = self
			.licenses
			.iter()
			.map(|license| license.clone())
			.find(|license| license.id.eq(id));
	
		query
	}
}

impl LicenseDatabase {
    pub fn load_file(file: &Path) -> LicenseDatabase {
        let file_contents = std::fs::read_to_string(file).unwrap();
        let database: LicenseDatabase = serde_yaml::from_str(&file_contents).unwrap();

        database
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct License {
    pub id: String,
    pub filename: String,
    pub description: String,
}