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 | |
parent | 0c30e065b678d41d8932b3bf0926608cfa15a7ac (diff) | |
download | jellything-1c97c3785047d50fd5500f99457a254e0f95aefe.tar jellything-1c97c3785047d50fd5500f99457a254e0f95aefe.tar.bz2 jellything-1c97c3785047d50fd5500f99457a254e0f95aefe.tar.zst |
listing and style improved
-rw-r--r-- | src/frontend/pages/mod.rs | 3 | ||||
-rw-r--r-- | src/frontend/pages/node.rs | 14 | ||||
-rw-r--r-- | src/frontend/style/layout.css | 3 | ||||
-rw-r--r-- | src/library.rs | 55 |
4 files changed, 51 insertions, 24 deletions
diff --git a/src/frontend/pages/mod.rs b/src/frontend/pages/mod.rs index 6e7bd87..6145886 100644 --- a/src/frontend/pages/mod.rs +++ b/src/frontend/pages/mod.rs @@ -1,5 +1,3 @@ -use std::{error::Error, fmt::Display}; - use actix_web::{ body::BoxBody, http::{ @@ -8,6 +6,7 @@ use actix_web::{ }, HttpResponseBuilder, Responder, }; +use std::fmt::Display; pub mod home; pub mod layout; diff --git a/src/frontend/pages/node.rs b/src/frontend/pages/node.rs index c3f7791..b956d3e 100644 --- a/src/frontend/pages/node.rs +++ b/src/frontend/pages/node.rs @@ -1,6 +1,6 @@ use super::layout::Layout; use crate::{ - frontend::pages::{HtmlTemplate, MyError, MyResult}, + frontend::pages::MyResult, library::{LibDirectory, LibItem, LibNode}, AppState, }; @@ -20,10 +20,7 @@ pub async fn page_library_node( let mut out = String::new(); match node.deref() { LibNode::Directory(dir) => Layout { - title: format!( - "{} - Library", - dir.path.file_name().unwrap().to_str().unwrap() - ), + title: format!("{} - Library", node.title()), main: Directory { dir: dir.clone() }, } .render(&mut out)?, @@ -39,6 +36,13 @@ pub async fn page_library_node( markup::define! { Directory(dir: Arc<LibDirectory>) { h1 { @dir.data.name } + ul.directorylisting { + @for el in &dir.child_nodes().unwrap() { + li { + span.title { @el.title() } + } + } + } } Item(item: Arc<LibItem>) { h1 { "thats an item" } diff --git a/src/frontend/style/layout.css b/src/frontend/style/layout.css index d6196b0..dd05e9a 100644 --- a/src/frontend/style/layout.css +++ b/src/frontend/style/layout.css @@ -10,6 +10,7 @@ body { background-color: #1a1a1a; + width: 100vw; } nav { @@ -17,7 +18,7 @@ nav { top: 0px; left: 0px; padding: 1em; - width: 100vw; + width: calc(100vw - 2em); height: 2em; background-color: #41414144; } 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) + } +} |