capsule_library/library/
library.rs1use std::path::PathBuf;
2
3use capsule_core::sidecar::LibraryConfigCbor;
4
5use crate::db::DatabaseDriver;
6use crate::library::error::LibraryError;
7use crate::library::lock;
8use crate::sidecar_io::write_library_config;
9
10#[allow(dead_code)]
15pub struct Library {
16 pub root: PathBuf,
17 pub db: DatabaseDriver,
18 config: LibraryConfigCbor,
19}
20
21impl Library {
22 pub(crate) fn new(root: PathBuf, db: DatabaseDriver, config: LibraryConfigCbor) -> Self {
23 Self { root, db, config }
24 }
25
26 pub fn config(&self) -> &LibraryConfigCbor {
27 &self.config
28 }
29
30 pub fn close(mut self) -> Result<(), LibraryError> {
33 self.config.last_opened_at = now_secs();
34 let config_path = self.root.join(".library/config.cbor");
35 write_library_config(&config_path, &self.config)
36 .map_err(|e| LibraryError::Cbor(e.to_string()))?;
37 let root = self.root.clone();
38 std::mem::forget(self); lock::release(&root)?;
40 Ok(())
41 }
42}
43
44impl Drop for Library {
45 fn drop(&mut self) {
46 let _ = lock::release(&self.root);
47 }
48}
49
50fn now_secs() -> i64 {
51 std::time::SystemTime::now()
52 .duration_since(std::time::UNIX_EPOCH)
53 .unwrap_or_default()
54 .as_secs() as i64
55}