1use crate::Lb;
2use crate::model::errors::{LbErrKind, LbResult};
3use crate::model::file::File;
4use crate::model::path_ops::Filter;
5use crate::model::tree_like::TreeLike;
6use uuid::Uuid;
7
8impl Lb {
9 #[instrument(level = "debug", skip(self), err(Debug))]
10 pub async fn create_link_at_path(&self, path: &str, target_id: Uuid) -> LbResult<File> {
11 let mut tx = self.begin_tx().await;
12 let db = tx.db();
13
14 let mut tree = (&db.base_metadata)
15 .to_staged(&mut db.local_metadata)
16 .to_lazy();
17
18 let root = db.root.get().ok_or(LbErrKind::RootNonexistent)?;
19
20 let id = tree.create_link_at_path(path, target_id, root, &self.keychain)?;
21
22 let ui_file = tree.decrypt(&self.keychain, &id, &db.pub_key_lookup)?;
23
24 self.events.meta_changed();
25
26 Ok(ui_file)
27 }
28
29 #[instrument(level = "debug", skip(self), err(Debug))]
30 pub async fn create_at_path(&self, path: &str) -> LbResult<File> {
31 let mut tx = self.begin_tx().await;
32 let db = tx.db();
33
34 let mut tree = (&db.base_metadata)
35 .to_staged(&mut db.local_metadata)
36 .to_lazy();
37
38 let root = db.root.get().ok_or(LbErrKind::RootNonexistent)?;
39
40 let id = tree.create_at_path(path, root, &self.keychain)?;
41
42 let ui_file = tree.decrypt(&self.keychain, &id, &db.pub_key_lookup)?;
43
44 self.events.meta_changed();
45
46 Ok(ui_file)
47 }
48
49 #[instrument(level = "debug", skip(self), err(Debug))]
50 pub async fn get_by_path(&self, path: &str) -> LbResult<File> {
51 let tx = self.ro_tx().await;
52 let db = tx.db();
53
54 let mut tree = (&db.base_metadata).to_staged(&db.local_metadata).to_lazy();
55
56 let root = db.root.get().ok_or(LbErrKind::RootNonexistent)?;
57
58 let id = tree.path_to_id(path, root, &self.keychain)?;
59
60 let ui_file = tree.decrypt(&self.keychain, &id, &db.pub_key_lookup)?;
61
62 Ok(ui_file)
63 }
64
65 #[instrument(level = "debug", skip(self), err(Debug))]
66 pub async fn get_path_by_id(&self, id: Uuid) -> LbResult<String> {
67 let tx = self.ro_tx().await;
68 let db = tx.db();
69
70 let mut tree = (&db.base_metadata).to_staged(&db.local_metadata).to_lazy();
71 let path = tree.id_to_path(&id, &self.keychain)?;
72
73 Ok(path)
74 }
75
76 #[instrument(level = "debug", skip(self), err(Debug))]
77 pub async fn list_paths(&self, filter: Option<Filter>) -> LbResult<Vec<String>> {
78 Ok(self
79 .list_paths_with_ids(filter)
80 .await?
81 .into_iter()
82 .map(|(_, path)| path)
83 .collect())
84 }
85
86 #[instrument(level = "debug", skip(self), err(Debug))]
87 pub async fn list_paths_with_ids(
88 &self, filter: Option<Filter>,
89 ) -> LbResult<Vec<(Uuid, String)>> {
90 let tx = self.ro_tx().await;
91 let db = tx.db();
92
93 let mut tree = (&db.base_metadata).to_staged(&db.local_metadata).to_lazy();
94 let paths = tree.list_paths(filter, &self.keychain)?;
95
96 Ok(paths)
97 }
98}