diff --git a/Cargo.lock b/Cargo.lock index 6b2f008..f04b38e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -573,10 +573,12 @@ dependencies = [ "clients", "color-eyre", "core_types", + "http 1.0.0", "image", "leptos", "qrcode", "rayon", + "rmp-serde", "serde", "strum", "surrealdb", @@ -4030,16 +4032,6 @@ dependencies = [ "serde", ] -[[package]] -name = "rmp-sfn" -version = "0.1.0" -dependencies = [ - "http 1.0.0", - "rmp-serde", - "serde", - "server_fn", -] - [[package]] name = "roaring" version = "0.10.3" diff --git a/crates/bl/Cargo.toml b/crates/bl/Cargo.toml index e6afea6..74f82f1 100644 --- a/crates/bl/Cargo.toml +++ b/crates/bl/Cargo.toml @@ -11,10 +11,13 @@ leptos.workspace = true bytes.workspace = true serde.workspace = true thiserror.workspace = true +http.workspace = true clients = { path = "../clients", optional = true } artifact = { path = "../artifact", optional = true } +rmp-serde = "1.1.2" + base64 = { workspace = true, optional = true } color-eyre = { workspace = true, optional = true } image = { workspace = true, optional = true } diff --git a/crates/bl/src/fetch.rs b/crates/bl/src/fetch.rs index 7c25b25..0f0a576 100644 --- a/crates/bl/src/fetch.rs +++ b/crates/bl/src/fetch.rs @@ -1,7 +1,10 @@ use core_types::{PhotoGroup, PhotoThumbnailDisplayParams}; -use leptos::{server, ServerFnError}; +use leptos::{server, server_fn::codec::Json, ServerFnError}; -#[server] +#[server( + input = Json, + output = Json, +)] #[cfg_attr(feature = "ssr", tracing::instrument)] pub async fn fetch_user_owned_photo_groups( user_id: core_types::UserRecordId, @@ -43,7 +46,10 @@ pub async fn fetch_user_owned_photo_groups( }) } -#[server] +#[server( + input = Json, + output = Json, +)] #[cfg_attr(feature = "ssr", tracing::instrument)] pub async fn fetch_photo_group( photo_group_id: core_types::PhotoGroupRecordId, @@ -77,7 +83,10 @@ pub async fn fetch_photo_group( }) } -#[server] +#[server( + input = Json, + output = Json, +)] #[cfg_attr(feature = "ssr", tracing::instrument)] pub async fn fetch_user( user_id: core_types::UserRecordId, @@ -110,7 +119,10 @@ pub async fn fetch_user( }) } -#[server] +#[server( + input = Json, + output = Json, +)] #[cfg_attr(feature = "ssr", tracing::instrument)] pub async fn fetch_photo_thumbnail( photo_id: core_types::PhotoRecordId, diff --git a/crates/bl/src/lib.rs b/crates/bl/src/lib.rs index 34f640c..8c3b64f 100644 --- a/crates/bl/src/lib.rs +++ b/crates/bl/src/lib.rs @@ -2,4 +2,5 @@ pub mod fetch; #[cfg(feature = "ssr")] pub mod model_ext; pub mod qr_code; +pub mod rmp_sfn; pub mod upload; diff --git a/crates/bl/src/qr_code.rs b/crates/bl/src/qr_code.rs index 9ae6e0b..1c4a4fc 100644 --- a/crates/bl/src/qr_code.rs +++ b/crates/bl/src/qr_code.rs @@ -1,4 +1,4 @@ -use leptos::{server, ServerFnError}; +use leptos::{server, server_fn::codec::Json, ServerFnError}; /// Generate a QR code from the given data. Returns base64 encoded PNG data. #[cfg(feature = "ssr")] @@ -21,7 +21,10 @@ pub fn generate_qr_code_inner(data: &str) -> color_eyre::eyre::Result { Ok(data) } -#[server] +#[server( + input = Json, + output = Json, +)] #[cfg_attr(feature = "ssr", tracing::instrument)] pub async fn generate_qr_code(data: String) -> Result { generate_qr_code_inner(&data).map_err(|e| { diff --git a/crates/rmp-sfn/src/lib.rs b/crates/bl/src/rmp_sfn.rs similarity index 83% rename from crates/rmp-sfn/src/lib.rs rename to crates/bl/src/rmp_sfn.rs index 712c67e..584a421 100644 --- a/crates/rmp-sfn/src/lib.rs +++ b/crates/bl/src/rmp_sfn.rs @@ -1,22 +1,18 @@ -#![warn(missing_docs)] - -//! A library for using [MessagePack](https://msgpack.org/) with [server_fn](https://crates.io/crates/server_fn). - use http::Method; -use serde::{de::DeserializeOwned, Deserialize, Serialize}; -use server_fn::{ +use leptos::server_fn::{ codec::{Encoding, FromReq, FromRes, IntoReq, IntoRes}, request::{ClientReq, Req}, response::{ClientRes, Res}, ServerFnError, }; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// A codec for MessagePack. pub struct MessagePack; /// A wrapper for a type that can be encoded to MessagePack. #[derive(Serialize, Deserialize)] -pub struct RmpEncoded(T); +pub struct RmpEncoded(pub T); impl Encoding for MessagePack { const CONTENT_TYPE: &'static str = "application/msgpack"; @@ -50,8 +46,8 @@ where T: DeserializeOwned, { async fn from_req(req: Request) -> Result> { - let string_data = req.try_into_string().await?; - rmp_serde::from_slice::(string_data.as_bytes()) + let data = req.try_into_bytes().await?; + rmp_serde::from_slice::(&data) .map(RmpEncoded) .map_err(|e| ServerFnError::Args(e.to_string())) } @@ -75,8 +71,8 @@ where T: DeserializeOwned, { async fn from_res(res: Response) -> Result> { - let data = res.try_into_string().await?; - rmp_serde::from_slice(data.as_bytes()) + let data = res.try_into_bytes().await?; + rmp_serde::from_slice(&data) .map(RmpEncoded) .map_err(|e| ServerFnError::Deserialization(e.to_string())) } diff --git a/crates/bl/src/upload.rs b/crates/bl/src/upload.rs index a422624..1b5886e 100644 --- a/crates/bl/src/upload.rs +++ b/crates/bl/src/upload.rs @@ -6,6 +6,8 @@ use core_types::{PhotoGroupRecordId, PhotoGroupUploadParams, PhotoRecordId}; use leptos::{server, server_fn::codec::Json, ServerFnError}; use strum::{Display, EnumString}; +use crate::rmp_sfn::{MessagePack, RmpEncoded}; + fn thumbnail_size(aspect_ratio: f32) -> (u32, u32) { if aspect_ratio > 1.0 { (200, (200.0 / aspect_ratio) as u32) @@ -22,7 +24,8 @@ pub enum PhotoUploadError { } #[server( - input = Json, + input = MessagePack, + custom = RmpEncoded, output = Json, )] #[cfg_attr(feature = "ssr", tracing::instrument)] diff --git a/crates/rmp-sfn/Cargo.toml b/crates/rmp-sfn/Cargo.toml deleted file mode 100644 index 3c05c60..0000000 --- a/crates/rmp-sfn/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "rmp-sfn" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -http.workspace = true -rmp-serde = "1.1.2" -serde = { workspace = true, features = ["derive"] } -server_fn.workspace = true