58 lines
1.3 KiB
Gleam
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))
|
|
}
|