use std::collections::HashMap;
#[cfg(feature = "detection-cache")]
mod cache;
mod detect;
#[cfg(feature = "detection-inline-cache")]
mod inline_cache;
mod license;
pub use license::{LicenseType, TextData};
mod ngram;
mod preproc;
pub mod scan;
pub struct LicenseEntry {
pub original: TextData,
pub aliases: Vec<String>,
pub headers: Vec<TextData>,
pub alternates: Vec<TextData>,
}
impl LicenseEntry {
pub fn new(original: TextData) -> Self {
Self {
original,
aliases: Vec::new(),
alternates: Vec::new(),
headers: Vec::new(),
}
}
}
#[derive(Default)]
pub struct Store {
pub(crate) licenses: HashMap<String, LicenseEntry>,
}
impl Store {
pub fn new() -> Self {
Self {
licenses: HashMap::new(),
}
}
#[inline]
pub fn len(&self) -> usize {
self.licenses.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.licenses.is_empty()
}
#[inline]
pub fn licenses(&self) -> impl Iterator<Item = &String> {
self.licenses.keys()
}
#[inline]
pub fn get_original(&self, name: &str) -> Option<&TextData> {
self.licenses.get(name).map(|le| &le.original)
}
#[inline]
pub fn add_license(&mut self, name: String, data: TextData) {
let entry = LicenseEntry::new(data);
self.licenses.insert(name, entry);
}
#[inline]
pub fn insert_entry(&mut self, name: String, entry: LicenseEntry) {
self.licenses.insert(name, entry);
}
#[inline]
pub fn iter(&self) -> std::collections::hash_map::Iter<'_, String, LicenseEntry> {
self.licenses.iter()
}
#[inline]
pub fn add_variant(
&mut self,
name: &str,
variant: LicenseType,
data: TextData,
) -> Result<(), StoreError> {
let entry = self
.licenses
.get_mut(name)
.ok_or(StoreError::UnknownLicense)?;
match variant {
LicenseType::Alternate => {
entry.alternates.push(data);
}
LicenseType::Header => {
entry.headers.push(data);
}
LicenseType::Original => {
return Err(StoreError::OriginalInvalidForVariant);
}
}
Ok(())
}
#[inline]
pub fn aliases(&self, name: &str) -> Option<&Vec<String>> {
self.licenses.get(name).map(|le| &le.aliases)
}
#[inline]
pub fn set_aliases(&mut self, name: &str, aliases: Vec<String>) -> Result<(), StoreError> {
let entry = self
.licenses
.get_mut(name)
.ok_or(StoreError::UnknownLicense)?;
entry.aliases = aliases;
Ok(())
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum StoreError {
UnknownLicense,
OriginalInvalidForVariant,
}
impl std::fmt::Display for StoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnknownLicense => f.write_str("specified license did not exist in the store"),
Self::OriginalInvalidForVariant => {
f.write_str("attempted to add an original license text as a variant")
}
}
}
}
impl std::error::Error for StoreError {}