40 lines
909 B
Gleam
40 lines
909 B
Gleam
import gleam/dynamic/decode.{type Decoder}
|
|
import gleam/json.{type Json}
|
|
|
|
pub type ShipNavFlightMode {
|
|
Drift
|
|
Stealth
|
|
Cruise
|
|
Burn
|
|
}
|
|
|
|
pub fn parse(value: String) -> Result(ShipNavFlightMode, Nil) {
|
|
case value {
|
|
"DRIFT" -> Ok(Drift)
|
|
"STEALTH" -> Ok(Stealth)
|
|
"CRUISE" -> Ok(Cruise)
|
|
"BURN" -> Ok(Burn)
|
|
_ -> Error(Nil)
|
|
}
|
|
}
|
|
|
|
pub fn decoder() -> Decoder(ShipNavFlightMode) {
|
|
use value <- decode.then(decode.string)
|
|
case parse(value) {
|
|
Ok(ship_nav_flight_mode) -> decode.success(ship_nav_flight_mode)
|
|
Error(Nil) -> decode.failure(Drift, "ShipNavFlightMode")
|
|
}
|
|
}
|
|
|
|
pub fn to_string(ship_nav_flight_mode: ShipNavFlightMode) -> String {
|
|
case ship_nav_flight_mode {
|
|
Drift -> "DRIFT"
|
|
Stealth -> "STEALTH"
|
|
Cruise -> "CRUISE"
|
|
Burn -> "BURN"
|
|
}
|
|
}
|
|
|
|
pub fn encode(ship_nav_flight_mode: ShipNavFlightMode) -> Json {
|
|
json.string(to_string(ship_nav_flight_mode))
|
|
}
|