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