-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathparameter.rs
More file actions
63 lines (56 loc) · 1.59 KB
/
parameter.rs
File metadata and controls
63 lines (56 loc) · 1.59 KB
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
use super::*;
#[derive(PartialEq, Debug, Clone, Serialize)]
pub(crate) struct Parameter<'src> {
pub(crate) default: Option<Expression<'src>>,
pub(crate) export: bool,
pub(crate) help: Option<String>,
pub(crate) kind: ParameterKind,
pub(crate) long: Option<String>,
pub(crate) name: Name<'src>,
#[serde(skip)]
pub(crate) number: Number,
pub(crate) pattern: Option<Pattern<'src>>,
pub(crate) short: Option<char>,
pub(crate) value: Option<String>,
}
impl<'src> Parameter<'src> {
pub(crate) fn is_option(&self) -> bool {
self.long.is_some() || self.short.is_some()
}
pub(crate) fn is_required(&self) -> bool {
self.default.is_none() && self.kind != ParameterKind::Star
}
pub(crate) fn check_pattern_match(
&self,
recipe: &Recipe<'src>,
value: &str,
) -> Result<(), Error<'src>> {
let Some(pattern) = &self.pattern else {
return Ok(());
};
if pattern.is_match(value) {
return Ok(());
}
Err(Error::ArgumentPatternMismatch {
argument: value.into(),
parameter: self.name.lexeme(),
pattern: Box::new(pattern.clone()),
recipe: recipe.name(),
})
}
}
impl ColorDisplay for Parameter<'_> {
fn fmt(&self, f: &mut Formatter, color: Color) -> fmt::Result {
if let Some(prefix) = self.kind.prefix() {
write!(f, "{}", color.annotation().paint(prefix))?;
}
if self.export {
write!(f, "$")?;
}
write!(f, "{}", color.parameter().paint(self.name.lexeme()))?;
if let Some(ref default) = self.default {
write!(f, "={}", color.string().paint(&default.to_string()))?;
}
Ok(())
}
}