summaryrefslogtreecommitdiff
path: root/src/piece.rs
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-07-27 17:46:28 +0200
committerJulian T <julian@jtle.dk>2021-07-27 17:46:28 +0200
commitf0648bcc4e7a6b7ce3f59013f25413a667dc507a (patch)
treea204f651e8fabb98953897f4c3b186ac89e57969 /src/piece.rs
parentd7244c2cc7b8f62f2f42cca7f3dece89c30bf105 (diff)
Sort by timestamps and switch to thiserror crateHEADmaster
Diffstat (limited to 'src/piece.rs')
-rw-r--r--src/piece.rs53
1 files changed, 48 insertions, 5 deletions
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<String, PathBuf>,
- info: Option<String>,
+ pub scaled: HashMap<String, PathBuf>,
+ pub info: Option<String>,
}
-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<cmp::Ordering> {
+ Some(self.cmp(other))
+ }
+}
+
+impl cmp::PartialEq for Piece {
+ fn eq(&self, _: &Self) -> bool {
+ false
+ }
+}
+
+impl cmp::Eq for Piece {}