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_keys error: {0}")]
50    PkgarKeys(#[from] pkgar_keys::Error),
51    #[cfg(feature = "library")]
52    #[error("pkgar error: {0}")]
53    Pkgar(Box<pkgar::Error>),
54}
55
56#[cfg(feature = "library")]
57impl From<pkgar::Error> for Error {
58    fn from(value: pkgar::Error) -> Self {
59        Error::Pkgar(Box::new(value))
60    }
61}
62
63impl From<std::io::Error> for Error {
64    fn from(value: std::io::Error) -> Self {
65        if value.kind() == std::io::ErrorKind::PermissionDenied {
66            return Error::MissingPermissions;
67        } else {
68            return Error::IO(value);
69        }
70    }
71}
72
73#[cfg(feature = "library")]
74pub trait Backend {
75    /// individually install a package
76    fn install(&mut self, package: RemotePackage) -> Result<(), Error>;
77    /// individually uninstall a package
78    fn uninstall(&mut self, package: PackageName) -> Result<(), Error>;
79    /// individually upgrade a package
80    fn upgrade(&mut self, package: &RemotePackage) -> Result<(), Error>;
81    /// download package TOML data
82    fn get_package_detail(&self, package: &PackageName) -> Result<RemotePackage, Error>;
83    /// download repo TOML data
84    fn get_repository_detail(&self) -> Result<Repository, Error>;
85    /// get state of current installation
86    fn get_package_state(&self) -> PackageState;
87    /// check if there's pending transaction conflicts before committing
88    fn commit_check_conflict(&self) -> Result<&Vec<pkgar::TransactionConflict>, Error>;
89    /// commit all pending changes, and set state of current installation
90    fn commit_state(&mut self, new_state: PackageState) -> Result<usize, Error>;
91    /// abort all pending changes
92    fn abort_state(&mut self) -> Result<usize, Error>;
93}