gleam-spacetraders-sdk/src/models/transaction_type.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

34 lines
768 B
Gleam

import gleam/dynamic/decode.{type Decoder}
import gleam/json.{type Json}
pub type TransactionType {
Purchase
Sell
}
pub fn parse(value: String) -> Result(TransactionType, Nil) {
case value {
"PURCHASE" -> Ok(Purchase)
"SELL" -> Ok(Sell)
_ -> Error(Nil)
}
}
pub fn decoder() -> Decoder(TransactionType) {
use value <- decode.then(decode.string)
case parse(value) {
Ok(transaction_type) -> decode.success(transaction_type)
Error(Nil) -> decode.failure(Purchase, "TransactionType")
}
}
pub fn to_string(transaction_type: TransactionType) -> String {
case transaction_type {
Purchase -> "PURCHASE"
Sell -> "SELL"
}
}
pub fn encode(transaction_type: TransactionType) -> Json {
json.string(to_string(transaction_type))
}