wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam
at main 65 lines 1.3 kB view raw
1import gleam/dynamic/decode 2import gleam/json 3 4pub type Priority { 5 High 6 Medium 7 Low 8} 9 10pub fn to_string(value: Priority) { 11 case value { 12 High -> "high" 13 Low -> "low" 14 Medium -> "medium" 15 } 16} 17 18pub fn to_string_pt_br(value: Priority) { 19 case value { 20 High -> "alta" 21 Low -> "baixa" 22 Medium -> "média" 23 } 24} 25 26pub fn to_json(priority: Priority) -> json.Json { 27 to_string(priority) 28 |> json.string 29} 30 31pub fn from_string(maybe_priority: String) -> Result(Priority, Nil) { 32 case maybe_priority { 33 "high" -> Ok(High) 34 "medium" -> Ok(Medium) 35 "low" -> Ok(Low) 36 37 _ -> Error(Nil) 38 } 39} 40 41pub fn from_string_pt_br(maybe_priority: String) -> Result(Priority, Nil) { 42 case maybe_priority { 43 "alta" -> Ok(High) 44 "média" -> Ok(Medium) 45 "baixa" -> Ok(Low) 46 47 _ -> Error(Nil) 48 } 49} 50 51pub fn decoder() -> decode.Decoder(Priority) { 52 use prioriry_string <- decode.then(decode.string) 53 case from_string(prioriry_string) { 54 Error(_) -> decode.failure(Low, "priority") 55 Ok(value) -> decode.success(value) 56 } 57} 58 59pub fn decoder_pt_br() -> decode.Decoder(Priority) { 60 use prioriry_string <- decode.then(decode.string) 61 case from_string_pt_br(prioriry_string) { 62 Error(_) -> decode.failure(Low, "priority") 63 Ok(value) -> decode.success(value) 64 } 65}