dioxus_rsx/
text_node.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
use crate::{literal::HotLiteral, location::DynIdx, HotReloadFormattedSegment, IfmtInput};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::ToTokens;
use quote::{quote, TokenStreamExt};
use syn::Result;
use syn::{
    parse::{Parse, ParseStream},
    LitStr,
};

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub struct TextNode {
    pub input: HotReloadFormattedSegment,
    pub dyn_idx: DynIdx,
}

impl Parse for TextNode {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(Self {
            input: input.parse()?,
            dyn_idx: DynIdx::default(),
        })
    }
}

impl ToTokens for TextNode {
    fn to_tokens(&self, tokens: &mut TokenStream2) {
        let txt = &self.input;

        if txt.is_static() {
            tokens.append_all(quote! {
                dioxus_core::DynamicNode::Text(dioxus_core::VText::new(#txt.to_string()))
            })
        } else {
            // todo:
            // Use the RsxLiteral implementation to spit out a hotreloadable variant of this string
            // This is not super efficient since we're doing a bit of cloning
            let as_lit = HotLiteral::Fmted(txt.clone());

            tokens.append_all(quote! {
                dioxus_core::DynamicNode::Text(dioxus_core::VText::new( #as_lit ))
            })
        }
    }
}

impl TextNode {
    pub fn from_text(text: &str) -> Self {
        let ifmt = IfmtInput {
            source: LitStr::new(text, Span::call_site()),
            segments: vec![],
        };
        Self {
            input: ifmt.into(),
            dyn_idx: Default::default(),
        }
    }

    pub fn is_static(&self) -> bool {
        self.input.is_static()
    }
}

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

    #[test]
    fn parses() {
        let input = syn::parse2::<TextNode>(quote! { "hello world" }).unwrap();
        assert_eq!(input.input.source.value(), "hello world");
    }

    #[test]
    fn to_tokens_with_hr() {
        let lit = syn::parse2::<TextNode>(quote! { "hi {world1} {world2} {world3}" }).unwrap();
        println!("{}", lit.to_token_stream().pretty_unparse());
    }

    #[test]
    fn raw_str() {
        let input = syn::parse2::<TextNode>(quote! { r#"hello world"# }).unwrap();
        println!("{}", input.input.source.to_token_stream());
        assert_eq!(input.input.source.value(), "hello world");
    }
}