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, 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>,
pub suggested_licenses: Option<Vec<(String, f32)>>,
}
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>,
suggested_licenses: Option<Vec<(String, f32)>>,
) -> Self {
let has_licenses = licenses.is_some();
Self {
name,
version,
dependency_type,
url,
licenses,
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 manually checking this dependency's license. Remember this: https://choosealicense.com/no-permission/ and ignore it if you feel confident about it to avoid this warning."))
}
}),
suggested_licenses,
}
}
}
#[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,
}
}
}