feat: if expression formatting

This commit is contained in:
rvcas 2022-11-05 17:53:11 -04:00
parent 809d5ea5c5
commit 7092673c45
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
2 changed files with 48 additions and 4 deletions

View File

@ -601,7 +601,48 @@ impl<'comments> Formatter<'comments> {
let document = match expr {
UntypedExpr::ByteArray { .. } => todo!(),
UntypedExpr::If { .. } => todo!(),
UntypedExpr::If {
branches,
final_else,
..
} => {
let first = branches.first();
break_("if", "if ")
.append(self.wrap_expr(&first.condition))
.nest(INDENT)
.append(break_("", " "))
.append("{")
.group()
.append(line())
.nest(INDENT)
.append(self.expr(&first.body))
.append(line())
.append("} ")
.append(join(
branches[1..].iter().map(|branch| {
break_("else if", "else if ")
.append(self.wrap_expr(&branch.condition))
.nest(INDENT)
.append(break_("", " "))
.append("{")
.group()
.append(line())
.nest(INDENT)
.append(self.expr(&branch.body))
.append(line())
.append("} ")
}),
nil(),
))
.append("else {")
.group()
.append(line().nest(INDENT))
.append(self.expr(final_else))
.append(line())
.append("}")
.force_break()
}
UntypedExpr::Todo { label: None, .. } => "todo".to_doc(),
UntypedExpr::Todo { label: Some(l), .. } => docvec!["todo(\"", l, "\")"],

View File

@ -11,8 +11,11 @@ pub type Redeemer {
}
pub fn spend(datum: Datum, rdmr: Redeemer, ctx: spend.ScriptContext) -> Bool {
when rdmr is {
Buy -> True
Sell -> datum.something == "Aiken"
if datum.something == "Aiken" {
True
} else if datum.something == "Wow" {
True
} else {
False
}
}