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

43 lines
1 KiB
Gleam

import gleam/dynamic/decode.{type Decoder}
import gleam/json.{type Json}
pub type ShipType {
ShipProbe
ShipMiningDrone
ShipSiphonDrone
ShipInterceptor
ShipLightHauler
}
pub fn parse(value: String) -> Result(ShipType, Nil) {
case value {
"SHIP_PROBE" -> Ok(ShipProbe)
"SHIP_MINING_DRONE" -> Ok(ShipMiningDrone)
"SHIP_SIPHON_DRONE" -> Ok(ShipSiphonDrone)
"SHIP_INTERCEPTOR" -> Ok(ShipInterceptor)
"SHIP_LIGHT_HAULER" -> Ok(ShipLightHauler)
_ -> Error(Nil)
}
}
pub fn decoder() -> Decoder(ShipType) {
use value <- decode.then(decode.string)
case parse(value) {
Ok(ship_type) -> decode.success(ship_type)
Error(Nil) -> decode.failure(ShipProbe, "ShipType")
}
}
pub fn to_string(ship_type: ShipType) -> String {
case ship_type {
ShipProbe -> "SHIP_PROBE"
ShipMiningDrone -> "SHIP_MINING_DRONE"
ShipSiphonDrone -> "SHIP_SIPHON_DRONE"
ShipInterceptor -> "SHIP_INTERCEPTOR"
ShipLightHauler -> "SHIP_LIGHT_HAULER"
}
}
pub fn encode(ship_type: ShipType) -> Json {
json.string(to_string(ship_type))
}