From e605ba84e52509cfec9ecb7599bb3833c1e7f8e8 Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Sun, 4 May 2025 13:01:34 -0700 Subject: [PATCH] bit of work toward generate() --- src/generate.rs | 24 +++++++++++++-- src/generate/album_dir.rs | 63 ++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/src/generate.rs b/src/generate.rs index fa7562c..614234b 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -1,10 +1,28 @@ mod album_dir; use album_dir::AlbumDir; -use std::io; +use std::env; use std::path::{Path, PathBuf}; -pub fn generate(root_path: &PathBuf) -> Result<(), io::Error> { - let _ = AlbumDir::try_from(root_path)?; +const OUTPUT_PATH: &'static str = "site"; + +pub fn generate(root_path: &PathBuf) -> anyhow::Result<()> { + let orig_path = env::current_dir()?; + let album = AlbumDir::try_from(root_path)?; + env::set_current_dir(&root_path)?; + + generate_images(&album)?; + generate_html(&album)?; + + env::set_current_dir(orig_path)?; + Ok(()) +} + +fn generate_images(album: &AlbumDir) -> anyhow::Result<()> { + let output_path = album.path.join(OUTPUT_PATH); + Ok(()) +} + +fn generate_html(album: &AlbumDir) -> anyhow::Result<()> { Ok(()) } diff --git a/src/generate/album_dir.rs b/src/generate/album_dir.rs index 7dc08dc..a4dac5a 100644 --- a/src/generate/album_dir.rs +++ b/src/generate/album_dir.rs @@ -1,19 +1,19 @@ use image::ImageReader; use std::ffi::OsString; -use std::io; +use std::fs; use std::path::PathBuf; use std::slice::Iter; /// An album directory, which has images and possibly child albums #[derive(Clone)] pub struct AlbumDir { - path: PathBuf, - images: Vec, + pub path: PathBuf, + pub images: Vec, // TOOD: Remove the parent reference? Causes a lot of issues // parent: Option>, - children: Vec, + pub children: Vec, - description: String, + pub description: String, } impl AlbumDir { @@ -24,9 +24,9 @@ impl AlbumDir { } impl TryFrom<&PathBuf> for AlbumDir { - type Error = io::Error; + type Error = anyhow::Error; - fn try_from(p: &PathBuf) -> io::Result { + fn try_from(p: &PathBuf) -> anyhow::Result { let mut images = vec![]; let mut children = vec![]; let mut description = "".to_string(); @@ -38,20 +38,27 @@ impl TryFrom<&PathBuf> for AlbumDir { println!("Found file: {}", entry_path.display()); if let Some(filename) = entry_path.file_name() { if filename == "description.txt" { - todo!(); - // description = String::from_utf8(fs::read(entry_path)?)?; + description = fs::read_to_string(entry_path)?; } else if filename == "description.md" { + let _conents = fs::read_to_string(entry_path)?; + // TODO: render markdown todo!(); } else { let reader = ImageReader::open(&entry_path)?.with_guessed_format()?; if reader.format().is_some() { // Found an image - // TODO: If image filename but with .md or .txt exists, read that in as - // the image description OR make this part of Image::from - todo!(); - let description = String::new(); + let mut description = String::new(); + + // Read in any associated description file + if entry_path.with_extension(".txt").exists() { + description = fs::read_to_string(&entry_path)?; + } else if entry_path.with_extension(".md").exists() { + let _contents = fs::read(entry_path)?; + // TODO: render markdown + todo!(); + } images.push(Image { - filename: filename.to_os_string(), + path: entry_path, description, }); } @@ -128,7 +135,7 @@ impl<'a> Iterator for AlbumIter<'a> { #[derive(Clone, Hash, PartialEq, Eq)] struct Image { - filename: OsString, + path: PathBuf, description: String, } @@ -144,11 +151,11 @@ mod tests { description: "".to_string(), images: vec![ Image { - filename: "foo".into(), + path: "foo".into(), description: "".to_string(), }, Image { - filename: "bar".into(), + path: "bar".into(), description: "".to_string(), }, ], @@ -160,19 +167,19 @@ mod tests { description: "".to_string(), images: vec![ Image { - filename: "subdir/foo".into(), + path: "subdir/foo".into(), description: "".to_string(), }, Image { - filename: "subdir/bar".into(), + path: "subdir/bar".into(), description: "".to_string(), }, ], children: vec![AlbumDir { - path: "deeper_subdir".into(), + path: "subdir/deeper_subdir".into(), description: "".to_string(), images: vec![Image { - filename: "deeper_subdir/image.jpg".into(), + path: "subdir/deeper_subdir/image.jpg".into(), description: "".to_string(), }], children: vec![], @@ -186,20 +193,14 @@ mod tests { children: vec![], }); - let imgs: HashSet = ad - .iter() - .map(|i| i.filename.to_str().unwrap().to_string()) - .collect(); - let expected: HashSet = [ + let imgs: HashSet<&str> = ad.iter().map(|i| i.path.to_str().unwrap()).collect(); + let expected: HashSet<&str> = HashSet::from([ "foo", "bar", "subdir/foo", "subdir/bar", - "deeper_subdir/image.jpg", - ] - .iter() - .map(|s| s.to_string()) - .collect(); + "subdir/deeper_subdir/image.jpg", + ]); assert_eq!(imgs, expected); } }