use core::{
convert::{TryFrom, TryInto},
fmt,
};
use crate::ParseError;
mod decl;
mod serde;
#[doc(inline)]
pub use self::decl::SpdxLicense;
pub type Map<A> = [A; SpdxLicense::COUNT];
impl<'a> TryFrom<&'a str> for SpdxLicense {
type Error = ParseError<'a>;
#[inline]
fn try_from(id: &'a str) -> Result<Self, Self::Error> {
if id.is_empty() {
return Err(ParseError::Empty);
}
Self::_from_id(id).ok_or(ParseError::UnknownLicenseId(id))
}
}
impl fmt::Display for SpdxLicense {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.id().fmt(f)
}
}
impl SpdxLicense {
pub const COUNT: usize = Self::_COUNT;
#[inline]
pub fn all() -> impl DoubleEndedIterator<Item = Self> + ExactSizeIterator {
(0..(Self::COUNT as u16)).map(|l| unsafe {
core::mem::transmute(l)
})
}
#[inline]
pub fn parse<'a, I>(input: I) -> Result<Self, ParseError<'a>>
where I: TryInto<Self, Error = ParseError<'a>>
{
input.try_into()
}
#[inline]
pub const fn id(self) -> &'static str {
Self::ID[self as usize]
}
#[inline]
pub const fn name(self) -> &'static str {
Self::NAME[self as usize]
}
#[inline]
pub const fn is_libre(self) -> bool {
Self::LIBRE[self as usize]
}
#[inline]
pub const fn is_osi_approved(self) -> bool {
Self::OSI[self as usize]
}
#[inline]
pub const fn is_creative_commons(self) -> bool {
const MIN: usize = SpdxLicense::CcBy1 as usize;
const MAX: usize = SpdxLicense::CC01 as usize;
let val = self as usize;
(val >= MIN) & (val <= MAX)
}
}