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

58 lines
1.3 KiB
Gleam

import gleam/dynamic/decode.{type Decoder}
import gleam/json.{type Json}
pub type SystemType {
NeutronStar
RedStar
OrangeStar
BlueStar
YoungStar
WhiteDwarf
BlackHole
Hypergiant
Nebula
Unstable
}
pub fn parse(value: String) -> Result(SystemType, Nil) {
case value {
"NEUTRON_STAR" -> Ok(NeutronStar)
"RED_STAR" -> Ok(RedStar)
"ORANGE_STAR" -> Ok(OrangeStar)
"BLUE_STAR" -> Ok(BlueStar)
"YOUNG_STAR" -> Ok(YoungStar)
"WHITE_DWARF" -> Ok(WhiteDwarf)
"BLACK_HOLE" -> Ok(BlackHole)
"HYPERGIANT" -> Ok(Hypergiant)
"NEBULA" -> Ok(Nebula)
"UNSTABLE" -> Ok(Unstable)
_ -> Error(Nil)
}
}
pub fn decoder() -> Decoder(SystemType) {
use value <- decode.then(decode.string)
case parse(value) {
Ok(system_type) -> decode.success(system_type)
Error(Nil) -> decode.failure(NeutronStar, "SystemType")
}
}
pub fn to_string(system_type: SystemType) -> String {
case system_type {
NeutronStar -> "NEUTRON_STAR"
RedStar -> "RED_STAR"
OrangeStar -> "ORANGE_STAR"
BlueStar -> "BLUE_STAR"
YoungStar -> "YOUNG_STAR"
WhiteDwarf -> "WHITE_DWARF"
BlackHole -> "BLACK_HOLE"
Hypergiant -> "HYPERGIANT"
Nebula -> "NEBULA"
Unstable -> "UNSTABLE"
}
}
pub fn encode(system_type: SystemType) -> Json {
json.string(to_string(system_type))
}