gleamgen/expression/case_

Types

pub type CaseExpression(input, output) {
  CaseExpression(
    input_expression: Expression(input),
    cases: List(
      #(Matcher(Unchecked, Unchecked), Expression(output)),
    ),
  )
}

Constructors

  • CaseExpression(
      input_expression: Expression(input),
      cases: List(
        #(Matcher(Unchecked, Unchecked), Expression(output)),
      ),
    )

Functions

pub fn build_expression(
  case_: CaseExpression(a, b),
) -> Expression(b)
pub fn new(
  input_expression: Expression(a),
) -> CaseExpression(a, b)

Create a new case expression that matches on the given input_expression

case_.new(expression.string("hello"))
|> case_.with_matcher(matcher.string_literal("hello"), fn(_) {
  expression.string("world")
})
|> case_.with_matcher(matcher.variable("v"), fn(v) {
  expression.concat_string(v, expression.string(" world"))
})
|> case_.build_expression()
|> expression.render(render.default_context())
|> render.to_string()

This creates:

case "hello" {
  "hello" -> "world"
  v -> v <> " world"
}

Note that providing a tuple literal as the case subject will expand into multiple subjects:

case_.new(expression.tuple2(expression.string("hello"), expression.int(3)))
|> case_.with_matcher(
  matcher.tuple2(matcher.string_literal("hello"), matcher.variable("_")),
  fn(_) { expression.string("world") },
)
|> case_.with_matcher(matcher.variable("_"), fn(_) {
  expression.string("other")
})
|> case_.build_expression()
|> expression.render(render.default_context())
|> render.to_string()

This creates:

case "hello", 3 {
  "hello", _ -> "world"
  _, _ -> "other"
}
pub fn with_matcher(
  old: CaseExpression(a, b),
  matcher: Matcher(a, c),
  handler: fn(c) -> Expression(b),
) -> CaseExpression(a, b)
Search Document