From ed8b69de13020dc4aed384c41481450b36e5da59 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 26 Jun 2024 08:45:22 +0200 Subject: [PATCH] do not crash if there are no items to paginate --- Cargo.lock | 2 +- little-hesinde/Cargo.toml | 2 +- little-hesinde/src/handlers/paginated.rs | 23 +++++++++++++---------- little-hesinde/src/lib.rs | 2 +- little-hesinde/src/templates.rs | 1 + little-hesinde/templates/empty.html | 6 ++++++ 6 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 little-hesinde/templates/empty.html diff --git a/Cargo.lock b/Cargo.lock index cb95472..59e23ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -889,7 +889,7 @@ dependencies = [ [[package]] name = "little-hesinde" -version = "0.1.4" +version = "0.1.5" dependencies = [ "calibre-db", "clap", diff --git a/little-hesinde/Cargo.toml b/little-hesinde/Cargo.toml index ce859cc..80c72c8 100644 --- a/little-hesinde/Cargo.toml +++ b/little-hesinde/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "little-hesinde" -version = "0.1.4" +version = "0.1.5" edition = "2021" license = { workspace = true } authors = { workspace = true } diff --git a/little-hesinde/src/handlers/paginated.rs b/little-hesinde/src/handlers/paginated.rs index 76a92af..deb972e 100644 --- a/little-hesinde/src/handlers/paginated.rs +++ b/little-hesinde/src/handlers/paginated.rs @@ -1,16 +1,13 @@ //! Deal with cursor pagination. -use std::fmt::Debug; - +use super::error::HandlerError; +use crate::templates::TEMPLATES; use calibre_db::data::error::DataStoreError; use poem::{error::InternalServerError, web::Html, IntoResponse, Response}; use serde::Serialize; +use std::fmt::Debug; use tera::Context; -use crate::templates::TEMPLATES; - -use super::error::HandlerError; - /// Render a tera template with paginated items and generate back and forth links. pub fn render( template: &str, @@ -25,11 +22,18 @@ where P: Fn(&str) -> Result, M: Fn(&str) -> Result, { - let items = fetcher().map_err(HandlerError::DataError)?; - let mut context = Context::new(); + context.insert("nav", template); + + let items = fetcher().map_err(HandlerError::DataError)?; + if items.is_empty() { + return Ok(TEMPLATES + .render("empty", &context) + .map_err(InternalServerError) + .map(Html)? + .into_response()); + } - // fails already in the sql query if there is nothing returned let first_item = items.first().unwrap(); let last_item = items.last().unwrap(); @@ -42,7 +46,6 @@ where context.insert("has_more", &has_more); context.insert("backward_cursor", &backward_cursor); context.insert("forward_cursor", &forward_cursor); - context.insert("nav", template); context.insert(template, &items); Ok(TEMPLATES diff --git a/little-hesinde/src/lib.rs b/little-hesinde/src/lib.rs index 855d0ce..87a7fe4 100644 --- a/little-hesinde/src/lib.rs +++ b/little-hesinde/src/lib.rs @@ -71,7 +71,7 @@ pub mod opds { pub mod templates; pub const APP_NAME: &str = "little-hesinde"; -pub const VERSION: &str = "0.1.4"; +pub const VERSION: &str = "0.1.5"; /// Internal marker data in lieu of a proper `Accept` header. #[derive(Debug, Clone, Copy)] diff --git a/little-hesinde/src/templates.rs b/little-hesinde/src/templates.rs index c0c7cd8..b92efdd 100644 --- a/little-hesinde/src/templates.rs +++ b/little-hesinde/src/templates.rs @@ -13,6 +13,7 @@ pub static TEMPLATES: Lazy = Lazy::new(|| { ("book_list", include_str!("../templates/book_list.html")), ("books", include_str!("../templates/books.html")), ("series", include_str!("../templates/series.html")), + ("empty", include_str!("../templates/empty.html")), ]) .expect("failed to parse tera templates"); diff --git a/little-hesinde/templates/empty.html b/little-hesinde/templates/empty.html new file mode 100644 index 0000000..14eef05 --- /dev/null +++ b/little-hesinde/templates/empty.html @@ -0,0 +1,6 @@ +{% extends "base" %} +{% block title %} +{% endblock title %} +{% block content %} +

No items

+{% endblock content %}