Simplify drawing logic
Now that only typst is left, things become a lot simpler.
This commit is contained in:
parent
a55109c677
commit
7d74a56559
12 changed files with 41 additions and 109 deletions
|
|
@ -2,10 +2,7 @@ use axum::{Form, extract::State};
|
||||||
use jiff::Zoned;
|
use jiff::Zoned;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::server::{Server, somehow};
|
||||||
drawer::{Command, NewTypstDrawing},
|
|
||||||
server::{Server, somehow},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Data {
|
struct Data {
|
||||||
|
|
@ -34,10 +31,6 @@ 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"));
|
||||||
|
|
||||||
let _ = server
|
server.print_typst(typst).await;
|
||||||
.tx
|
|
||||||
.send(Command::draw(NewTypstDrawing::new(typst)))
|
|
||||||
.await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ use image::{ImageFormat, Rgba, RgbaImage, imageops};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
drawer::{Command, NewTypstDrawing},
|
|
||||||
printer::Printer,
|
printer::Printer,
|
||||||
server::{Server, somehow},
|
server::{Server, somehow},
|
||||||
};
|
};
|
||||||
|
|
@ -101,10 +100,6 @@ 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"));
|
||||||
|
|
||||||
let _ = server
|
server.print_typst(typst).await;
|
||||||
.tx
|
|
||||||
.send(Command::draw(NewTypstDrawing::new(typst)))
|
|
||||||
.await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
use axum::{Form, extract::State};
|
use axum::{Form, extract::State};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::server::Server;
|
||||||
drawer::{Command, NewTypstDrawing},
|
|
||||||
server::Server,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Data {
|
struct Data {
|
||||||
|
|
@ -31,8 +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"));
|
||||||
|
|
||||||
let _ = server
|
server.print_typst(typst).await;
|
||||||
.tx
|
|
||||||
.send(Command::draw(NewTypstDrawing::new(typst)))
|
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
use axum::{Form, extract::State};
|
use axum::{Form, extract::State};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::server::Server;
|
||||||
drawer::{Command, NewTypstDrawing},
|
|
||||||
server::Server,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Data {
|
struct Data {
|
||||||
|
|
@ -57,8 +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);
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = server
|
server.print_typst(typst).await;
|
||||||
.tx
|
|
||||||
.send(Command::draw(NewTypstDrawing::new(typst)))
|
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,7 @@ use axum::{
|
||||||
use image::ImageFormat;
|
use image::ImageFormat;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::server::{Server, somehow, statuscode::status_code};
|
||||||
drawer::{Command, NewTypstDrawing},
|
|
||||||
server::{Server, somehow, statuscode::status_code},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Data {
|
struct Data {
|
||||||
|
|
@ -79,10 +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"));
|
||||||
|
|
||||||
let _ = server
|
server.print_typst(typst).await;
|
||||||
.tx
|
|
||||||
.send(Command::draw(NewTypstDrawing::new(typst)))
|
|
||||||
.await;
|
|
||||||
|
|
||||||
Ok(Redirect::to("image").into_response())
|
Ok(Redirect::to("image").into_response())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
use axum::{Form, extract::State};
|
use axum::{Form, extract::State};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::server::Server;
|
||||||
drawer::{Command, NewTypstDrawing},
|
|
||||||
server::Server,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Data {
|
struct Data {
|
||||||
|
|
@ -31,8 +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"));
|
||||||
|
|
||||||
let _ = server
|
server.print_typst(typst).await;
|
||||||
.tx
|
|
||||||
.send(Command::draw(NewTypstDrawing::new(typst)))
|
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,7 @@
|
||||||
use axum::{Form, extract::State};
|
use axum::{Form, extract::State};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::server::{Server, somehow};
|
||||||
drawer::{Command, NewTypstDrawing},
|
|
||||||
server::{Server, somehow},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Data {
|
struct Data {
|
||||||
|
|
@ -25,10 +22,6 @@ 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"));
|
||||||
|
|
||||||
let _ = server
|
server.print_typst(typst).await;
|
||||||
.tx
|
|
||||||
.send(Command::draw(NewTypstDrawing::new(typst)))
|
|
||||||
.await;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,11 @@
|
||||||
mod backlog;
|
use showbits_typst::Typst;
|
||||||
mod new_typst;
|
|
||||||
|
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
use crate::persistent_printer::PersistentPrinter;
|
use crate::persistent_printer::PersistentPrinter;
|
||||||
|
|
||||||
pub use self::{backlog::BacklogDrawing, new_typst::NewTypstDrawing};
|
pub enum Command {
|
||||||
|
Backlog,
|
||||||
pub trait Drawing {
|
Typst(Typst),
|
||||||
fn draw(&self, printer: &mut PersistentPrinter) -> anyhow::Result<()>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Command(Box<dyn Drawing + Send>);
|
|
||||||
|
|
||||||
impl Command {
|
|
||||||
pub fn draw<D: Drawing + Send + 'static>(drawing: D) -> Self {
|
|
||||||
Self(Box::new(drawing))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Drawer {
|
pub struct Drawer {
|
||||||
|
|
@ -31,7 +20,20 @@ impl Drawer {
|
||||||
|
|
||||||
pub fn run(&mut self) -> anyhow::Result<()> {
|
pub fn run(&mut self) -> anyhow::Result<()> {
|
||||||
while let Some(command) = self.rx.blocking_recv() {
|
while let Some(command) = self.rx.blocking_recv() {
|
||||||
command.0.draw(&mut self.printer)?;
|
self.run_cmd(command)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_cmd(&mut self, command: Command) -> anyhow::Result<()> {
|
||||||
|
match command {
|
||||||
|
Command::Backlog => {
|
||||||
|
self.printer.print_backlog()?;
|
||||||
|
}
|
||||||
|
Command::Typst(typst) => {
|
||||||
|
let image = typst.render()?;
|
||||||
|
self.printer.print_image(&image)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
use crate::persistent_printer::PersistentPrinter;
|
|
||||||
|
|
||||||
use super::Drawing;
|
|
||||||
|
|
||||||
pub struct BacklogDrawing;
|
|
||||||
|
|
||||||
impl Drawing for BacklogDrawing {
|
|
||||||
fn draw(&self, printer: &mut PersistentPrinter) -> anyhow::Result<()> {
|
|
||||||
printer.print_backlog()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
use showbits_typst::Typst;
|
|
||||||
|
|
||||||
use crate::persistent_printer::PersistentPrinter;
|
|
||||||
|
|
||||||
use super::Drawing;
|
|
||||||
|
|
||||||
pub struct NewTypstDrawing(pub Typst);
|
|
||||||
|
|
||||||
impl NewTypstDrawing {
|
|
||||||
pub fn new(typst: impl Into<Typst>) -> Self {
|
|
||||||
Self(typst.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drawing for NewTypstDrawing {
|
|
||||||
fn draw(&self, printer: &mut PersistentPrinter) -> anyhow::Result<()> {
|
|
||||||
let image = self.0.render()?;
|
|
||||||
printer.print_image(&image)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -8,7 +8,7 @@ mod server;
|
||||||
use std::{path::PathBuf, time::Duration};
|
use std::{path::PathBuf, time::Duration};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use drawer::{BacklogDrawing, Command};
|
use drawer::Command;
|
||||||
use tokio::{runtime::Runtime, sync::mpsc};
|
use tokio::{runtime::Runtime, sync::mpsc};
|
||||||
|
|
||||||
use self::{drawer::Drawer, persistent_printer::PersistentPrinter};
|
use self::{drawer::Drawer, persistent_printer::PersistentPrinter};
|
||||||
|
|
@ -44,7 +44,7 @@ fn main() -> anyhow::Result<()> {
|
||||||
runtime.spawn(server::run(tx.clone(), args.addr));
|
runtime.spawn(server::run(tx.clone(), args.addr));
|
||||||
runtime.spawn(async move {
|
runtime.spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
let _ = tx.send(Command::draw(BacklogDrawing)).await;
|
let _ = tx.send(Command::Backlog).await;
|
||||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ use axum::{
|
||||||
extract::DefaultBodyLimit,
|
extract::DefaultBodyLimit,
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
};
|
};
|
||||||
|
use showbits_typst::Typst;
|
||||||
use tokio::{net::TcpListener, sync::mpsc};
|
use tokio::{net::TcpListener, sync::mpsc};
|
||||||
|
|
||||||
use crate::{documents, drawer::Command};
|
use crate::{documents, drawer::Command};
|
||||||
|
|
@ -15,7 +16,13 @@ use self::r#static::get_static_file;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
pub tx: mpsc::Sender<Command>,
|
tx: mpsc::Sender<Command>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Server {
|
||||||
|
pub async fn print_typst(&self, typst: Typst) {
|
||||||
|
let _ = self.tx.send(Command::Typst(typst)).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()> {
|
pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue