gleam-spacetraders-sdk/src/models/supply_level.gleam
Lily Rose 64f3729d0c
Some checks are pending
test / test (push) Waiting to run
Refactoring and general tidying up
2025-06-17 19:04:29 +10:00

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))
}