Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix server fn encodings #61

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/bl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
22 changes: 17 additions & 5 deletions crates/bl/src/fetch.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/bl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 5 additions & 2 deletions crates/bl/src/qr_code.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -21,7 +21,10 @@ pub fn generate_qr_code_inner(data: &str) -> color_eyre::eyre::Result<String> {
Ok(data)
}

#[server]
#[server(
input = Json,
output = Json,
)]
#[cfg_attr(feature = "ssr", tracing::instrument)]
pub async fn generate_qr_code(data: String) -> Result<String, ServerFnError> {
generate_qr_code_inner(&data).map_err(|e| {
Expand Down
18 changes: 7 additions & 11 deletions crates/rmp-sfn/src/lib.rs → crates/bl/src/rmp_sfn.rs
Original file line number Diff line number Diff line change
@@ -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>(T);
pub struct RmpEncoded<T>(pub T);

impl Encoding for MessagePack {
const CONTENT_TYPE: &'static str = "application/msgpack";
Expand Down Expand Up @@ -50,8 +46,8 @@ where
T: DeserializeOwned,
{
async fn from_req(req: Request) -> Result<Self, ServerFnError<Err>> {
let string_data = req.try_into_string().await?;
rmp_serde::from_slice::<T>(string_data.as_bytes())
let data = req.try_into_bytes().await?;
rmp_serde::from_slice::<T>(&data)
.map(RmpEncoded)
.map_err(|e| ServerFnError::Args(e.to_string()))
}
Expand All @@ -75,8 +71,8 @@ where
T: DeserializeOwned,
{
async fn from_res(res: Response) -> Result<Self, ServerFnError<Err>> {
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()))
}
Expand Down
5 changes: 4 additions & 1 deletion crates/bl/src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -22,7 +24,8 @@ pub enum PhotoUploadError {
}

#[server(
input = Json,
input = MessagePack,
custom = RmpEncoded,
output = Json,
)]
#[cfg_attr(feature = "ssr", tracing::instrument)]
Expand Down
12 changes: 0 additions & 12 deletions crates/rmp-sfn/Cargo.toml

This file was deleted.

Loading