gleam-spacetraders-sdk/src/models/account_id.gleam
Lily Rose 64f3729d0c
Some checks are pending
test / test (push) Waiting to run
Refactoring and general tidying up
2025-06-17 19:04:29 +10:00

33 lines
756 B
Gleam

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