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
use error::ExpansionError;
use env::{StringWrapper,VariableEnvironment};
use eval::{Fields, ParamEval, TildeExpansion, WordEval, WordEvalConfig};
use future::{Async, EnvFuture, Poll};
use std::fmt::Display;
use super::is_present;
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct Error<T, F> {
state: State<T, F>,
}
#[derive(Debug)]
enum State<T, F> {
ParamVal(Option<Fields<T>>),
EmptyParameter(Option<String>),
Error(Option<String>, F),
}
pub fn error<P: ?Sized, W, E: ?Sized>(
strict: bool,
param: &P,
error: Option<W>,
env: &E,
cfg: TildeExpansion
) -> Error<P::EvalResult, W::EvalFuture>
where P: ParamEval<E> + Display,
W: WordEval<E>,
{
let state = match is_present(strict, param.eval(false, env)) {
fields@Some(_) => State::ParamVal(fields),
None => {
let param_display = param.to_string();
match error {
Some(w) => {
let future = w.eval_with_config(env, WordEvalConfig {
split_fields_further: false,
tilde_expansion: cfg,
});
State::Error(Some(param_display), future)
},
None => State::EmptyParameter(Some(param_display)),
}
},
};
Error {
state: state,
}
}
impl<T, FT, F, E: ?Sized> EnvFuture<E> for Error<T, F>
where FT: StringWrapper,
F: EnvFuture<E, Item = Fields<FT>>,
F::Error: From<ExpansionError>,
E: VariableEnvironment,
{
type Item = Fields<T>;
type Error = F::Error;
fn poll(&mut self, env: &mut E) -> Poll<Self::Item, Self::Error> {
match self.state {
State::ParamVal(ref mut fields) => {
let ret = fields.take().expect("polled twice");
Ok(Async::Ready(ret))
},
State::EmptyParameter(ref mut param) => {
let param = param.take().expect("polled twice");
let msg = String::from("parameter null or not set");
Err(ExpansionError::EmptyParameter(param, msg).into())
},
State::Error(ref mut param, ref mut f) => {
let err = try_ready!(f.poll(env)).join().into_owned();
let param = param.take().expect("polled twice");
Err(ExpansionError::EmptyParameter(param, err).into())
},
}
}
fn cancel(&mut self, env: &mut E) {
match self.state {
State::ParamVal(_) |
State::EmptyParameter(_) => {},
State::Error(_, ref mut f) => f.cancel(env),
}
}
}