Skip to content

Commit

Permalink
feat(template.mustache): prettier syntax :3 (+ handle non-existent ed…
Browse files Browse the repository at this point in the history
…ge case)
  • Loading branch information
loqusion committed Apr 8, 2024
1 parent 712acee commit bd58938
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/hyprshade/template/mustache.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ def render(


def nullish_coalesce(text: str, render: Callable[[str], str]):
lhs, *rhs = NULLISH_COALESCE_OPERATOR_PATTERN.split(text)
match len(rhs):
case 0:
match NULLISH_COALESCE_OPERATOR_PATTERN.split(text):
case [_]:
raise ValueError(
"Mustache nullish coalesce operator requires a default value."
)
case 1:
case [lhs, rhs]:
rendered_lhs = render(lhs)
return rendered_lhs if rendered_lhs.strip() else render(rhs[0])
case _:
return rendered_lhs if rendered_lhs.strip() else render(rhs)
case [_, _, *_]:
raise ValueError(
"Mustache nullish coalesce operator must occur only once in an expression."
)
case _:
raise ValueError("Mustache nullish coalesce operator is not valid.")


NULLISH_COALESCE_LAMBDA_NAME: Final = "d"
Expand Down

0 comments on commit bd58938

Please sign in to comment.