diff --git a/showbits-thermal-printer/src/documents/calendar/mod.rs b/showbits-thermal-printer/src/documents/calendar/mod.rs index 64a425b..e22477a 100644 --- a/showbits-thermal-printer/src/documents/calendar/mod.rs +++ b/showbits-thermal-printer/src/documents/calendar/mod.rs @@ -2,7 +2,7 @@ use axum::{Form, extract::State}; use jiff::Zoned; use serde::{Deserialize, Serialize}; -use crate::server::Server; +use crate::server::{Server, somehow}; #[derive(Serialize)] struct Data { @@ -18,7 +18,7 @@ pub struct FormData { pub feed: Option, } -pub async fn post(server: State, Form(form): Form) { +pub async fn post(server: State, Form(form): Form) -> somehow::Result<()> { let date = Zoned::now().date(); let data = Data { @@ -31,5 +31,5 @@ pub async fn post(server: State, Form(form): Form) { .with_json("/data.json", &data) .with_main_file(include_str!("main.typ")); - server.print_typst(typst).await; + server.print_typst(typst).await } diff --git a/showbits-thermal-printer/src/documents/cells/mod.rs b/showbits-thermal-printer/src/documents/cells/mod.rs index d0e84ed..2dad986 100644 --- a/showbits-thermal-printer/src/documents/cells/mod.rs +++ b/showbits-thermal-printer/src/documents/cells/mod.rs @@ -146,6 +146,5 @@ pub async fn post(server: State, Form(form): Form) -> somehow: .with_file("/image.png", bytes) .with_main_file(include_str!("main.typ")); - server.print_typst(typst).await; - Ok(()) + server.print_typst(typst).await } diff --git a/showbits-thermal-printer/src/documents/chat/mod.rs b/showbits-thermal-printer/src/documents/chat/mod.rs index 0bed793..d48de69 100644 --- a/showbits-thermal-printer/src/documents/chat/mod.rs +++ b/showbits-thermal-printer/src/documents/chat/mod.rs @@ -1,7 +1,7 @@ use axum::{Form, extract::State}; use serde::{Deserialize, Serialize}; -use crate::server::Server; +use crate::server::{Server, somehow}; #[derive(Serialize)] struct Data { @@ -17,7 +17,7 @@ pub struct FormData { pub feed: Option, } -pub async fn post(server: State, Form(form): Form) { +pub async fn post(server: State, Form(form): Form) -> somehow::Result<()> { let data = Data { username: form.username, content: form.content, @@ -28,5 +28,5 @@ pub async fn post(server: State, Form(form): Form) { .with_json("/data.json", &data) .with_main_file(include_str!("main.typ")); - server.print_typst(typst).await; + server.print_typst(typst).await } diff --git a/showbits-thermal-printer/src/documents/egg/mod.rs b/showbits-thermal-printer/src/documents/egg/mod.rs index 68f1bc7..be8c094 100644 --- a/showbits-thermal-printer/src/documents/egg/mod.rs +++ b/showbits-thermal-printer/src/documents/egg/mod.rs @@ -1,7 +1,7 @@ use axum::{Form, extract::State}; use serde::{Deserialize, Serialize}; -use crate::server::Server; +use crate::server::{Server, somehow}; #[derive(Serialize)] struct Data { @@ -21,7 +21,7 @@ pub struct FormData { pub feed: Option, } -pub async fn post(server: State, Form(form): Form) { +pub async fn post(server: State, Form(form): Form) -> somehow::Result<()> { let seed = form.seed.unwrap_or_else(rand::random); let data = Data { @@ -54,5 +54,5 @@ pub async fn post(server: State, Form(form): Form) { typst.add_file(format!("/egg_bad/pattern_{i:02}.png"), *pattern); } - server.print_typst(typst).await; + server.print_typst(typst).await } diff --git a/showbits-thermal-printer/src/documents/image/mod.rs b/showbits-thermal-printer/src/documents/image/mod.rs index 6c4b56d..db1bc44 100644 --- a/showbits-thermal-printer/src/documents/image/mod.rs +++ b/showbits-thermal-printer/src/documents/image/mod.rs @@ -76,6 +76,6 @@ pub async fn post(server: State, mut multipart: Multipart) -> somehow::R .with_file("/image.png", bytes) .with_main_file(include_str!("main.typ")); - server.print_typst(typst).await; + server.print_typst(typst).await?; Ok(().into_response()) } diff --git a/showbits-thermal-printer/src/documents/text/mod.rs b/showbits-thermal-printer/src/documents/text/mod.rs index c59ba79..e3b33a6 100644 --- a/showbits-thermal-printer/src/documents/text/mod.rs +++ b/showbits-thermal-printer/src/documents/text/mod.rs @@ -1,7 +1,7 @@ use axum::{Form, extract::State}; use serde::{Deserialize, Serialize}; -use crate::server::Server; +use crate::server::{Server, somehow}; #[derive(Serialize)] struct Data { @@ -17,7 +17,7 @@ pub struct FormData { pub feed: Option, } -pub async fn post(server: State, Form(form): Form) { +pub async fn post(server: State, Form(form): Form) -> somehow::Result<()> { let data = Data { text: form.text, force_wrap: form.force_wrap.unwrap_or(false), @@ -28,5 +28,5 @@ pub async fn post(server: State, Form(form): Form) { .with_json("/data.json", &data) .with_main_file(include_str!("main.typ")); - server.print_typst(typst).await; + server.print_typst(typst).await } diff --git a/showbits-thermal-printer/src/documents/tictactoe/mod.rs b/showbits-thermal-printer/src/documents/tictactoe/mod.rs index 83db6f4..cc4e2af 100644 --- a/showbits-thermal-printer/src/documents/tictactoe/mod.rs +++ b/showbits-thermal-printer/src/documents/tictactoe/mod.rs @@ -22,6 +22,5 @@ pub async fn post(server: State, Form(form): Form) -> somehow: .with_json("/data.json", &data) .with_main_file(include_str!("main.typ")); - server.print_typst(typst).await; - Ok(()) + server.print_typst(typst).await } diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 46e9c64..5d3599c 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -1,11 +1,11 @@ use showbits_typst::Typst; -use tokio::sync::mpsc; +use tokio::sync::{mpsc, oneshot}; use crate::persistent_printer::PersistentPrinter; pub enum Command { Backlog, - Typst(Typst), + Typst(Typst, oneshot::Sender>), } pub struct Drawer { @@ -30,11 +30,16 @@ impl Drawer { Command::Backlog => { self.printer.print_backlog()?; } - Command::Typst(typst) => { - let image = typst.render()?; - self.printer.print_image(&image)?; + Command::Typst(typst, tx) => { + let _ = tx.send(self.run_cmd_typst(typst)); } } Ok(()) } + + fn run_cmd_typst(&mut self, typst: Typst) -> anyhow::Result<()> { + let image = typst.render()?; + self.printer.print_image(&image)?; + Ok(()) + } } diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index 45203f0..8eb46f7 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -8,7 +8,10 @@ use axum::{ routing::{get, post}, }; use showbits_typst::Typst; -use tokio::{net::TcpListener, sync::mpsc}; +use tokio::{ + net::TcpListener, + sync::{mpsc, oneshot}, +}; use crate::{documents, drawer::Command}; @@ -18,8 +21,10 @@ pub struct Server { } impl Server { - pub async fn print_typst(&self, typst: Typst) { - let _ = self.tx.send(Command::Typst(typst)).await; + pub async fn print_typst(&self, typst: Typst) -> somehow::Result<()> { + let (tx, rx) = oneshot::channel(); + let _ = self.tx.send(Command::Typst(typst, tx)).await; + rx.await?.map_err(somehow::Error) } }