Skip to main content

xwrapup_parser/ds_node/
ds_if.rs

1use super::ds_traits::DsNodeIsMe;
2use quote::ToTokens;
3use std::fmt::Debug;
4use syn::parse::{Parse, ParseStream};
5
6pub struct DsIf {
7    condition: syn::Expr,
8}
9
10impl DsIf {
11    pub fn get_condition(&self) -> &syn::Expr {
12        &self.condition
13    }
14}
15
16impl Debug for DsIf {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        write!(f, "If({:?})", self.condition.to_token_stream().to_string())
19    }
20}
21
22impl Parse for DsIf {
23    fn parse(input: ParseStream) -> syn::Result<Self> {
24        input.parse::<syn::Token![if]>()?;
25        // Parse condition without consuming the braces (which are for children)
26        let condition: syn::Expr = input.call(expr_without_block)?;
27        Ok(DsIf { condition })
28    }
29}
30
31fn expr_without_block(input: ParseStream) -> syn::Result<syn::Expr> {
32    let mut tokens = proc_macro2::TokenStream::new();
33    while !input.is_empty() && !input.peek(syn::token::Brace) {
34        let tt: proc_macro2::TokenTree = input.parse()?;
35        tokens.extend(std::iter::once(tt));
36    }
37    syn::parse2(tokens)
38}
39
40impl DsNodeIsMe for DsIf {
41    fn is_me(input: ParseStream) -> bool {
42        input.peek(syn::Token![if])
43    }
44}