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
//! Example of controll flow implementations:
//! 1. One variant is based on tags `<for />` `<if />` `<else />` `<else-if />`
//! 2. another variand is based on escape character inside unquoted texts `@if
//!    {}` `@for foo in array {}`
use std::marker::PhantomData;

use quote::ToTokens;
use syn::parse::{Parse, ParseStream};

pub mod escape;
#[cfg(feature = "extendable")]
pub mod extendable;
pub mod tags;

#[cfg(feature = "extendable")]
pub use extendable::ExtendableCustomNode;

// Either variant, with Parse/ToTokens implementation
#[derive(Copy, Clone, Debug)]
pub enum Either<A, B> {
    A(A),
    B(B),
}
impl<A: Parse, B: Parse> Parse for Either<A, B> {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        if Self::peek_a(input) {
            input.parse().map(Self::A)
        } else {
            input.parse().map(Self::B)
        }
    }
}
impl<A: ToTokens, B: ToTokens> ToTokens for Either<A, B> {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        match self {
            Self::A(a) => a.to_tokens(tokens),
            Self::B(b) => b.to_tokens(tokens),
        }
    }
}

#[allow(dead_code)]
impl<A, B> Either<A, B> {
    pub fn peek_a(stream: ParseStream) -> bool
    where
        A: Parse,
        B: Parse,
    {
        stream.fork().parse::<A>().is_ok()
    }
    pub fn to_b(self) -> Option<B> {
        match self {
            Self::A(_) => None,
            Self::B(b) => Some(b),
        }
    }
    pub fn to_a(self) -> Option<A> {
        match self {
            Self::A(a) => Some(a),
            Self::B(_) => None,
        }
    }
    pub fn is_b(self) -> bool {
        match self {
            Self::A(_) => false,
            Self::B(_) => true,
        }
    }
    pub fn is_a(self) -> bool {
        match self {
            Self::A(_) => true,
            Self::B(_) => false,
        }
    }
}

pub struct EitherA<A, B>(pub A, pub PhantomData<B>);
pub struct EitherB<A, B>(pub PhantomData<A>, pub B);

impl<A, B> TryFrom<Either<A, B>> for EitherA<A, B> {
    type Error = Either<A, B>;
    fn try_from(value: Either<A, B>) -> Result<Self, Self::Error> {
        match value {
            Either::A(a) => Ok(EitherA(a, PhantomData)),
            rest => Err(rest),
        }
    }
}

impl<A, B> TryFrom<Either<A, B>> for EitherB<A, B> {
    type Error = Either<A, B>;
    fn try_from(value: Either<A, B>) -> Result<Self, Self::Error> {
        match value {
            Either::B(b) => Ok(EitherB(PhantomData, b)),
            rest => Err(rest),
        }
    }
}

impl<A, B> From<EitherA<A, B>> for Either<A, B> {
    fn from(value: EitherA<A, B>) -> Self {
        Self::A(value.0)
    }
}

impl<A, B> From<EitherB<A, B>> for Either<A, B> {
    fn from(value: EitherB<A, B>) -> Self {
        Self::B(value.1)
    }
}

pub trait TryIntoOrCloneRef<T>: Sized {
    fn try_into_or_clone_ref(self) -> Either<T, Self>;
    fn new_from_value(value: T) -> Self;
}

impl<T> TryIntoOrCloneRef<T> for T {
    fn try_into_or_clone_ref(self) -> Either<T, Self> {
        Either::A(self)
    }
    fn new_from_value(value: T) -> Self {
        value
    }
}

#[cfg(test)]
mod tests {

    #[test]
    #[cfg(feature = "extendable")]
    fn test_mixed_tags_and_escape() {
        use quote::ToTokens;
        use rstml::node::Node;

        use crate::{escape, tags, ExtendableCustomNode};

        let tokens = quote::quote! {
                @if true {
                    <p>True</p>
                    <for foo in array !>
                        <p>Foo</p>
                    </for>
                }
                else {
                    <p>False</p>
                }
                @for foo in array {
                    <if foo == 1 !>
                        <p>Foo</p>
                    </if>
                    <p>Foo</p>
                }
        };

        let result = ExtendableCustomNode::parse2_with_config::<(
            tags::Conditions,
            escape::EscapeCode,
        )>(Default::default(), tokens);
        let ok = result.into_result().unwrap();
        assert_eq!(ok.len(), 2);

        let Node::Custom(c) = &ok[0] else {
            unreachable!()
        };
        let escape_if = c.try_downcast_ref::<escape::EscapeCode>().unwrap();
        let escape::EscapedExpr::If(if_) = &escape_if.expression else {
            unreachable!()
        };
        assert_eq!(if_.condition.to_token_stream().to_string(), "true");
        let for_tag = &if_.then_branch.body[1];
        let Node::Custom(c) = &for_tag else {
            unreachable!()
        };
        let for_tag = c.try_downcast_ref::<tags::Conditions>().unwrap();
        let tags::Conditions::For(for_) = for_tag else {
            unreachable!()
        };

        assert_eq!(for_.pat.to_token_stream().to_string(), "foo");

        let Node::Custom(c) = &ok[1] else {
            unreachable!()
        };

        let escape_for = c.try_downcast_ref::<escape::EscapeCode>().unwrap();
        let escape::EscapedExpr::For(for_) = &escape_for.expression else {
            unreachable!()
        };
        assert_eq!(for_.pat.to_token_stream().to_string(), "foo");
    }
}