dioxus_rsx/
literal.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use proc_macro2::Span;
use quote::quote;
use quote::ToTokens;
use std::fmt::Display;
use std::ops::Deref;
use syn::{
    parse::{Parse, ParseStream},
    Lit, LitBool, LitFloat, LitInt, LitStr,
};

use crate::{location::DynIdx, IfmtInput, Segment};
use proc_macro2::TokenStream as TokenStream2;

/// A literal value in the rsx! macro
///
/// These get hotreloading super powers, making them a bit more complex than a normal literal.
/// In debug mode we need to generate a bunch of extra code to support hotreloading.
///
/// Eventually we want to remove this notion of hot literals since we're generating different code
/// in debug than in release, which is harder to maintain and can lead to bugs.
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub enum HotLiteral {
    /// A *formatted* string literal
    /// We know this will generate a String, not an &'static str
    ///
    /// The raw str type will generate a &'static str, but we need to distinguish the two for component props
    ///
    /// "hello {world}"
    Fmted(HotReloadFormattedSegment),

    /// A float literal
    ///
    /// 1.0
    Float(LitFloat),

    /// An int literal
    ///
    /// 1
    Int(LitInt),

    /// A bool literal
    ///
    /// true
    Bool(LitBool),
}

impl HotLiteral {
    pub fn quote_as_hot_reload_literal(&self) -> TokenStream2 {
        match &self {
            HotLiteral::Fmted(f) => quote! { dioxus_core::internal::HotReloadLiteral::Fmted(#f) },
            HotLiteral::Float(f) => {
                quote! { dioxus_core::internal::HotReloadLiteral::Float(#f as _) }
            }
            HotLiteral::Int(f) => quote! { dioxus_core::internal::HotReloadLiteral::Int(#f as _) },
            HotLiteral::Bool(f) => quote! { dioxus_core::internal::HotReloadLiteral::Bool(#f) },
        }
    }
}

impl Parse for HotLiteral {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let raw = input.parse::<Lit>()?;

        let value = match raw.clone() {
            Lit::Int(a) => HotLiteral::Int(a),
            Lit::Bool(a) => HotLiteral::Bool(a),
            Lit::Float(a) => HotLiteral::Float(a),
            Lit::Str(a) => HotLiteral::Fmted(IfmtInput::new_litstr(a)?.into()),
            _ => {
                return Err(syn::Error::new(
                    raw.span(),
                    "Only string, int, float, and bool literals are supported",
                ))
            }
        };

        Ok(value)
    }
}

impl ToTokens for HotLiteral {
    fn to_tokens(&self, out: &mut proc_macro2::TokenStream) {
        match &self {
            HotLiteral::Fmted(f) => {
                f.formatted_input.to_tokens(out);
            }
            HotLiteral::Float(f) => f.to_tokens(out),
            HotLiteral::Int(f) => f.to_tokens(out),
            HotLiteral::Bool(f) => f.to_tokens(out),
        }
    }
}

impl HotLiteral {
    pub fn span(&self) -> Span {
        match self {
            HotLiteral::Fmted(f) => f.span(),
            HotLiteral::Float(f) => f.span(),
            HotLiteral::Int(f) => f.span(),
            HotLiteral::Bool(f) => f.span(),
        }
    }
}

impl HotLiteral {
    // We can only handle a few types of literals - the rest need to be expressions
    // todo on adding more of course - they're not hard to support, just work
    pub fn peek(input: ParseStream) -> bool {
        if input.peek(Lit) {
            let lit = input.fork().parse::<Lit>().unwrap();

            matches!(
                lit,
                Lit::Str(_) | Lit::Int(_) | Lit::Float(_) | Lit::Bool(_)
            )
        } else {
            false
        }
    }

    pub fn is_static(&self) -> bool {
        match &self {
            HotLiteral::Fmted(fmt) => fmt.is_static(),
            _ => false,
        }
    }

    pub fn from_raw_text(text: &str) -> Self {
        HotLiteral::Fmted(HotReloadFormattedSegment::from(IfmtInput {
            source: LitStr::new(text, Span::call_site()),
            segments: vec![],
        }))
    }
}

impl Display for HotLiteral {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self {
            HotLiteral::Fmted(l) => l.to_string_with_quotes().fmt(f),
            HotLiteral::Float(l) => l.fmt(f),
            HotLiteral::Int(l) => l.fmt(f),
            HotLiteral::Bool(l) => l.value().fmt(f),
        }
    }
}

/// A formatted segment that can be hot reloaded
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub struct HotReloadFormattedSegment {
    pub formatted_input: IfmtInput,
    pub dynamic_node_indexes: Vec<DynIdx>,
}

impl HotReloadFormattedSegment {
    /// This method is very important!
    /// Deref + Spanned + .span() methods leads to name collisions
    pub fn span(&self) -> Span {
        self.formatted_input.span()
    }
}

impl Deref for HotReloadFormattedSegment {
    type Target = IfmtInput;

    fn deref(&self) -> &Self::Target {
        &self.formatted_input
    }
}

impl From<IfmtInput> for HotReloadFormattedSegment {
    fn from(input: IfmtInput) -> Self {
        let mut dynamic_node_indexes = Vec::new();
        for segment in &input.segments {
            if let Segment::Formatted { .. } = segment {
                dynamic_node_indexes.push(DynIdx::default());
            }
        }
        Self {
            formatted_input: input,
            dynamic_node_indexes,
        }
    }
}

impl Parse for HotReloadFormattedSegment {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ifmt: IfmtInput = input.parse()?;
        Ok(Self::from(ifmt))
    }
}

impl ToTokens for HotReloadFormattedSegment {
    fn to_tokens(&self, tokens: &mut TokenStream2) {
        let mut idx = 0_usize;
        let segments = self.segments.iter().map(|s| match s {
            Segment::Literal(lit) => quote! {
                dioxus_core::internal::FmtSegment::Literal { value: #lit }
            },
            Segment::Formatted(_fmt) => {
                // increment idx for the dynamic segment so we maintain the mapping
                let _idx = self.dynamic_node_indexes[idx].get();
                idx += 1;
                quote! {
                   dioxus_core::internal::FmtSegment::Dynamic { id: #_idx }
                }
            }
        });

        // The static segments with idxs for locations
        tokens.extend(quote! {
            dioxus_core::internal::FmtedSegments::new( vec![ #(#segments),* ], )
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use prettier_please::PrettyUnparse;

    #[test]
    fn parses_lits() {
        let _ = syn::parse2::<HotLiteral>(quote! { "hello" }).unwrap();
        let _ = syn::parse2::<HotLiteral>(quote! { "hello {world}" }).unwrap();
        let _ = syn::parse2::<HotLiteral>(quote! { 1 }).unwrap();
        let _ = syn::parse2::<HotLiteral>(quote! { 1.0 }).unwrap();
        let _ = syn::parse2::<HotLiteral>(quote! { false }).unwrap();
        let _ = syn::parse2::<HotLiteral>(quote! { true }).unwrap();

        // Refuses the other unsupported types - we could add them if we wanted to
        assert!(syn::parse2::<HotLiteral>(quote! { b"123" }).is_err());
        assert!(syn::parse2::<HotLiteral>(quote! { 'a' }).is_err());

        let lit = syn::parse2::<HotLiteral>(quote! { "hello" }).unwrap();
        assert!(matches!(lit, HotLiteral::Fmted(_)));

        let lit = syn::parse2::<HotLiteral>(quote! { "hello {world}" }).unwrap();
        assert!(matches!(lit, HotLiteral::Fmted(_)));
    }

    #[test]
    fn outputs_a_signal() {
        // Should output a type of f64 which we convert into whatever the expected type is via "into"
        // todo: hmmmmmmmmmmmm might not always work
        let lit = syn::parse2::<HotLiteral>(quote! { 1.0 }).unwrap();
        println!("{}", lit.to_token_stream().pretty_unparse());

        let lit = syn::parse2::<HotLiteral>(quote! { "hi" }).unwrap();
        println!("{}", lit.to_token_stream().pretty_unparse());

        let lit = syn::parse2::<HotLiteral>(quote! { "hi {world}" }).unwrap();
        println!("{}", lit.to_token_stream().pretty_unparse());
    }

    #[test]
    fn static_str_becomes_str() {
        let lit = syn::parse2::<HotLiteral>(quote! { "hello" }).unwrap();
        let HotLiteral::Fmted(segments) = &lit else {
            panic!("expected a formatted string");
        };
        assert!(segments.is_static());
        assert_eq!(r##""hello""##, segments.to_string_with_quotes());
        println!("{}", lit.to_token_stream().pretty_unparse());
    }

    #[test]
    fn formatted_prints_as_formatted() {
        let lit = syn::parse2::<HotLiteral>(quote! { "hello {world}" }).unwrap();
        let HotLiteral::Fmted(segments) = &lit else {
            panic!("expected a formatted string");
        };
        assert!(!segments.is_static());
        assert_eq!(r##""hello {world}""##, segments.to_string_with_quotes());
        println!("{}", lit.to_token_stream().pretty_unparse());
    }
}