bit of work toward generate()

This commit is contained in:
Nick Pegg 2025-05-04 13:01:34 -07:00
parent 36abf3ecc5
commit e605ba84e5
2 changed files with 53 additions and 34 deletions

View file

@ -1,10 +1,28 @@
mod album_dir; mod album_dir;
use album_dir::AlbumDir; use album_dir::AlbumDir;
use std::io; use std::env;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
pub fn generate(root_path: &PathBuf) -> Result<(), io::Error> { const OUTPUT_PATH: &'static str = "site";
let _ = AlbumDir::try_from(root_path)?;
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(()) Ok(())
} }

View file

@ -1,19 +1,19 @@
use image::ImageReader; use image::ImageReader;
use std::ffi::OsString; use std::ffi::OsString;
use std::io; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::slice::Iter; use std::slice::Iter;
/// An album directory, which has images and possibly child albums /// An album directory, which has images and possibly child albums
#[derive(Clone)] #[derive(Clone)]
pub struct AlbumDir { pub struct AlbumDir {
path: PathBuf, pub path: PathBuf,
images: Vec<Image>, pub images: Vec<Image>,
// TOOD: Remove the parent reference? Causes a lot of issues // TOOD: Remove the parent reference? Causes a lot of issues
// parent: Option<Box<&'a AlbumDir>>, // parent: Option<Box<&'a AlbumDir>>,
children: Vec<AlbumDir>, pub children: Vec<AlbumDir>,
description: String, pub description: String,
} }
impl AlbumDir { impl AlbumDir {
@ -24,9 +24,9 @@ impl AlbumDir {
} }
impl TryFrom<&PathBuf> for AlbumDir { impl TryFrom<&PathBuf> for AlbumDir {
type Error = io::Error; type Error = anyhow::Error;
fn try_from(p: &PathBuf) -> io::Result<AlbumDir> { fn try_from(p: &PathBuf) -> anyhow::Result<AlbumDir> {
let mut images = vec![]; let mut images = vec![];
let mut children = vec![]; let mut children = vec![];
let mut description = "".to_string(); let mut description = "".to_string();
@ -38,20 +38,27 @@ impl TryFrom<&PathBuf> for AlbumDir {
println!("Found file: {}", entry_path.display()); println!("Found file: {}", entry_path.display());
if let Some(filename) = entry_path.file_name() { if let Some(filename) = entry_path.file_name() {
if filename == "description.txt" { if filename == "description.txt" {
todo!(); description = fs::read_to_string(entry_path)?;
// description = String::from_utf8(fs::read(entry_path)?)?;
} else if filename == "description.md" { } else if filename == "description.md" {
let _conents = fs::read_to_string(entry_path)?;
// TODO: render markdown
todo!(); todo!();
} else { } else {
let reader = ImageReader::open(&entry_path)?.with_guessed_format()?; let reader = ImageReader::open(&entry_path)?.with_guessed_format()?;
if reader.format().is_some() { if reader.format().is_some() {
// Found an image // Found an image
// TODO: If image filename but with .md or .txt exists, read that in as let mut description = String::new();
// the image description OR make this part of Image::from<Path>
todo!(); // Read in any associated description file
let description = String::new(); 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 { images.push(Image {
filename: filename.to_os_string(), path: entry_path,
description, description,
}); });
} }
@ -128,7 +135,7 @@ impl<'a> Iterator for AlbumIter<'a> {
#[derive(Clone, Hash, PartialEq, Eq)] #[derive(Clone, Hash, PartialEq, Eq)]
struct Image { struct Image {
filename: OsString, path: PathBuf,
description: String, description: String,
} }
@ -144,11 +151,11 @@ mod tests {
description: "".to_string(), description: "".to_string(),
images: vec![ images: vec![
Image { Image {
filename: "foo".into(), path: "foo".into(),
description: "".to_string(), description: "".to_string(),
}, },
Image { Image {
filename: "bar".into(), path: "bar".into(),
description: "".to_string(), description: "".to_string(),
}, },
], ],
@ -160,19 +167,19 @@ mod tests {
description: "".to_string(), description: "".to_string(),
images: vec![ images: vec![
Image { Image {
filename: "subdir/foo".into(), path: "subdir/foo".into(),
description: "".to_string(), description: "".to_string(),
}, },
Image { Image {
filename: "subdir/bar".into(), path: "subdir/bar".into(),
description: "".to_string(), description: "".to_string(),
}, },
], ],
children: vec![AlbumDir { children: vec![AlbumDir {
path: "deeper_subdir".into(), path: "subdir/deeper_subdir".into(),
description: "".to_string(), description: "".to_string(),
images: vec![Image { images: vec![Image {
filename: "deeper_subdir/image.jpg".into(), path: "subdir/deeper_subdir/image.jpg".into(),
description: "".to_string(), description: "".to_string(),
}], }],
children: vec![], children: vec![],
@ -186,20 +193,14 @@ mod tests {
children: vec![], children: vec![],
}); });
let imgs: HashSet<String> = ad let imgs: HashSet<&str> = ad.iter().map(|i| i.path.to_str().unwrap()).collect();
.iter() let expected: HashSet<&str> = HashSet::from([
.map(|i| i.filename.to_str().unwrap().to_string())
.collect();
let expected: HashSet<String> = [
"foo", "foo",
"bar", "bar",
"subdir/foo", "subdir/foo",
"subdir/bar", "subdir/bar",
"deeper_subdir/image.jpg", "subdir/deeper_subdir/image.jpg",
] ]);
.iter()
.map(|s| s.to_string())
.collect();
assert_eq!(imgs, expected); assert_eq!(imgs, expected);
} }
} }