From 0cdd92dc952f941ecd2f82a2031aee01b15ec7e7 Mon Sep 17 00:00:00 2001 From: spinline Date: Wed, 11 Feb 2026 18:32:41 +0300 Subject: [PATCH] fix: resolve messagepack codec trait bounds and literals --- shared/src/codec.rs | 77 +++++++++++++++++++---------------- shared/src/server_fns/auth.rs | 10 ++--- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/shared/src/codec.rs b/shared/src/codec.rs index 73d4a64..b0c760e 100644 --- a/shared/src/codec.rs +++ b/shared/src/codec.rs @@ -1,83 +1,88 @@ +use leptos::prelude::*; use leptos::server_fn::codec::{Encoding, FromReq, FromRes, IntoReq, IntoRes}; use leptos::server_fn::request::{ClientReq, Req}; -use leptos::server_fn::response::{ClientRes, Res}; -use leptos::server_fn::error::ServerFnError; +use leptos::server_fn::response::{ClientRes, Res, TryRes}; +use http::Method; +use bytes::Bytes; use serde::{de::DeserializeOwned, Serialize}; use std::future::Future; pub struct MessagePack; -impl leptos::server_fn::codec::ContentType for MessagePack { +impl leptos::server_fn::ContentType for MessagePack { const CONTENT_TYPE: &'static str = "application/msgpack"; } impl Encoding for MessagePack { - const METHOD: leptos::server_fn::request::Method = leptos::server_fn::request::Method::POST; + const METHOD: Method = Method::POST; } #[cfg(any(feature = "ssr", feature = "hydrate"))] -impl IntoReq for MessagePack +impl IntoReq for T where - Input: Serialize + Send, - Output: Send, + Request: ClientReq, + T: Serialize + Send, + Error: Send, { - fn into_req(self, args: Input, path: &str) -> Result { - let data = rmp_serde::to_vec(&args) - .map_err(|e| ServerFnError::Serialization(e.to_string()))?; - - ClientReq::try_new( - leptos::server_fn::request::Method::POST, + fn into_req(self, path: &str, accepts: &str) -> Result { + let data = rmp_serde::to_vec(&self) + .map_err(|e| ServerFnError::new(e.to_string()).into())?; + + // Use try_new_post_bytes which should be available on ClientReq trait + Request::try_new_post_bytes( path, - leptos::server_fn::request::Payload::Binary(bytes::Bytes::from(data)) + MessagePack::CONTENT_TYPE, + accepts, + Bytes::from(data) ) } } #[cfg(any(feature = "ssr", feature = "hydrate"))] -impl FromRes for MessagePack +impl FromRes for T where - Input: Send, - Output: DeserializeOwned + Send, + Response: ClientRes + Send, + T: DeserializeOwned + Send, + Error: Send, { - fn from_res(res: ClientRes) -> impl Future> + Send { + fn from_res(res: Response) -> impl Future> + Send { async move { let data = res.try_into_bytes().await?; rmp_serde::from_slice(&data) - .map_err(|e| ServerFnError::Deserialization(e.to_string())) + .map_err(|e| ServerFnError::new(e.to_string()).into()) } } } #[cfg(feature = "ssr")] -impl FromReq for MessagePack +impl FromReq for T where - Input: DeserializeOwned + Send, - Output: Send, + Request: Req + Send, + T: DeserializeOwned, + Error: Send, { - fn from_req(req: Req) -> impl Future> + Send { + fn from_req(req: Request) -> impl Future> + Send { async move { - let body_bytes = req.try_into_bytes().await?; - rmp_serde::from_slice(&body_bytes) - .map_err(|e| ServerFnError::Args(e.to_string())) + let data = req.try_into_bytes().await?; + rmp_serde::from_slice(&data) + .map_err(|e| ServerFnError::new(e.to_string()).into()) } } } #[cfg(feature = "ssr")] -impl IntoRes for MessagePack +impl IntoRes for T where - Input: Send, - Output: Serialize + Send, + Response: TryRes + Send, + T: Serialize + Send, + Error: Send, { - fn into_res(res: Output) -> impl Future> + Send { + fn into_res(self) -> impl Future> + Send { async move { - let data = rmp_serde::to_vec(&res) - .map_err(|e| ServerFnError::Serialization(e.to_string()))?; + let data = rmp_serde::to_vec(&self) + .map_err(|e| ServerFnError::new(e.to_string()).into())?; - Res::try_from_bytes( - bytes::Bytes::from(data), - "application/msgpack" - ) + Response::try_from_bytes(MessagePack::CONTENT_TYPE, Bytes::from(data)) } } } diff --git a/shared/src/server_fns/auth.rs b/shared/src/server_fns/auth.rs index 9850fe8..0156f55 100644 --- a/shared/src/server_fns/auth.rs +++ b/shared/src/server_fns/auth.rs @@ -20,7 +20,7 @@ pub struct SetupStatus { pub completed: bool, } -#[server(GetSetupStatus, "/api/server_fns/GetSetupStatus", encoding = MessagePack)] +#[server(GetSetupStatus, "/api/server_fns/GetSetupStatus", encoding = "MessagePack")] pub async fn get_setup_status() -> Result { use crate::DbContext; @@ -33,7 +33,7 @@ pub async fn get_setup_status() -> Result { }) } -#[server(Setup, "/api/server_fns/Setup", encoding = MessagePack)] +#[server(Setup, "/api/server_fns/Setup", encoding = "MessagePack")] pub async fn setup(username: String, password: String) -> Result<(), ServerFnError> { use crate::DbContext; @@ -55,7 +55,7 @@ pub async fn setup(username: String, password: String) -> Result<(), ServerFnErr Ok(()) } -#[server(Login, "/api/server_fns/Login", encoding = MessagePack)] +#[server(Login, "/api/server_fns/Login", encoding = "MessagePack")] pub async fn login(username: String, password: String) -> Result { use crate::DbContext; use leptos_axum::ResponseOptions; @@ -111,7 +111,7 @@ pub async fn login(username: String, password: String) -> Result Result<(), ServerFnError> { use leptos_axum::ResponseOptions; use cookie::{Cookie, SameSite}; @@ -132,7 +132,7 @@ pub async fn logout() -> Result<(), ServerFnError> { Ok(()) } -#[server(GetUser, "/api/server_fns/GetUser", encoding = MessagePack)] +#[server(GetUser, "/api/server_fns/GetUser", encoding = "MessagePack")] pub async fn get_user() -> Result, ServerFnError> { use axum::http::HeaderMap; use leptos_axum::extract;