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