use crate::upgrade::registry::UpgradeType;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UpgradeSelection {
pub all: bool,
pub patch_only: bool,
pub minor_and_patch: bool,
pub packages: Option<Vec<String>>,
pub dependencies: Option<Vec<String>>,
pub max_upgrade_type: Option<UpgradeType>,
}
impl UpgradeSelection {
#[must_use]
pub fn all() -> Self {
Self { all: true, ..Default::default() }
}
#[must_use]
pub fn patch_only() -> Self {
Self { patch_only: true, ..Default::default() }
}
#[must_use]
pub fn minor_and_patch() -> Self {
Self { minor_and_patch: true, ..Default::default() }
}
#[must_use]
pub fn packages(packages: Vec<String>) -> Self {
Self { packages: Some(packages), ..Default::default() }
}
#[must_use]
pub fn dependencies(deps: Vec<String>) -> Self {
Self { dependencies: Some(deps), ..Default::default() }
}
#[must_use]
pub fn matches_type(&self, upgrade_type: UpgradeType) -> bool {
if self.all {
return true;
}
if self.patch_only {
return upgrade_type == UpgradeType::Patch;
}
if self.minor_and_patch {
return matches!(upgrade_type, UpgradeType::Patch | UpgradeType::Minor);
}
if let Some(max_type) = self.max_upgrade_type {
return match max_type {
UpgradeType::Patch => upgrade_type == UpgradeType::Patch,
UpgradeType::Minor => {
matches!(upgrade_type, UpgradeType::Patch | UpgradeType::Minor)
}
UpgradeType::Major => true,
};
}
true
}
#[must_use]
pub fn matches_package(&self, package_name: &str) -> bool {
if let Some(ref packages) = self.packages {
packages.iter().any(|p| p == package_name)
} else {
true
}
}
#[must_use]
pub fn matches_dependency(&self, dependency_name: &str) -> bool {
if let Some(ref dependencies) = self.dependencies {
dependencies.iter().any(|d| d == dependency_name)
} else {
true
}
}
#[must_use]
pub fn has_filters(&self) -> bool {
self.all
|| self.patch_only
|| self.minor_and_patch
|| self.packages.is_some()
|| self.dependencies.is_some()
|| self.max_upgrade_type.is_some()
}
}