43 lines
910 B
Gleam
43 lines
910 B
Gleam
import gleam/dynamic/decode.{type Decoder}
|
|
import gleam/json.{type Json}
|
|
|
|
pub type SupplyLevel {
|
|
Scarce
|
|
Limited
|
|
Moderate
|
|
High
|
|
Abundant
|
|
}
|
|
|
|
pub fn parse(value: String) -> Result(SupplyLevel, Nil) {
|
|
case value {
|
|
"SCARCE" -> Ok(Scarce)
|
|
"LIMITED" -> Ok(Limited)
|
|
"MODERATE" -> Ok(Moderate)
|
|
"HIGH" -> Ok(High)
|
|
"ABUNDANT" -> Ok(Abundant)
|
|
_ -> Error(Nil)
|
|
}
|
|
}
|
|
|
|
pub fn decoder() -> Decoder(SupplyLevel) {
|
|
use value <- decode.then(decode.string)
|
|
case parse(value) {
|
|
Ok(supply_level) -> decode.success(supply_level)
|
|
Error(Nil) -> decode.failure(Scarce, "SupplyLevel")
|
|
}
|
|
}
|
|
|
|
pub fn to_string(supply_level: SupplyLevel) -> String {
|
|
case supply_level {
|
|
Scarce -> "SCARCE"
|
|
Limited -> "LIMITED"
|
|
Moderate -> "MODERATE"
|
|
High -> "HIGH"
|
|
Abundant -> "ABUNDANT"
|
|
}
|
|
}
|
|
|
|
pub fn encode(supply_level: SupplyLevel) -> Json {
|
|
json.string(to_string(supply_level))
|
|
}
|