use std::path::PathBuf;
use anyhow::{Result, anyhow};
use io_pimdir::{PimdirBlobs, PimdirStore};
use crate::config::PimdirConfig;
pub struct PimdirClient {
pub(crate) store: PimdirStore,
pub(crate) blobs: PimdirBlobs,
pub(crate) source: String,
}
impl PimdirClient {
pub fn new(config: PimdirConfig) -> Result<Self> {
let root = shellexpand::full(&config.root.to_string_lossy())
.map(|expanded| PathBuf::from(expanded.into_owned()))
.unwrap_or_else(|_| config.root.clone());
let open = |source: &str| {
PimdirStore::open(&root, source)
.map_err(|err| anyhow!("Open pimdir store `{}`: {err}", root.display()))
};
let source = match config.source.clone() {
Some(source) => source,
None => {
let probe = open("probe")?;
match probe.distinct_sources()?.as_slice() {
[only] => only.clone(),
_ => "local".to_string(),
}
}
};
let store = open(&source)?;
let blobs = PimdirBlobs::open(&root);
Ok(Self {
store,
blobs,
source,
})
}
}