gleam-spacetraders-sdk/src/models/system_symbol.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
804 B
Gleam

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