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