gleam-spacetraders-sdk/src/spacetraders_models/account_id.gleam

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))
}