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

33 lines
812 B
Gleam

import gleam/dynamic/decode.{type Decoder}
import gleam/json.{type Json}
import gleam/string
pub opaque type Constellation {
Constellation(String)
}
pub const min_length: Int = 1
pub fn parse(value: String) -> Result(Constellation, Nil) {
case string.length(value) >= min_length {
True -> Ok(Constellation(value))
False -> Error(Nil)
}
}
pub fn decoder() -> Decoder(Constellation) {
use value <- decode.then(decode.string)
case parse(value) {
Ok(constellation) -> decode.success(constellation)
Error(Nil) -> decode.failure(Constellation("invalid"), "Constellation")
}
}
pub fn to_string(constellation: Constellation) -> String {
let Constellation(value) = constellation
value
}
pub fn encode(constellation: Constellation) -> Json {
json.string(to_string(constellation))
}