use serde::{Deserialize, Serialize};
use std::fmt::Debug;
#[derive(Serialize, Deserialize, Debug, Clone, Eq, Ord, PartialEq, PartialOrd, Default)]
pub struct Dependency {
pub name: String,
pub version: String,
}
#[derive(Serialize, Deserialize, Debug, Eq, Ord, PartialEq, PartialOrd, Clone, Default)]
pub struct RetrievedDependency {
pub name: String,
pub version: String,
pub dependency_type: String,
pub url: Option<String>,
pub licenses: Option<Vec<String>>,
pub validated: bool,
pub is_valid: bool,
pub is_ignored: bool,
pub error: Option<String>,
pub comment: Option<Comment>,
}
impl RetrievedDependency {
#[must_use]
pub fn new(
name: String,
version: String,
dependency_type: String,
url: Option<String>,
licenses: Option<Vec<String>>,
error: Option<String>,
comment: Option<Comment>,
) -> Self {
let has_licenses = licenses.is_some();
Self {
name,
version,
dependency_type,
url,
licenses: licenses.or_else(|| Some(vec!["NO-LICENSE".to_string()])),
validated: false,
is_valid: has_licenses && error.is_none(),
is_ignored: false,
error: error.or_else(|| {
if has_licenses {
None
} else {
Some("No License".to_owned())
}
}),
comment: comment.or_else(|| {
if has_licenses {
None
} else {
Some(Comment::removable("Consider ignoring this specific dependency. You can also accept the NO-LICENSE key."))
}
}),
}
}
}
#[derive(Serialize, Deserialize, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Clone)]
pub struct Comment {
pub text: String,
pub remove_when_valid: bool,
}
impl Comment {
pub fn removable(text: impl Into<String>) -> Self {
Self {
text: text.into(),
remove_when_valid: true,
}
}
pub fn non_removable(text: impl Into<String>) -> Self {
Self {
text: text.into(),
remove_when_valid: false,
}
}
}