summaryrefslogtreecommitdiff
path: root/src/piece.rs
blob: 2c0b5450c6431907c85d8183753ccb548abd46b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::path::PathBuf;
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_path: PathBuf,
    //thumb_path: PathBuf,
    info: Option<String>,
}

pub fn create_index(ctx: &Context, pieces: &[Piece]) -> Result<(), tera::Error> {
    let mut web_ctx = WebContext::new();
    web_ctx.insert("pieces", pieces);

    println!("render: {}", ctx.tmpl.render("index.html", &web_ctx)?);
    Ok(())
}

impl Piece {
    pub fn new(ctx: &Context, pic: Picture) -> Result<Self, ConversionError> {
        println!("Creating piece from file {}", pic.path.display());
        let mut conv = pic.convert()?;
        let scaled_path = conv.get_size(ctx, ctx.options.size_scaled)?;
        //let thumb_path = conv.get_size(ctx, ctx.options.size_thumb)?;

        let info: Option<String> = 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_path, info,
        })
    }
}