haj 0.3.4

the beautiful, cross-distro package manager and tui.
/// Semantic origin classification for a package, independent of distro repo names.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PackageSource {
    Official(String),
    External(String),
    Local,
}

impl PackageSource {
    pub fn from_repo(repo: &str) -> Self {
        match repo {
            "aur" | "local/aur" | "copr" | "ppa" | "flatpak" => Self::External(repo.to_string()),
            "local" => Self::Local,
            other => Self::Official(other.to_string()),
        }
    }

    pub fn is_external(&self) -> bool {
        matches!(self, Self::External(_))
    }

    pub fn is_local(&self) -> bool {
        matches!(self, Self::Local)
    }

    pub fn is_official(&self) -> bool {
        matches!(self, Self::Official(_))
    }
}

#[derive(Clone, Debug, Default)]
pub struct PackageModel {
    pub name: String,
    pub version: String,
    pub repo: String,
    pub is_installed: bool,
    pub is_upgradable: bool,
    pub size_mb: f64,
    pub desc: Option<String>,
}

impl PackageModel {
    pub fn source(&self) -> PackageSource {
        PackageSource::from_repo(&self.repo)
    }

    pub fn is_external(&self) -> bool {
        self.source().is_external()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_package_source_classification() {
        assert_eq!(
            PackageSource::from_repo("aur"),
            PackageSource::External("aur".into())
        );
        assert!(PackageSource::from_repo("local/aur").is_external());
        assert!(PackageSource::from_repo("copr").is_external());
        assert!(PackageSource::from_repo("local").is_local());
        assert_eq!(
            PackageSource::from_repo("extra"),
            PackageSource::Official("extra".into())
        );
        assert!(PackageSource::from_repo("fedora").is_official());
    }
}