34 lines
735 B
Gleam
34 lines
735 B
Gleam
import gleam/dynamic/decode.{type Decoder}
|
|
import gleam/json.{type Json}
|
|
|
|
pub type CrewRotation {
|
|
Strict
|
|
Relaxed
|
|
}
|
|
|
|
pub fn parse(value: String) -> Result(CrewRotation, Nil) {
|
|
case value {
|
|
"STRICT" -> Ok(Strict)
|
|
"RELAXED" -> Ok(Relaxed)
|
|
_ -> Error(Nil)
|
|
}
|
|
}
|
|
|
|
pub fn decoder() -> Decoder(CrewRotation) {
|
|
use value <- decode.then(decode.string)
|
|
case parse(value) {
|
|
Ok(crew_rotation) -> decode.success(crew_rotation)
|
|
Error(Nil) -> decode.failure(Strict, "CrewRotation")
|
|
}
|
|
}
|
|
|
|
pub fn to_string(crew_rotation: CrewRotation) -> String {
|
|
case crew_rotation {
|
|
Strict -> "STRICT"
|
|
Relaxed -> "RELAXED"
|
|
}
|
|
}
|
|
|
|
pub fn encode(crew_rotation: CrewRotation) -> Json {
|
|
json.string(to_string(crew_rotation))
|
|
}
|