use std::fmt::{Display, Formatter};
use serde::{Deserialize, Serialize};
use url::Url;
use crate::name;
use crate::resource::NodeId;
name!(
LicenseKey
);
name!(
LicenseName
);
name!(
SpdxId
);
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
pub struct License {
key: LicenseKey,
name: LicenseName,
spdx_id: SpdxId,
url: Url,
node_id: NodeId,
}
impl License {
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn key(&self) -> &LicenseKey {
&self.key
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn name(&self) -> &LicenseName {
&self.name
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn spdx_id(&self) -> &SpdxId {
&self.spdx_id
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn url(&self) -> &Url {
&self.url
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn node_id(&self) -> &NodeId {
&self.node_id
}
}
impl Display for License {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
#[cfg(test)]
mod tests {
use url::Url;
use crate::resource::NodeId;
use super::{License, LicenseKey, LicenseName, SpdxId};
#[test]
fn trait_deserialize() {
let license: License =
serde_json::from_str(include_str!("../../tests/fixtures/resource/license.json"))
.unwrap();
assert_eq!("apache-2.0", license.key().get());
}
#[test]
fn trait_display() {
let license = License {
key: LicenseKey::new("apache-2.0"),
name: LicenseName::new("Apache License 2.0"),
spdx_id: SpdxId::new("Apache-2.0"),
url: Url::parse("https://api.github.com/licenses/apache-2.0").unwrap(),
node_id: NodeId::new("MDc6TGljZW5zZTI="),
};
assert_eq!("Apache License 2.0", license.to_string());
}
#[test]
fn trait_send() {
fn assert_send<T: Send>() {}
assert_send::<License>();
}
#[test]
fn trait_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<License>();
}
}