70 lines
1.8 KiB
Gleam
70 lines
1.8 KiB
Gleam
import gleam/dynamic/decode.{type Decoder}
|
|
import gleam/json.{type Json}
|
|
|
|
pub type WaypointType {
|
|
Planet
|
|
GasGiant
|
|
Moon
|
|
OrbitalStation
|
|
JumpGate
|
|
AsteroidField
|
|
Asteroid
|
|
EngineeredAsteroid
|
|
AsteroidBase
|
|
Nebula
|
|
DebrisField
|
|
GravityWell
|
|
ArtificialGravityWell
|
|
FuelStation
|
|
}
|
|
|
|
pub fn parse(value: String) -> Result(WaypointType, Nil) {
|
|
case value {
|
|
"PLANET" -> Ok(Planet)
|
|
"GAS_GIANT" -> Ok(GasGiant)
|
|
"MOON" -> Ok(Moon)
|
|
"ORBITAL_STATION" -> Ok(OrbitalStation)
|
|
"JUMP_GATE" -> Ok(JumpGate)
|
|
"ASTEROID_FIELD" -> Ok(AsteroidField)
|
|
"ASTEROID" -> Ok(Asteroid)
|
|
"ENGINEERED_ASTEROID" -> Ok(EngineeredAsteroid)
|
|
"ASTEROID_BASE" -> Ok(AsteroidBase)
|
|
"NEBULA" -> Ok(Nebula)
|
|
"DEBRIS_FIELD" -> Ok(DebrisField)
|
|
"GRAVITY_WELL" -> Ok(GravityWell)
|
|
"ARTIFICIAL_GRAVITY_WELL" -> Ok(ArtificialGravityWell)
|
|
"FUEL_STATION" -> Ok(FuelStation)
|
|
_ -> Error(Nil)
|
|
}
|
|
}
|
|
|
|
pub fn decoder() -> Decoder(WaypointType) {
|
|
use value <- decode.then(decode.string)
|
|
case parse(value) {
|
|
Ok(waypoint_type) -> decode.success(waypoint_type)
|
|
Error(Nil) -> decode.failure(Planet, "WaypointType")
|
|
}
|
|
}
|
|
|
|
pub fn to_string(waypoint_type: WaypointType) -> String {
|
|
case waypoint_type {
|
|
Planet -> "PLANET"
|
|
GasGiant -> "GAS_GIANT"
|
|
Moon -> "MOON"
|
|
OrbitalStation -> "ORBITAL_STATION"
|
|
JumpGate -> "JUMP_GATE"
|
|
AsteroidField -> "ASTEROID_FIELD"
|
|
Asteroid -> "ASTEROID"
|
|
EngineeredAsteroid -> "ENGINEERED_ASTEROID"
|
|
AsteroidBase -> "ASTEROID_BASE"
|
|
Nebula -> "NEBULA"
|
|
DebrisField -> "DEBRIS_FIELD"
|
|
GravityWell -> "GRAVITY_WELL"
|
|
ArtificialGravityWell -> "ARTIFICIAL_GRAVITY_WELL"
|
|
FuelStation -> "FUEL_STATION"
|
|
}
|
|
}
|
|
|
|
pub fn encode(waypoint_type: WaypointType) -> Json {
|
|
json.string(to_string(waypoint_type))
|
|
}
|