gleam-spacetraders-sdk/src/models/contract_type.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

37 lines
839 B
Gleam

import gleam/dynamic/decode.{type Decoder}
import gleam/json.{type Json}
pub type ContractType {
Procurement
Transport
Shuttle
}
pub fn parse(value: String) -> Result(ContractType, Nil) {
case value {
"PROCUREMENT" -> Ok(Procurement)
"TRANSPORT" -> Ok(Transport)
"SHUTTLE" -> Ok(Shuttle)
_ -> Error(Nil)
}
}
pub fn decoder() -> Decoder(ContractType) {
use value <- decode.then(decode.string)
case parse(value) {
Ok(contract_type) -> decode.success(contract_type)
Error(Nil) -> decode.failure(Procurement, "ContractType")
}
}
pub fn to_string(contract_type: ContractType) -> String {
case contract_type {
Procurement -> "PROCUREMENT"
Transport -> "TRANSPORT"
Shuttle -> "SHUTTLE"
}
}
pub fn encode(contract_type: ContractType) -> Json {
json.string(to_string(contract_type))
}