33 lines
804 B
Gleam
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))
|
|
}
|