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
use wagon_parser::parser::expression::Expression;
use proc_macro2::{TokenStream, Ident};
use quote::quote;
use crate::SpannableIdent;

use super::{Rc, ToTokensState};

impl<U> ToTokensState<U> for Expression {
    fn to_tokens(&self, state: &mut U, label: Rc<Ident>, attr_fun: fn(&mut U, Rc<Ident>, SpannableIdent)) -> TokenStream {
        match self {
            Self::Subproc(s) => {
                quote!(
                    {
                        let out = subprocess::Exec::shell(#s).stdout(subprocess::Redirection::Pipe).capture().expect("Was unable to capture shell output").stdout_str();
                        let value: Value = serde_json::from_str(&out).expect("Was unable to parse json");
                        value
                    }
                )
            },
            Self::If { this, then, r#else } => {
                let this_stream = this.to_tokens(state, label.clone(), attr_fun);
                let then_stream = then.to_tokens(state, label.clone(), attr_fun);
                let if_stream = quote!(
                    if #this_stream {
                        #then_stream
                    }
                );
                if let Some(e) = r#else {
                    let e_stream = e.to_tokens(state, label, attr_fun);
                    quote!(
                        #if_stream else {
                            #e_stream
                        }
                    )
                } else {
                    if_stream
                }
            },
            Self::Disjunct(d) => {
                d.to_tokens(state, label, attr_fun)
            },
        }
    }
}