leptos_server/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! Utilities for communicating between the server and the client with Leptos.

#![deny(missing_docs)]
#![forbid(unsafe_code)]

mod action;
pub use action::*;
use std::borrow::Borrow;
mod local_resource;
pub use local_resource::*;
mod multi_action;
pub use multi_action::*;
mod once_resource;
pub use once_resource::*;
mod resource;
pub use resource::*;
mod shared;

use base64::{engine::general_purpose::STANDARD_NO_PAD, DecodeError, Engine};
pub use shared::*;

/// Encodes data into a string.
pub trait IntoEncodedString {
    /// Encodes the data.
    fn into_encoded_string(self) -> String;
}

/// Decodes data from a string.
pub trait FromEncodedStr {
    /// The decoded data.
    type DecodedType<'a>: Borrow<Self>;

    /// The type of an error encountered during decoding.
    type DecodingError;

    /// Decodes the string.
    fn from_encoded_str(
        data: &str,
    ) -> Result<Self::DecodedType<'_>, Self::DecodingError>;
}

impl IntoEncodedString for String {
    fn into_encoded_string(self) -> String {
        self
    }
}

impl FromEncodedStr for str {
    type DecodedType<'a> = &'a str;
    type DecodingError = ();

    fn from_encoded_str(
        data: &str,
    ) -> Result<Self::DecodedType<'_>, Self::DecodingError> {
        Ok(data)
    }
}

impl IntoEncodedString for Vec<u8> {
    fn into_encoded_string(self) -> String {
        STANDARD_NO_PAD.encode(self)
    }
}

impl FromEncodedStr for [u8] {
    type DecodedType<'a> = Vec<u8>;
    type DecodingError = DecodeError;

    fn from_encoded_str(
        data: &str,
    ) -> Result<Self::DecodedType<'_>, Self::DecodingError> {
        STANDARD_NO_PAD.decode(data)
    }
}

#[cfg(feature = "tachys")]
mod view_implementations {
    use crate::Resource;
    use reactive_graph::traits::Read;
    use std::future::Future;
    use tachys::{
        html::attribute::Attribute,
        hydration::Cursor,
        reactive_graph::{RenderEffectState, Suspend, SuspendState},
        ssr::StreamBuilder,
        view::{
            add_attr::AddAnyAttr, Position, PositionState, Render, RenderHtml,
        },
    };

    impl<T, Ser> Render for Resource<T, Ser>
    where
        T: Render + Send + Sync + Clone,
        Ser: Send + 'static,
    {
        type State = RenderEffectState<SuspendState<T>>;

        fn build(self) -> Self::State {
            (move || Suspend::new(async move { self.await })).build()
        }

        fn rebuild(self, state: &mut Self::State) {
            (move || Suspend::new(async move { self.await })).rebuild(state)
        }
    }

    impl<T, Ser> AddAnyAttr for Resource<T, Ser>
    where
        T: RenderHtml + Send + Sync + Clone,
        Ser: Send + 'static,
    {
        type Output<SomeNewAttr: Attribute> = Box<
            dyn FnMut() -> Suspend<
                <T as AddAnyAttr>::Output<
                    <SomeNewAttr::CloneableOwned as Attribute>::CloneableOwned,
                >,
            >
            + Send
        >;

        fn add_any_attr<NewAttr: Attribute>(
            self,
            attr: NewAttr,
        ) -> Self::Output<NewAttr>
        where
            Self::Output<NewAttr>: RenderHtml,
        {
            (move || Suspend::new(async move { self.await })).add_any_attr(attr)
        }
    }

    impl<T, Ser> RenderHtml for Resource<T, Ser>
    where
        T: RenderHtml + Send + Sync + Clone,
        Ser: Send + 'static,
    {
        type AsyncOutput = Option<T>;

        const MIN_LENGTH: usize = 0;

        fn dry_resolve(&mut self) {
            self.read();
        }

        fn resolve(self) -> impl Future<Output = Self::AsyncOutput> + Send {
            (move || Suspend::new(async move { self.await })).resolve()
        }

        fn to_html_with_buf(
            self,
            buf: &mut String,
            position: &mut Position,
            escape: bool,
            mark_branches: bool,
        ) {
            (move || Suspend::new(async move { self.await })).to_html_with_buf(
                buf,
                position,
                escape,
                mark_branches,
            );
        }

        fn to_html_async_with_buf<const OUT_OF_ORDER: bool>(
            self,
            buf: &mut StreamBuilder,
            position: &mut Position,
            escape: bool,
            mark_branches: bool,
        ) where
            Self: Sized,
        {
            (move || Suspend::new(async move { self.await }))
                .to_html_async_with_buf::<OUT_OF_ORDER>(
                    buf,
                    position,
                    escape,
                    mark_branches,
                );
        }

        fn hydrate<const FROM_SERVER: bool>(
            self,
            cursor: &Cursor,
            position: &PositionState,
        ) -> Self::State {
            (move || Suspend::new(async move { self.await }))
                .hydrate::<FROM_SERVER>(cursor, position)
        }
    }
}