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 ContractId {
|
|
ContractId(String)
|
|
}
|
|
|
|
pub const min_length: Int = 1
|
|
|
|
pub fn parse(value: String) -> Result(ContractId, Nil) {
|
|
case string.length(value) >= min_length {
|
|
True -> Ok(ContractId(value))
|
|
False -> Error(Nil)
|
|
}
|
|
}
|
|
|
|
pub fn decoder() -> Decoder(ContractId) {
|
|
use value <- decode.then(decode.string)
|
|
case parse(value) {
|
|
Ok(contract_id) -> decode.success(contract_id)
|
|
Error(Nil) -> decode.failure(ContractId("invalid"), "ContractId")
|
|
}
|
|
}
|
|
|
|
pub fn to_string(contract_id: ContractId) -> String {
|
|
let ContractId(symbol) = contract_id
|
|
symbol
|
|
}
|
|
|
|
pub fn encode(contract_id: ContractId) -> Json {
|
|
json.string(to_string(contract_id))
|
|
}
|