Show rendering errors to user

This also delays the response to the HTTP request until the printer has
printed whatever it should.
This commit is contained in:
Joscha 2025-03-03 19:57:32 +01:00
parent 14a0596a25
commit aaa1a916da
9 changed files with 33 additions and 25 deletions

View file

@ -2,7 +2,7 @@ use axum::{Form, extract::State};
use jiff::Zoned; use jiff::Zoned;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::server::Server; use crate::server::{Server, somehow};
#[derive(Serialize)] #[derive(Serialize)]
struct Data { struct Data {
@ -18,7 +18,7 @@ pub struct FormData {
pub feed: Option<bool>, pub feed: Option<bool>,
} }
pub async fn post(server: State<Server>, Form(form): Form<FormData>) { pub async fn post(server: State<Server>, Form(form): Form<FormData>) -> somehow::Result<()> {
let date = Zoned::now().date(); let date = Zoned::now().date();
let data = Data { let data = Data {
@ -31,5 +31,5 @@ pub async fn post(server: State<Server>, Form(form): Form<FormData>) {
.with_json("/data.json", &data) .with_json("/data.json", &data)
.with_main_file(include_str!("main.typ")); .with_main_file(include_str!("main.typ"));
server.print_typst(typst).await; server.print_typst(typst).await
} }

View file

@ -146,6 +146,5 @@ pub async fn post(server: State<Server>, Form(form): Form<FormData>) -> somehow:
.with_file("/image.png", bytes) .with_file("/image.png", bytes)
.with_main_file(include_str!("main.typ")); .with_main_file(include_str!("main.typ"));
server.print_typst(typst).await; server.print_typst(typst).await
Ok(())
} }

View file

@ -1,7 +1,7 @@
use axum::{Form, extract::State}; use axum::{Form, extract::State};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::server::Server; use crate::server::{Server, somehow};
#[derive(Serialize)] #[derive(Serialize)]
struct Data { struct Data {
@ -17,7 +17,7 @@ pub struct FormData {
pub feed: Option<bool>, pub feed: Option<bool>,
} }
pub async fn post(server: State<Server>, Form(form): Form<FormData>) { pub async fn post(server: State<Server>, Form(form): Form<FormData>) -> somehow::Result<()> {
let data = Data { let data = Data {
username: form.username, username: form.username,
content: form.content, content: form.content,
@ -28,5 +28,5 @@ pub async fn post(server: State<Server>, Form(form): Form<FormData>) {
.with_json("/data.json", &data) .with_json("/data.json", &data)
.with_main_file(include_str!("main.typ")); .with_main_file(include_str!("main.typ"));
server.print_typst(typst).await; server.print_typst(typst).await
} }

View file

@ -1,7 +1,7 @@
use axum::{Form, extract::State}; use axum::{Form, extract::State};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::server::Server; use crate::server::{Server, somehow};
#[derive(Serialize)] #[derive(Serialize)]
struct Data { struct Data {
@ -21,7 +21,7 @@ pub struct FormData {
pub feed: Option<bool>, pub feed: Option<bool>,
} }
pub async fn post(server: State<Server>, Form(form): Form<FormData>) { pub async fn post(server: State<Server>, Form(form): Form<FormData>) -> somehow::Result<()> {
let seed = form.seed.unwrap_or_else(rand::random); let seed = form.seed.unwrap_or_else(rand::random);
let data = Data { let data = Data {
@ -54,5 +54,5 @@ pub async fn post(server: State<Server>, Form(form): Form<FormData>) {
typst.add_file(format!("/egg_bad/pattern_{i:02}.png"), *pattern); typst.add_file(format!("/egg_bad/pattern_{i:02}.png"), *pattern);
} }
server.print_typst(typst).await; server.print_typst(typst).await
} }

View file

@ -76,6 +76,6 @@ pub async fn post(server: State<Server>, mut multipart: Multipart) -> somehow::R
.with_file("/image.png", bytes) .with_file("/image.png", bytes)
.with_main_file(include_str!("main.typ")); .with_main_file(include_str!("main.typ"));
server.print_typst(typst).await; server.print_typst(typst).await?;
Ok(().into_response()) Ok(().into_response())
} }

View file

@ -1,7 +1,7 @@
use axum::{Form, extract::State}; use axum::{Form, extract::State};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::server::Server; use crate::server::{Server, somehow};
#[derive(Serialize)] #[derive(Serialize)]
struct Data { struct Data {
@ -17,7 +17,7 @@ pub struct FormData {
pub feed: Option<bool>, pub feed: Option<bool>,
} }
pub async fn post(server: State<Server>, Form(form): Form<FormData>) { pub async fn post(server: State<Server>, Form(form): Form<FormData>) -> somehow::Result<()> {
let data = Data { let data = Data {
text: form.text, text: form.text,
force_wrap: form.force_wrap.unwrap_or(false), force_wrap: form.force_wrap.unwrap_or(false),
@ -28,5 +28,5 @@ pub async fn post(server: State<Server>, Form(form): Form<FormData>) {
.with_json("/data.json", &data) .with_json("/data.json", &data)
.with_main_file(include_str!("main.typ")); .with_main_file(include_str!("main.typ"));
server.print_typst(typst).await; server.print_typst(typst).await
} }

View file

@ -22,6 +22,5 @@ pub async fn post(server: State<Server>, Form(form): Form<FormData>) -> somehow:
.with_json("/data.json", &data) .with_json("/data.json", &data)
.with_main_file(include_str!("main.typ")); .with_main_file(include_str!("main.typ"));
server.print_typst(typst).await; server.print_typst(typst).await
Ok(())
} }

View file

@ -1,11 +1,11 @@
use showbits_typst::Typst; use showbits_typst::Typst;
use tokio::sync::mpsc; use tokio::sync::{mpsc, oneshot};
use crate::persistent_printer::PersistentPrinter; use crate::persistent_printer::PersistentPrinter;
pub enum Command { pub enum Command {
Backlog, Backlog,
Typst(Typst), Typst(Typst, oneshot::Sender<anyhow::Result<()>>),
} }
pub struct Drawer { pub struct Drawer {
@ -30,11 +30,16 @@ impl Drawer {
Command::Backlog => { Command::Backlog => {
self.printer.print_backlog()?; self.printer.print_backlog()?;
} }
Command::Typst(typst) => { Command::Typst(typst, tx) => {
let image = typst.render()?; let _ = tx.send(self.run_cmd_typst(typst));
self.printer.print_image(&image)?;
} }
} }
Ok(()) Ok(())
} }
fn run_cmd_typst(&mut self, typst: Typst) -> anyhow::Result<()> {
let image = typst.render()?;
self.printer.print_image(&image)?;
Ok(())
}
} }

View file

@ -8,7 +8,10 @@ use axum::{
routing::{get, post}, routing::{get, post},
}; };
use showbits_typst::Typst; use showbits_typst::Typst;
use tokio::{net::TcpListener, sync::mpsc}; use tokio::{
net::TcpListener,
sync::{mpsc, oneshot},
};
use crate::{documents, drawer::Command}; use crate::{documents, drawer::Command};
@ -18,8 +21,10 @@ pub struct Server {
} }
impl Server { impl Server {
pub async fn print_typst(&self, typst: Typst) { pub async fn print_typst(&self, typst: Typst) -> somehow::Result<()> {
let _ = self.tx.send(Command::Typst(typst)).await; let (tx, rx) = oneshot::channel();
let _ = self.tx.send(Command::Typst(typst, tx)).await;
rx.await?.map_err(somehow::Error)
} }
} }