import gleam/dynamic/decode import gleam/json pub type Priority { High Medium Low } pub fn to_string(value: Priority) { case value { High -> "high" Low -> "low" Medium -> "medium" } } pub fn to_string_pt_br(value: Priority) { case value { High -> "alta" Low -> "baixa" Medium -> "média" } } pub fn to_json(priority: Priority) -> json.Json { to_string(priority) |> json.string } pub fn from_string(maybe_priority: String) -> Result(Priority, Nil) { case maybe_priority { "high" -> Ok(High) "medium" -> Ok(Medium) "low" -> Ok(Low) _ -> Error(Nil) } } pub fn from_string_pt_br(maybe_priority: String) -> Result(Priority, Nil) { case maybe_priority { "alta" -> Ok(High) "média" -> Ok(Medium) "baixa" -> Ok(Low) _ -> Error(Nil) } } pub fn decoder() -> decode.Decoder(Priority) { use prioriry_string <- decode.then(decode.string) case from_string(prioriry_string) { Error(_) -> decode.failure(Low, "priority") Ok(value) -> decode.success(value) } } pub fn decoder_pt_br() -> decode.Decoder(Priority) { use prioriry_string <- decode.then(decode.string) case from_string_pt_br(prioriry_string) { Error(_) -> decode.failure(Low, "priority") Ok(value) -> decode.success(value) } }