conch_runtime_pshaw/eval/param_subst/
default.rs

1use super::is_present;
2use crate::eval::{Fields, ParamEval, TildeExpansion, WordEval, WordEvalConfig};
3
4/// Evaluate a parameter or use a default value if it is empty.
5///
6/// First, `param` will be evaluated and returned as is as long as the result is
7/// non-empty, or if the result is defined-but-empty and `strict = false`.
8///
9/// Otherwise, `default` will be evaluated using `cfg` and that response yielded.
10///
11/// Note: field splitting will neither be done on the parameter, nor the default word.
12pub async fn default<P, W, E>(
13    strict: bool,
14    param: &P,
15    default: Option<W>,
16    env: &mut E,
17    cfg: TildeExpansion,
18) -> Result<Fields<W::EvalResult>, W::Error>
19where
20    P: ?Sized + ParamEval<E, EvalResult = W::EvalResult>,
21    W: WordEval<E>,
22    E: ?Sized,
23{
24    if let Some(fields) = is_present(strict, param.eval(false, env)) {
25        return Ok(fields);
26    }
27
28    let word = match default {
29        Some(w) => w,
30        None => return Ok(Fields::Zero),
31    };
32
33    let future = word.eval_with_config(
34        env,
35        WordEvalConfig {
36            split_fields_further: false,
37            tilde_expansion: cfg,
38        },
39    );
40
41    Ok(future.await?.await)
42}