From f0648bcc4e7a6b7ce3f59013f25413a667dc507a Mon Sep 17 00:00:00 2001 From: Julian T Date: Tue, 27 Jul 2021 17:46:28 +0200 Subject: Sort by timestamps and switch to thiserror crate --- src/piece.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) (limited to 'src/piece.rs') diff --git a/src/piece.rs b/src/piece.rs index 1ebad66..82d0313 100644 --- a/src/piece.rs +++ b/src/piece.rs @@ -1,24 +1,47 @@ use std::path::PathBuf; use std::collections::HashMap; +use std::fs; +use std::io; +use std::cmp; use crate::picture::{Picture, ConversionError}; use crate::context::Context; use tera::Context as WebContext; use serde::Serialize; +#[derive(Debug, thiserror::Error)] +pub enum RenderError { + #[error("saving to file")] + Io { + #[from] + source: io::Error, + }, + #[error("rendering template")] + Templating { + #[from] + source: tera::Error, + }, +} + #[derive(Debug, Serialize)] pub struct Piece { - pic: Picture, + pub pic: Picture, - scaled: HashMap, - info: Option, + pub scaled: HashMap, + pub info: Option, } -pub fn create_index(ctx: &Context, pieces: &[Piece]) -> Result<(), tera::Error> { +pub fn create_index(ctx: &Context, pieces: &[Piece]) -> Result<(), RenderError> { 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)?); + println!("Rendering index template"); + + // Open file for writing and render template + let mut wr = fs::File::create(ctx.options.builddir.join("index.html"))?; + let mut wr = io::BufWriter::new(&mut wr); + + ctx.tmpl.render_to("index.html", &web_ctx, &mut wr)?; Ok(()) } @@ -42,3 +65,23 @@ impl Piece { }) } } + +impl cmp::Ord for Piece { + fn cmp(&self, other: &Self) -> cmp::Ordering { + self.pic.taken_chrono.cmp(&other.pic.taken_chrono) + } +} + +impl cmp::PartialOrd for Piece { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl cmp::PartialEq for Piece { + fn eq(&self, _: &Self) -> bool { + false + } +} + +impl cmp::Eq for Piece {} -- cgit v1.2.3