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
use {HOME, POLLED_TWICE};
use conch_parser::ast;
use env::{StringWrapper, VariableEnvironment};
use future::{Async, EnvFuture, Poll};
use eval::{Fields, ParamEval, TildeExpansion, WordEval, WordEvalConfig};
use std::borrow::Borrow;
impl<T, P, S, E: ?Sized> WordEval<E> for ast::SimpleWord<T, P, S>
where T: StringWrapper,
P: ParamEval<E, EvalResult = T>,
S: WordEval<E, EvalResult = T>,
E: VariableEnvironment<Var = T>,
E::VarName: Borrow<String>,
{
type EvalResult = T;
type Error = S::Error;
type EvalFuture = SimpleWord<Self::EvalResult, S::EvalFuture>;
fn eval_with_config(self, env: &E, cfg: WordEvalConfig) -> Self::EvalFuture {
use self::ast::SimpleWord::*;
let done = match self {
Literal(s) |
Escaped(s) => Fields::Single(s),
ref s@Star |
ref s@Question |
ref s@SquareOpen |
ref s@SquareClose |
ref s@Colon |
ref s@Tilde => eval_constant_or_panic(s, cfg.tilde_expansion, env),
Param(p) => p.eval(cfg.split_fields_further, env).unwrap_or(Fields::Zero),
Subst(s) => return SimpleWord {
state: State::Subst(s.eval_with_config(env, cfg)),
},
};
SimpleWord {
state: State::Done(Some(done)),
}
}
}
impl<'a, T, P, S, E: ?Sized> WordEval<E> for &'a ast::SimpleWord<T, P, S>
where T: StringWrapper,
P: ParamEval<E, EvalResult = T>,
&'a S: WordEval<E, EvalResult = T>,
E: VariableEnvironment<Var = T>,
E::VarName: Borrow<String>,
{
type EvalResult = T;
type Error = <&'a S as WordEval<E>>::Error;
type EvalFuture = SimpleWord<Self::EvalResult, <&'a S as WordEval<E>>::EvalFuture>;
fn eval_with_config(self, env: &E, cfg: WordEvalConfig) -> Self::EvalFuture {
use self::ast::SimpleWord::*;
let done = match *self {
Literal(ref s) |
Escaped(ref s) => Fields::Single(s.clone()),
ref s@Star |
ref s@Question |
ref s@SquareOpen |
ref s@SquareClose |
ref s@Colon |
ref s@Tilde => eval_constant_or_panic(s, cfg.tilde_expansion, env),
Param(ref p) => p.eval(cfg.split_fields_further, env).unwrap_or(Fields::Zero),
Subst(ref s) => return SimpleWord {
state: State::Subst(s.eval_with_config(env, cfg)),
},
};
SimpleWord {
state: State::Done(Some(done)),
}
}
}
fn eval_constant_or_panic<T, P, S, E: ?Sized>(
simple: &ast::SimpleWord<T, P, S>,
tilde_expansion: TildeExpansion,
env: &E,
) -> Fields<T>
where T: StringWrapper,
E: VariableEnvironment<Var = T>,
E::VarName: Borrow<String>,
{
use self::ast::SimpleWord::*;
match *simple {
Star => Fields::Single(String::from("*").into()),
Question => Fields::Single(String::from("?").into()),
SquareOpen => Fields::Single(String::from("[").into()),
SquareClose => Fields::Single(String::from("]").into()),
Colon => Fields::Single(String::from(":").into()),
Tilde => match tilde_expansion {
TildeExpansion::None => Fields::Single(String::from("~").into()),
TildeExpansion::All |
TildeExpansion::First => {
env.var(&HOME).map_or(Fields::Zero, |f| Fields::Single(f.clone()))
},
},
Literal(_) |
Escaped(_) |
Param(_) |
Subst(_) => panic!("not a constant variant, cannot eval this way!"),
}
}
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct SimpleWord<T, F> {
state: State<Fields<T>, F>,
}
#[derive(Debug)]
enum State<T, F> {
Done(Option<T>),
Subst(F),
}
impl<E: ?Sized, T, F> EnvFuture<E> for SimpleWord<T, F>
where F: EnvFuture<E, Item = Fields<T>>,
{
type Item = Fields<T>;
type Error = F::Error;
fn poll(&mut self, env: &mut E) -> Poll<Self::Item, Self::Error> {
match self.state {
State::Done(ref mut d) => Ok(Async::Ready(d.take().expect(POLLED_TWICE))),
State::Subst(ref mut f) => f.poll(env),
}
}
fn cancel(&mut self, env: &mut E) {
match self.state {
State::Done(_) => {},
State::Subst(ref mut f) => f.cancel(env),
}
}
}