Files
packie/src/lib.rs
T
2026-08-09 21:16:48 +08:00

114 lines
2.9 KiB
Rust

mod common;
pub mod config;
pub mod install;
pub mod link;
pub mod local;
pub mod package;
pub mod profile;
pub mod remove;
pub mod repo;
pub mod version;
use common::fs::LockGuard;
use config::AllConfig;
use local::{Cache, DataDir, LocalDb};
use profile::Profile;
use rust_i18n::t;
rust_i18n::i18n!("locales/libpackie", fallback = "en");
/// Main Packie state.
#[derive(Debug)]
pub struct Packie {
profile: Profile,
local_db: LocalDb,
config: AllConfig,
local_data: DataDir,
cache: Cache,
_lock_guard: LockGuard,
}
impl Packie {
/// Returns profile of the Packie instance.
pub fn profile(&self) -> &Profile {
&self.profile
}
/// Returns cache manager for the Packie instance.
pub fn cache(&self) -> &Cache {
&self.cache
}
}
#[derive(Debug, Clone)]
pub struct PackieBuilder {
profile: Profile,
readonly: bool,
nonblocking: bool,
}
impl PackieBuilder {
pub fn new() -> Self {
Self {
profile: Profile::builtin(),
readonly: false,
nonblocking: false,
}
}
pub fn readonly(mut self, val: bool) -> Self {
self.readonly = val;
self
}
pub fn nonblocking(mut self, val: bool) -> Self {
self.nonblocking = val;
self
}
pub fn profile(mut self, val: Profile) -> Self {
self.profile = val;
self
}
pub fn build(self) -> Result<Packie, BuildPackieError> {
let mut _lock_guard = LockGuard::noop();
if !self.readonly {
_ = std::fs::create_dir_all(&self.profile.packie_data_dir);
_ = std::fs::create_dir_all(&self.profile.packie_cache_dir);
_ = std::fs::create_dir_all(&self.profile.packie_config_dir);
_ = std::fs::create_dir_all(&self.profile.pkg_dir);
let lock_path = self.profile.packie_data_dir.join("packie.lock");
_lock_guard =
LockGuard::open(lock_path, self.nonblocking).map_err(BuildPackieError::Lock)?;
}
let local_db_path = self.profile.packie_data_dir.join(LocalDb::FILENAME);
let open_local_db = if self.readonly {
LocalDb::open_ro
} else {
LocalDb::open_rw
};
let local_db = open_local_db(local_db_path).map_err(BuildPackieError::LocalDb)?;
let config = AllConfig::open(&self.profile.packie_config_dir);
let cache = Cache(self.profile.packie_cache_dir.clone());
let local_data = DataDir(self.profile.packie_data_dir.clone());
Ok(Packie {
profile: self.profile,
local_db,
config,
cache,
local_data,
_lock_guard,
})
}
}
#[derive(Debug, thiserror::Error)]
pub enum BuildPackieError {
#[error("{0}")]
LocalDb(rusqlite::Error),
#[error("{}", t!("BuildPackieError.Lock", error = .0))]
Lock(std::io::Error),
}