pub mod apt;
pub mod detector;
pub mod dnf;
pub mod pacman;
pub mod zypper;
use crate::core::Mirror;
use anyhow::Result;
pub use apt::AptHandler;
pub use detector::{DistroDetector, DistroInfo};
pub use dnf::DnfHandler;
pub use pacman::PacmanHandler;
pub use zypper::ZypperHandler;
#[async_trait::async_trait]
pub trait DistroHandler: Send + Sync {
fn name(&self) -> &str;
fn detect(&self) -> bool;
async fn get_available_mirrors(&self) -> Result<Vec<Mirror>>;
fn get_current_mirrors(&self) -> Result<Vec<Mirror>>;
fn update_mirrors(&self, mirrors: &[Mirror]) -> Result<()>;
fn backup(&self) -> Result<()>;
fn restore_backup(&self) -> Result<()>;
fn validate(&self) -> Result<bool>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Distro {
Debian,
Ubuntu,
Fedora,
RHEL,
Arch,
Manjaro,
OpenSUSE,
Unknown,
}
impl Distro {
pub fn as_str(&self) -> &str {
match self {
Self::Debian => "Debian",
Self::Ubuntu => "Ubuntu",
Self::Fedora => "Fedora",
Self::RHEL => "RHEL",
Self::Arch => "Arch",
Self::Manjaro => "Manjaro",
Self::OpenSUSE => "openSUSE",
Self::Unknown => "Unknown",
}
}
pub fn get_handler(&self) -> Option<Box<dyn DistroHandler>> {
match self {
Self::Debian | Self::Ubuntu => Some(Box::new(AptHandler::new())),
Self::Fedora | Self::RHEL => Some(Box::new(DnfHandler::new())),
Self::Arch | Self::Manjaro => Some(Box::new(PacmanHandler::new())),
Self::OpenSUSE => Some(Box::new(ZypperHandler::new())),
Self::Unknown => None,
}
}
}
pub fn detect_handler() -> Result<Box<dyn DistroHandler>> {
let distro = DistroDetector::detect()?;
distro.get_handler()
.ok_or_else(|| anyhow::anyhow!("Unsupported distribution: {:?}", distro))
}
pub fn get_all_handlers() -> Vec<Box<dyn DistroHandler>> {
vec![
Box::new(AptHandler::new()),
Box::new(DnfHandler::new()),
Box::new(PacmanHandler::new()),
Box::new(ZypperHandler::new()),
]
}