From 24c84801f1e2e5a55c56fb9ad2c04750e981f736 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 1 Mar 2025 20:26:16 +0100 Subject: [PATCH] Make new endpoints more consistent --- .../src/documents/image/data.json | 6 ++-- .../src/documents/image/mod.rs | 26 ++++++++--------- .../src/documents/text/mod.rs | 29 +++++++++++++------ 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/showbits-thermal-printer/src/documents/image/data.json b/showbits-thermal-printer/src/documents/image/data.json index b0e3685..a824ec5 100644 --- a/showbits-thermal-printer/src/documents/image/data.json +++ b/showbits-thermal-printer/src/documents/image/data.json @@ -1,6 +1,6 @@ { - "seamless": true, - "feed": true, + "algo": "floyd-steinberg", "bright": true, - "algo": "floyd-steinberg" + "seamless": true, + "feed": true } diff --git a/showbits-thermal-printer/src/documents/image/mod.rs b/showbits-thermal-printer/src/documents/image/mod.rs index 006aa8b..bcd9ab6 100644 --- a/showbits-thermal-printer/src/documents/image/mod.rs +++ b/showbits-thermal-printer/src/documents/image/mod.rs @@ -15,20 +15,20 @@ use crate::{ }; #[derive(Serialize)] -pub struct Data { - pub seamless: bool, - pub feed: bool, - pub bright: bool, - pub algo: String, +struct Data { + algo: String, + bright: bool, + seamless: bool, + feed: bool, } pub async fn post(server: State, mut multipart: Multipart) -> somehow::Result { let mut image = None; let mut data = Data { + algo: "stucki".to_string(), + bright: true, seamless: false, feed: true, - bright: true, - algo: "floyd-steinberg".to_string(), }; while let Some(field) = multipart.next_field().await? { @@ -38,18 +38,18 @@ pub async fn post(server: State, mut multipart: Multipart) -> somehow::R let decoded = image::load_from_memory(&data)?.into_rgba8(); image = Some(decoded); } + Some("algo") => { + data.algo = field.text().await?; + } + Some("bright") => { + data.bright = !field.text().await?.is_empty(); + } Some("seamless") => { data.seamless = !field.text().await?.is_empty(); } Some("feed") => { data.feed = !field.text().await?.is_empty(); } - Some("bright") => { - data.bright = !field.text().await?.is_empty(); - } - Some("algo") => { - data.algo = field.text().await?; - } _ => {} } } diff --git a/showbits-thermal-printer/src/documents/text/mod.rs b/showbits-thermal-printer/src/documents/text/mod.rs index febe571..f85a91a 100644 --- a/showbits-thermal-printer/src/documents/text/mod.rs +++ b/showbits-thermal-printer/src/documents/text/mod.rs @@ -6,18 +6,29 @@ use crate::{ server::Server, }; -#[derive(Serialize, Deserialize)] -pub struct Data { - pub text: String, - #[serde(default)] - pub force_wrap: bool, - #[serde(default)] - pub feed: bool, +#[derive(Serialize)] +struct Data { + text: String, + force_wrap: bool, + feed: bool, } -pub async fn post(server: State, request: Form) { +#[derive(Deserialize)] +pub struct FormData { + pub text: String, + pub force_wrap: Option, + pub feed: Option, +} + +pub async fn post(server: State, Form(form): Form) { + let data = Data { + text: form.text, + force_wrap: form.force_wrap.unwrap_or(false), + feed: form.feed.unwrap_or(true), + }; + let typst = super::typst_with_lib() - .with_json("/data.json", &request.0) + .with_json("/data.json", &data) .with_main_file(include_str!("main.typ")); let _ = server