Skip to main content

pkg/backend/
mod.rs

1#[cfg(feature = "library")]
2pub mod pkgar_backend;
3
4use std::io;
5use thiserror::Error;
6
7use crate::{net_backend::DownloadError, package::PackageError, PackageName};
8#[cfg(feature = "library")]
9use crate::{package::RemotePackage, PackageState, Repository};
10
11// todo: make this better
12#[derive(Error, Debug)]
13pub enum Error {
14    #[error("Please add repos")]
15    ValidRepoNotFound,
16    #[error("Repository path is not valid: {0:?}")]
17    RepoPathInvalid(String),
18    #[error("Repository recursed infinitely with: {0:?}")]
19    RepoRecursion(Vec<PackageName>),
20    #[error("Cached package {0:?} source repo is not found")]
21    RepoCacheNotFound(PackageName),
22    #[error("Public key for {0:?} is not available")]
23    RepoNotLoaded(String),
24    #[error("Package {0:?} not found")]
25    PackageNotFound(PackageName),
26    #[error("Package {0:?} not installed")]
27    PackageNotInstalled(PackageName),
28    #[error("Package {0:?} name invalid")]
29    PackageNameInvalid(String),
30    #[error("{0}")]
31    Package(#[from] PackageError),
32    #[error("Path {0:?} isn't a Valid Unicode String")]
33    PathIsNotValidUnicode(String),
34    #[error("Content of {0:?} is not a valid UTF-8 content")]
35    ContentIsNotValidUnicode(String),
36    #[error("You don't have permissions required for this action, try performing it as root")]
37    MissingPermissions,
38
39    #[error("Package {0:?} is protected")]
40    ProtectedPackage(PackageName),
41
42    #[error("IO error: {0}")]
43    IO(io::Error),
44    #[error("Download error: {0}")]
45    Download(#[from] DownloadError),
46    #[error("Download error: {0}")]
47    TomlRead(#[from] toml::de::Error),
48    #[cfg(feature = "library")]
49    #[error("pkgar error: {0}")]
50    Pkgar(Box<pkgar::Error>),
51}
52
53#[cfg(feature = "library")]
54impl From<pkgar::Error> for Error {
55    fn from(value: pkgar::Error) -> Self {
56        Error::Pkgar(Box::new(value))
57    }
58}
59
60impl From<std::io::Error> for Error {
61    fn from(value: std::io::Error) -> Self {
62        if value.kind() == std::io::ErrorKind::PermissionDenied {
63            return Error::MissingPermissions;
64        } else {
65            return Error::IO(value);
66        }
67    }
68}
69
70#[cfg(feature = "library")]
71pub trait Backend {
72    /// individually install a package
73    fn install(&mut self, package: RemotePackage) -> Result<(), Error>;
74    /// individually uninstall a package
75    fn uninstall(&mut self, package: PackageName) -> Result<(), Error>;
76    /// individually upgrade a package
77    fn upgrade(&mut self, package: &RemotePackage) -> Result<(), Error>;
78    /// download package TOML data
79    fn get_package_detail(&self, package: &PackageName) -> Result<RemotePackage, Error>;
80    /// download repo TOML data
81    fn get_repository_detail(&self) -> Result<Repository, Error>;
82    /// get state of current installation
83    fn get_package_state(&self) -> PackageState;
84    /// check if there's pending transaction conflicts before committing
85    fn commit_check_conflict(&self) -> Result<&Vec<pkgar::TransactionConflict>, Error>;
86    /// commit all pending changes, and set state of current installation
87    fn commit_state(&mut self, new_state: PackageState) -> Result<usize, Error>;
88    /// abort all pending changes
89    fn abort_state(&mut self) -> Result<usize, Error>;
90}