diff options
author | metamuffin <metamuffin@disroot.org> | 2023-01-10 09:01:48 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-01-10 09:01:48 +0100 |
commit | 1c97c3785047d50fd5500f99457a254e0f95aefe (patch) | |
tree | a27d997ad42681946e3234fb114cf7cfc5eb815b /src/library.rs | |
parent | 0c30e065b678d41d8932b3bf0926608cfa15a7ac (diff) | |
download | jellything-1c97c3785047d50fd5500f99457a254e0f95aefe.tar jellything-1c97c3785047d50fd5500f99457a254e0f95aefe.tar.bz2 jellything-1c97c3785047d50fd5500f99457a254e0f95aefe.tar.zst |
listing and style improved
Diffstat (limited to 'src/library.rs')
-rw-r--r-- | src/library.rs | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/src/library.rs b/src/library.rs index e4c7fe6..d650733 100644 --- a/src/library.rs +++ b/src/library.rs @@ -18,7 +18,7 @@ pub enum LibNode { #[derive(Debug, Clone)] pub struct LibDirectory { pub path: PathBuf, - pub child_nodes: Vec<PathBuf>, + pub child_names: Vec<String>, pub data: LibDirectoryData, } @@ -33,7 +33,9 @@ pub struct LibDirectoryData { } #[derive(Debug, Clone, Deserialize, Serialize)] -pub struct LibItemData {} +pub struct LibItemData { + title: String, +} impl Library { pub fn open(path: &str) -> anyhow::Result<Self> { @@ -56,16 +58,7 @@ impl Library { Ok(n) } } -impl LibDirectory { - pub fn get_child(&self, p: &str) -> anyhow::Result<Arc<LibNode>> { - if p.contains("..") || p.starts_with("/") { - bail!("no! dont do that.") - } - let path = self.path.join(p); - // if !path.exists() {bail!("does not exist");} - LibNode::from_path(path) - } -} + impl LibNode { pub fn get_directory(&self) -> anyhow::Result<&LibDirectory> { match self { @@ -73,24 +66,54 @@ impl LibNode { LibNode::Item(_) => bail!("not a directory"), } } + pub fn title(&self) -> &str { + match self { + LibNode::Directory(d) => &d.data.name, + LibNode::Item(i) => &i.data.title, + } + } pub fn from_path(path: PathBuf) -> anyhow::Result<Arc<LibNode>> { if path.is_dir() { let mpath = path.join("directory.json"); let data: LibDirectoryData = serde_json::from_reader(File::open(mpath).context("metadata missing")?)?; - let child_nodes = path.read_dir()?.map(|e| e.unwrap().path()).collect(); + let child_names = path + .read_dir()? + .map(|e| e.unwrap().file_name().to_str().unwrap().to_string()) + .filter(|e| !e.ends_with(".json")) + .collect(); Ok(LibNode::Directory(Arc::new(LibDirectory { path, - child_nodes, + child_names, data, })) .into()) } else if path.is_file() { - let mpath = path.clone().with_extension(".metadata.json"); - let data: LibItemData = serde_json::from_reader(File::open(mpath)?)?; + let mpath = path.clone().with_extension("metadata.json"); + let data: LibItemData = + serde_json::from_reader(File::open(mpath).context("metadata missing")?) + .context("invalid metadata")?; Ok(LibNode::Item(Arc::new(LibItem { data })).into()) } else { bail!("did somebody really put a fifo or socket in the library?!") } } } + +impl LibDirectory { + pub fn get_child(&self, p: &str) -> anyhow::Result<Arc<LibNode>> { + if p.contains("..") || p.starts_with("/") { + bail!("no! dont do that.") + } + let path = self.path.join(p); + // if !path.exists() {bail!("does not exist");} + LibNode::from_path(path) + } + pub fn child_nodes(&self) -> anyhow::Result<Vec<Arc<LibNode>>> { + let mut o = vec![]; + for name in &self.child_names { + o.push(self.get_child(&name)?) + } + Ok(o) + } +} |