aboutsummaryrefslogtreecommitdiff
path: root/src/library.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-01-10 22:55:04 +0100
committermetamuffin <metamuffin@disroot.org>2023-01-10 22:55:04 +0100
commit552a3eb82347ee051855016f51ec452906cdc4d6 (patch)
tree18f2f1a53d7edf61a92f83d44893039009094064 /src/library.rs
parent80e668f9cda3eb7972198dbbc65f6f72eae43c99 (diff)
downloadjellything-552a3eb82347ee051855016f51ec452906cdc4d6.tar
jellything-552a3eb82347ee051855016f51ec452906cdc4d6.tar.bz2
jellything-552a3eb82347ee051855016f51ec452906cdc4d6.tar.zst
the rocket is launching! :rocket: :rocket:
Diffstat (limited to 'src/library.rs')
-rw-r--r--src/library.rs49
1 files changed, 37 insertions, 12 deletions
diff --git a/src/library.rs b/src/library.rs
index ea0b9ed..be75f10 100644
--- a/src/library.rs
+++ b/src/library.rs
@@ -14,6 +14,7 @@ pub enum Node {
#[derive(Debug, Clone)]
pub struct Directory {
+ pub lib_path: PathBuf,
pub identifier: String,
pub data: DirectoryInfo,
pub children: Vec<Arc<Node>>,
@@ -21,6 +22,7 @@ pub struct Directory {
#[derive(Debug, Clone)]
pub struct Item {
+ pub lib_path: PathBuf,
pub identifier: String,
pub data: ItemInfo,
}
@@ -28,7 +30,7 @@ pub struct Item {
impl Library {
pub fn open(path: &str) -> anyhow::Result<Self> {
Ok(Self {
- root: Node::from_path(path.into()).context("indexing root")?,
+ root: Node::from_path(path.into(), PathBuf::new(), true).context("indexing root")?,
})
}
pub fn nested(&self, path: &str) -> anyhow::Result<Arc<Node>> {
@@ -65,23 +67,37 @@ impl Node {
Node::Item(i) => &i.identifier,
}
}
- pub fn from_path(path: PathBuf) -> anyhow::Result<Arc<Node>> {
+ pub fn from_path(
+ path: PathBuf,
+ mut lib_path: PathBuf,
+ root: bool,
+ ) -> anyhow::Result<Arc<Node>> {
if path.is_dir() {
let mpath = path.join("directory.json");
let data: DirectoryInfo =
serde_json::from_reader(File::open(mpath).context("metadata missing")?)?;
+
+ let identifier = path.file_name().unwrap().to_str().unwrap().to_string();
+ if !root {
+ lib_path = lib_path.join(identifier.clone());
+ }
+
let children = path
.read_dir()?
.map(|e| e.unwrap().path())
.filter(|e| e.extension() != Some(OsStr::new("json")))
- .map(|e| Node::from_path(e.clone()).context(format!("loading {e:?}")))
+ .map(|e| {
+ Node::from_path(e.clone(), lib_path.clone(), false)
+ .context(format!("loading {e:?}"))
+ })
.into_iter()
.collect::<anyhow::Result<Vec<_>>>()?;
Ok(Node::Directory(Arc::new(Directory {
+ lib_path,
children,
data,
- identifier: path.file_name().unwrap().to_str().unwrap().to_string(),
+ identifier,
}))
.into())
} else if path.is_file() {
@@ -89,15 +105,17 @@ impl Node {
let datafile = File::open(mpath.clone())
.context(format!("metadata missing, tried path {mpath:?}"))?;
let data: ItemInfo = serde_json::from_reader(datafile).context("invalid metadata")?;
+ let identifier = path
+ .with_extension("")
+ .file_name()
+ .unwrap()
+ .to_str()
+ .unwrap()
+ .to_string();
Ok(Node::Item(Arc::new(Item {
+ lib_path: lib_path.join(identifier.clone()),
data,
- identifier: path
- .with_extension("")
- .file_name()
- .unwrap()
- .to_str()
- .unwrap()
- .to_string(),
+ identifier,
}))
.into())
} else {
@@ -105,8 +123,15 @@ impl Node {
}
}
}
-
+impl Item {
+ pub fn path(&self) -> String {
+ self.lib_path.to_str().unwrap().to_string()
+ }
+}
impl Directory {
+ pub fn path(&self) -> String {
+ self.lib_path.to_str().unwrap().to_string()
+ }
pub fn child_by_ident(&self, i: &str) -> Option<Arc<Node>> {
self.children
.iter()