use std::path::PathBuf; use std::collections::HashMap; use crate::picture::{Picture, ConversionError}; use crate::context::Context; use tera::Context as WebContext; use serde::Serialize; #[derive(Debug, Serialize)] pub struct Piece { pic: Picture, scaled: HashMap, info: Option, } pub fn create_index(ctx: &Context, pieces: &[Piece]) -> Result<(), tera::Error> { let mut web_ctx = WebContext::new(); web_ctx.insert("pieces", pieces); web_ctx.insert("opts", &ctx.options); println!("render: {}", ctx.tmpl.render("index.html", &web_ctx)?); Ok(()) } impl Piece { pub fn new(ctx: &Context, pic: Picture) -> Result { println!("Creating piece from file {}", pic.path.display()); let mut conv = pic.convert()?; let mut scaled = HashMap::with_capacity(ctx.config.sizes.capacity()); // Create all needed sizes for (id, size) in ctx.config.sizes.iter() { scaled.insert(id.clone(), conv.get_size(ctx, *size)?); } let info: Option = pic.path.file_name() .map(|name| name.to_str()).flatten() .map(|name| ctx.config.info.get(name).map(|s| s.clone())).flatten(); Ok(Piece { pic, scaled, info, }) } }