mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +00:00
hm custom encodings have orphan rule issues
This commit is contained in:
parent
35e8e74dcf
commit
1f017a2ade
1 changed files with 0 additions and 105 deletions
|
@ -310,108 +310,3 @@ pub fn RkyvExample() -> impl IntoView {
|
|||
</Transition>
|
||||
}
|
||||
}
|
||||
|
||||
/// Server function encodings are just types that implement a few traits.
|
||||
/// This means that you can implement your own encodings, by implementing those traits!
|
||||
///
|
||||
/// Here, we'll create a custom encoding that serializes and deserializes the server fn
|
||||
/// using TOML. Why would you ever want to do this? I don't know, but you can!
|
||||
struct Toml;
|
||||
|
||||
impl Encoding for Toml {
|
||||
const CONTENT_TYPE: &'static str = "application/toml";
|
||||
const METHOD: Method = Method::POST;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ssr"))]
|
||||
type Request = BrowserMockReq;
|
||||
#[cfg(feature = "ssr")]
|
||||
type Request = http::Request<axum::body::Body>;
|
||||
#[cfg(not(feature = "ssr"))]
|
||||
type Response = BrowserMockRes;
|
||||
#[cfg(feature = "ssr")]
|
||||
type Response = http::Response<axum::body::Body>;
|
||||
|
||||
impl<T> IntoReq<Toml, BrowserRequest, NoCustomError> for T {
|
||||
fn into_req(
|
||||
self,
|
||||
path: &str,
|
||||
accepts: &str,
|
||||
) -> Result<BrowserRequest, ServerFnError> {
|
||||
let data = toml::to_string(&self)
|
||||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
|
||||
Request::try_new_post(path, Toml::CONTENT_TYPE, accepts, data)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> FromReq<Toml, Request, NoCustomError> for T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
async fn from_req(req: Request) -> Result<Self, ServerFnError> {
|
||||
let string_data = req.try_into_string().await?;
|
||||
toml::from_str::<Self>(&string_data)
|
||||
.map_err(|e| ServerFnError::Args(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoRes<Toml, Response, NoCustomError> for T
|
||||
where
|
||||
T: Serialize + Send,
|
||||
{
|
||||
async fn into_res(self) -> Result<Response, ServerFnError> {
|
||||
let data = toml::to_string(&self)
|
||||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
|
||||
Response::try_from_string(Toml::CONTENT_TYPE, data)
|
||||
}
|
||||
}
|
||||
|
||||
impl<e> FromRes<Toml, BrowserResponse, NoCustomError> for T
|
||||
where
|
||||
T: DeserializeOwned + Send,
|
||||
{
|
||||
async fn from_res(res: BrowserResponse) -> Result<Self, ServerFnError> {
|
||||
let data = res.try_into_string().await?;
|
||||
toml::from_str(&data)
|
||||
.map_err(|e| ServerFnError::Deserialization(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
#[server(
|
||||
input = Toml,
|
||||
output = Toml
|
||||
)]
|
||||
pub async fn why_not(
|
||||
foo: String,
|
||||
bar: String,
|
||||
) -> Result<String, ServerFnError> {
|
||||
// insert a simulated wait
|
||||
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
|
||||
Ok(foo + &bar)
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn CustomEncoding() -> impl IntoView {
|
||||
let input_ref = NodeRef::<Input>::new();
|
||||
let (result, set_result) = create_signal(0);
|
||||
|
||||
view! {
|
||||
<h3>Custom encodings</h3>
|
||||
<p>
|
||||
"This example creates a custom encoding that sends server fn data using TOML. Why? Well... why not?"
|
||||
</p>
|
||||
<input node_ref=input_ref placeholder="Type something here."/>
|
||||
<button
|
||||
on:click=move |_| {
|
||||
let value = input_ref.get().unwrap().value();
|
||||
spawn_local(async move {
|
||||
let new_value = why_not(value, ", but in TOML!!!".to_string());
|
||||
set_result(new_value);
|
||||
});
|
||||
}
|
||||
>
|
||||
Submit
|
||||
</button>
|
||||
<p>{result}</p>
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue