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