37 lines
817 B
Gleam
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))
|
|
}
|