From 53e0cb5c7b2002a5ca2cbe3e82d645c9a7b8ab85 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 18 Jan 2022 00:42:49 -0500 Subject: consistency --- src/parse/ast.rs | 74 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/parse/ast.rs b/src/parse/ast.rs index dd5065a..5dabb43 100644 --- a/src/parse/ast.rs +++ b/src/parse/ast.rs @@ -210,16 +210,6 @@ pub struct Word { } impl Word { - fn build_ast(pair: pest::iterators::Pair) -> Self { - assert!(matches!( - pair.as_rule(), - Rule::word | Rule::alternation_word - )); - Self { - parts: pair.into_inner().flat_map(WordPart::build_ast).collect(), - } - } - pub async fn eval(self, env: &Env) -> anyhow::Result> { let mut opts = glob::MatchOptions::new(); opts.require_literal_separator = true; @@ -311,6 +301,16 @@ impl Word { } Ok(expanded_words) } + + fn build_ast(pair: pest::iterators::Pair) -> Self { + assert!(matches!( + pair.as_rule(), + Rule::word | Rule::alternation_word + )); + Self { + parts: pair.into_inner().flat_map(WordPart::build_ast).collect(), + } + } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -324,6 +324,33 @@ enum WordPart { } impl WordPart { + async fn eval(self, env: &Env) -> String { + match self { + Self::Alternation(_) => unreachable!(), + Self::Substitution(commands) => { + let mut cmd = async_std::process::Command::new( + std::env::current_exe().unwrap(), + ); + cmd.args(&["-c", &commands]); + cmd.stdin(async_std::process::Stdio::inherit()); + cmd.stderr(async_std::process::Stdio::inherit()); + let mut out = + String::from_utf8(cmd.output().await.unwrap().stdout) + .unwrap(); + if out.ends_with('\n') { + out.truncate(out.len() - 1); + } + out + } + Self::Var(name) => { + env.var(&name).unwrap_or_else(|| "".to_string()) + } + Self::Bareword(s) + | Self::DoubleQuoted(s) + | Self::SingleQuoted(s) => s, + } + } + #[allow(clippy::needless_pass_by_value)] fn build_ast( pair: pest::iterators::Pair, @@ -365,33 +392,6 @@ impl WordPart { _ => unreachable!(), }) } - - async fn eval(self, env: &Env) -> String { - match self { - Self::Alternation(_) => unreachable!(), - Self::Substitution(commands) => { - let mut cmd = async_std::process::Command::new( - std::env::current_exe().unwrap(), - ); - cmd.args(&["-c", &commands]); - cmd.stdin(async_std::process::Stdio::inherit()); - cmd.stderr(async_std::process::Stdio::inherit()); - let mut out = - String::from_utf8(cmd.output().await.unwrap().stdout) - .unwrap(); - if out.ends_with('\n') { - out.truncate(out.len() - 1); - } - out - } - Self::Var(name) => { - env.var(&name).unwrap_or_else(|| "".to_string()) - } - Self::Bareword(s) - | Self::DoubleQuoted(s) - | Self::SingleQuoted(s) => s, - } - } } #[derive(Debug, Clone, PartialEq, Eq)] -- cgit v1.2.3-54-g00ecf