use crate::{
error::Result,
meta::{tags::TagCache, users::UserTable},
query::SubHub,
store::Store,
Library,
};
use async_std::sync::{Arc, RwLock};
use std::{path::Path, result::Result as StdResult};
#[derive(Default)]
pub struct Builder {
#[allow(unused)]
offset: Option<String>,
}
impl Builder {
pub fn new() -> Self {
Self::default()
}
pub fn inspect_path<'tmp, P, S>(offset: P, _: S) -> StdResult<Arc<Library>, Self>
where
P: Into<&'tmp Path>,
S: Into<String>,
{
let p: &Path = offset.into();
if !p.exists() {
return Err(Self::new().offset(p));
}
let users = RwLock::new(UserTable::new());
let tag_cache = RwLock::new(TagCache::new());
let store = RwLock::new(Store::new());
let subs = SubHub::new();
Ok(Arc::new(Library {
users,
tag_cache,
store,
subs,
}))
}
pub fn offset<'tmp, P: Into<&'tmp Path>>(self, offset: P) -> Self {
let p: &Path = offset.into();
let offset = p.to_str().map(|s| s.to_string());
Self { offset, ..self }
}
pub fn root_sec<S: Into<String>>(self, _: S) -> Self {
self
}
pub fn build(self) -> Result<Arc<Library>> {
let users = RwLock::new(UserTable::new());
let tag_cache = RwLock::new(TagCache::new());
let store = RwLock::new(Store::new());
let subs = SubHub::new();
Library {
users,
tag_cache,
store,
subs,
}
.init()
.map(|l| Arc::new(l))
}
}