Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,21 @@ $ just bar
abc
```

If the `else` part is omitted, it defaults to "":

```just
bar mode='debug':
foo {{ if mode == 'release' { 'do_release_build' } }}
```

```console
$ just bar
foo

$ just bar release
foo do_release_build
```

### Stopping execution with error

Execution can be halted with the `error` function. For example:
Expand Down
5 changes: 4 additions & 1 deletion src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ impl<'src, 'run> Evaluator<'src, 'run> {
if self.evaluate_condition(condition)? {
self.evaluate_expression(then)
} else {
self.evaluate_expression(otherwise)
match otherwise {
Some(otherwise) => self.evaluate_expression(otherwise),
None => Ok(String::new()),
}
}
}
Expression::Group { contents } => self.evaluate_expression(contents),
Expand Down
17 changes: 10 additions & 7 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub(crate) enum Expression<'src> {
Conditional {
condition: Condition<'src>,
then: Box<Expression<'src>>,
otherwise: Box<Expression<'src>>,
otherwise: Option<Box<Expression<'src>>>,
},
/// `(contents)`
Group { contents: Box<Expression<'src>> },
Expand Down Expand Up @@ -72,13 +72,16 @@ impl Display for Expression<'_> {
condition,
then,
otherwise,
} => {
if let Self::Conditional { .. } = **otherwise {
write!(f, "if {condition} {{ {then} }} else {otherwise}")
} else {
write!(f, "if {condition} {{ {then} }} else {{ {otherwise} }}")
} => match otherwise {
None => write!(f, "if {condition} {{ {then} }}"),
Some(otherwise) => {
if let Self::Conditional { .. } = **otherwise {
write!(f, "if {condition} {{ {then} }} else {otherwise}")
} else {
write!(f, "if {condition} {{ {then} }} else {{ {otherwise} }}")
}
}
}
},
Self::Group { contents } => write!(f, "({contents})"),
Self::Join { lhs: None, rhs } => write!(f, "/ {rhs}"),
Self::Join {
Expand Down
4 changes: 3 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ impl<'src> Node<'src> for Expression<'src> {
tree.push_mut(operator.to_string());
tree.push_mut(rhs.tree());
tree.push_mut(then.tree());
tree.push_mut(otherwise.tree());
if let Some(otherwise) = otherwise.as_ref() {
tree.push_mut(otherwise.tree());
}
tree
}
Self::Group { contents } => Tree::List(vec![contents.tree()]),
Expand Down
27 changes: 16 additions & 11 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,21 +611,26 @@ impl<'run, 'src> Parser<'run, 'src> {

self.expect(BraceR)?;

self.expect_keyword(Keyword::Else)?;

let otherwise = if self.accepted_keyword(Keyword::If)? {
self.parse_conditional()?
} else {
self.expect(BraceL)?;
let otherwise = self.parse_expression()?;
self.expect(BraceR)?;
otherwise
};
if self.accepted_keyword(Keyword::Else)? {
let otherwise = if self.accepted_keyword(Keyword::If)? {
self.parse_conditional()?
} else {
self.expect(BraceL)?;
let otherwise = self.parse_expression()?;
self.expect(BraceR)?;
otherwise
};
return Ok(Expression::Conditional {
condition,
then: then.into(),
otherwise: Some(otherwise.into()),
});
}

Ok(Expression::Conditional {
condition,
then: then.into(),
otherwise: otherwise.into(),
otherwise: None,
})
}

Expand Down
7 changes: 5 additions & 2 deletions src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub enum Expression {
lhs: Box<Expression>,
rhs: Box<Expression>,
then: Box<Expression>,
otherwise: Box<Expression>,
otherwise: Option<Box<Expression>>,
operator: ConditionalOperator,
},
Join {
Expand Down Expand Up @@ -325,7 +325,10 @@ impl Expression {
} => Self::Conditional {
lhs: Self::new(lhs).into(),
operator: ConditionalOperator::new(*operator),
otherwise: Self::new(otherwise).into(),
otherwise: match otherwise {
Some(x) => Some(Self::new(x).into()),
None => None,
},
rhs: Self::new(rhs).into(),
then: Self::new(then).into(),
},
Expand Down
4 changes: 3 additions & 1 deletion src/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ impl<'src> Iterator for Variables<'_, 'src> {
then,
otherwise,
} => {
self.stack.push(otherwise);
if let Some(otherwise) = otherwise.as_ref() {
self.stack.push(otherwise);
}
self.stack.push(then);
self.stack.push(rhs);
self.stack.push(lhs);
Expand Down
38 changes: 6 additions & 32 deletions tests/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,43 +230,17 @@ fn if_else() {
}

#[test]
fn missing_else() {
fn optional_else() {
Test::new()
.justfile(
"
TEST := if path_exists('/bin/bash') == 'true' {'yes'}
",
)
.stderr(
"
error: Expected keyword `else` but found `end of line`
——▶ justfile:1:54
1 │ TEST := if path_exists('/bin/bash') == 'true' {'yes'}
│ ^
",
)
.status(EXIT_FAILURE)
.run();
}
x := if '1' == '1' { 'yes' }

#[test]
fn incorrect_else_identifier() {
Test::new()
.justfile(
"
TEST := if path_exists('/bin/bash') == 'true' {'yes'} els {'no'}
",
)
.stderr(
"
error: Expected keyword `else` but found identifier `els`
——▶ justfile:1:55
1 │ TEST := if path_exists('/bin/bash') == 'true' {'yes'} els {'no'}
│ ^^^
foo:
echo {{ x }}
",
)
.status(EXIT_FAILURE)
.stdout("yes\n")
.stderr("echo yes\n")
.run();
}
17 changes: 0 additions & 17 deletions tests/error_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@ fn invalid_alias_attribute() {
.run();
}

#[test]
fn expected_keyword() {
Test::new()
.justfile("foo := if '' == '' { '' } arlo { '' }")
.stderr(
"
error: Expected keyword `else` but found identifier `arlo`
——▶ justfile:1:27
1 │ foo := if '' == '' { '' } arlo { '' }
│ ^^^^
",
)
.status(EXIT_FAILURE)
.run();
}

#[test]
fn unexpected_character() {
Test::new()
Expand Down