Make new endpoints more consistent

This commit is contained in:
Joscha 2025-03-01 20:26:16 +01:00
parent db06addc42
commit 24c84801f1
3 changed files with 36 additions and 25 deletions

View file

@ -1,6 +1,6 @@
{ {
"seamless": true, "algo": "floyd-steinberg",
"feed": true,
"bright": true, "bright": true,
"algo": "floyd-steinberg" "seamless": true,
"feed": true
} }

View file

@ -15,20 +15,20 @@ use crate::{
}; };
#[derive(Serialize)] #[derive(Serialize)]
pub struct Data { struct Data {
pub seamless: bool, algo: String,
pub feed: bool, bright: bool,
pub bright: bool, seamless: bool,
pub algo: String, feed: bool,
} }
pub async fn post(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> { pub async fn post(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
let mut image = None; let mut image = None;
let mut data = Data { let mut data = Data {
algo: "stucki".to_string(),
bright: true,
seamless: false, seamless: false,
feed: true, feed: true,
bright: true,
algo: "floyd-steinberg".to_string(),
}; };
while let Some(field) = multipart.next_field().await? { while let Some(field) = multipart.next_field().await? {
@ -38,18 +38,18 @@ pub async fn post(server: State<Server>, mut multipart: Multipart) -> somehow::R
let decoded = image::load_from_memory(&data)?.into_rgba8(); let decoded = image::load_from_memory(&data)?.into_rgba8();
image = Some(decoded); image = Some(decoded);
} }
Some("algo") => {
data.algo = field.text().await?;
}
Some("bright") => {
data.bright = !field.text().await?.is_empty();
}
Some("seamless") => { Some("seamless") => {
data.seamless = !field.text().await?.is_empty(); data.seamless = !field.text().await?.is_empty();
} }
Some("feed") => { Some("feed") => {
data.feed = !field.text().await?.is_empty(); data.feed = !field.text().await?.is_empty();
} }
Some("bright") => {
data.bright = !field.text().await?.is_empty();
}
Some("algo") => {
data.algo = field.text().await?;
}
_ => {} _ => {}
} }
} }

View file

@ -6,18 +6,29 @@ use crate::{
server::Server, server::Server,
}; };
#[derive(Serialize, Deserialize)] #[derive(Serialize)]
pub struct Data { struct Data {
pub text: String, text: String,
#[serde(default)] force_wrap: bool,
pub force_wrap: bool, feed: bool,
#[serde(default)]
pub feed: bool,
} }
pub async fn post(server: State<Server>, request: Form<Data>) { #[derive(Deserialize)]
pub struct FormData {
pub text: String,
pub force_wrap: Option<bool>,
pub feed: Option<bool>,
}
pub async fn post(server: State<Server>, Form(form): Form<FormData>) {
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() let typst = super::typst_with_lib()
.with_json("/data.json", &request.0) .with_json("/data.json", &data)
.with_main_file(include_str!("main.typ")); .with_main_file(include_str!("main.typ"));
let _ = server let _ = server