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