gleam-spacetraders-models/src/spacetraders_models/trade_good_type.gleam
Lily Rose fab4c9df5d
Some checks are pending
test / test (push) Waiting to run
Initial commit
2025-07-08 23:03:42 +10:00

37 lines
817 B
Gleam

import gleam/dynamic/decode.{type Decoder}
import gleam/json.{type Json}
pub type TradeGoodType {
Import
Export
Exchange
}
pub fn parse(value: String) -> Result(TradeGoodType, Nil) {
case value {
"IMPORT" -> Ok(Import)
"EXPORT" -> Ok(Export)
"EXCHANGE" -> Ok(Exchange)
_ -> Error(Nil)
}
}
pub fn decoder() -> Decoder(TradeGoodType) {
use value <- decode.then(decode.string)
case parse(value) {
Ok(trade_good_type) -> decode.success(trade_good_type)
Error(Nil) -> decode.failure(Import, "TradeGoodType")
}
}
pub fn to_string(trade_good_type: TradeGoodType) -> String {
case trade_good_type {
Import -> "IMPORT"
Export -> "EXPORT"
Exchange -> "EXCHANGE"
}
}
pub fn encode(trade_good_type: TradeGoodType) -> Json {
json.string(to_string(trade_good_type))
}