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,
"feed": true,
"algo": "floyd-steinberg",
"bright": true,
"algo": "floyd-steinberg"
"seamless": true,
"feed": true
}

View file

@ -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<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
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<Server>, 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?;
}
_ => {}
}
}

View file

@ -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<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()
.with_json("/data.json", &request.0)
.with_json("/data.json", &data)
.with_main_file(include_str!("main.typ"));
let _ = server