wagon_codegen/nodes/
expression.rs

1use wagon_parser::parser::expression::Expression;
2use proc_macro2::{TokenStream, Ident};
3use quote::quote;
4use crate::SpannableIdent;
5
6use super::{Rc, ToTokensState};
7
8impl<U> ToTokensState<U> for Expression {
9    fn to_tokens(&self, state: &mut U, label: Rc<Ident>, attr_fun: fn(&mut U, Rc<Ident>, SpannableIdent)) -> TokenStream {
10        match self {
11            Self::Subproc(s) => {
12                quote!(
13                    {
14                        let out = subprocess::Exec::shell(#s).stdout(subprocess::Redirection::Pipe).capture().expect("Was unable to capture shell output").stdout_str();
15                        let value: Value = serde_json::from_str(&out).expect("Was unable to parse json");
16                        value
17                    }
18                )
19            },
20            Self::If { this, then, r#else } => {
21                let this_stream = this.to_tokens(state, label.clone(), attr_fun);
22                let then_stream = then.to_tokens(state, label.clone(), attr_fun);
23                let if_stream = quote!(
24                    if #this_stream {
25                        #then_stream
26                    }
27                );
28                if let Some(e) = r#else {
29                    let e_stream = e.to_tokens(state, label, attr_fun);
30                    quote!(
31                        #if_stream else {
32                            #e_stream
33                        }
34                    )
35                } else {
36                    if_stream
37                }
38            },
39            Self::Disjunct(d) => {
40                d.to_tokens(state, label, attr_fun)
41            },
42        }
43    }
44}