40 lines
1.3 KiB
Gleam
40 lines
1.3 KiB
Gleam
import birl.{type Time}
|
|
import gleam/dynamic/decode.{type Decoder}
|
|
import gleam/json.{type Json}
|
|
import models/survey_deposit.{type SurveyDeposit}
|
|
import models/survey_signature.{type SurveySignature}
|
|
import models/survey_size.{type SurveySize}
|
|
import models/waypoint_symbol.{type WaypointSymbol}
|
|
import utils/api
|
|
|
|
pub type Survey {
|
|
Survey(
|
|
signature: SurveySignature,
|
|
symbol: WaypointSymbol,
|
|
deposits: List(SurveyDeposit),
|
|
expiration: Time,
|
|
size: SurveySize,
|
|
)
|
|
}
|
|
|
|
pub fn decoder() -> Decoder(Survey) {
|
|
use signature <- decode.field("signature", survey_signature.decoder())
|
|
use symbol <- decode.field("symbol", waypoint_symbol.decoder())
|
|
use deposits <- decode.field(
|
|
"deposits",
|
|
decode.list(survey_deposit.decoder()),
|
|
)
|
|
use expiration <- decode.field("expiration", api.time_decoder())
|
|
use size <- decode.field("size", survey_size.decoder())
|
|
decode.success(Survey(signature:, symbol:, deposits:, expiration:, size:))
|
|
}
|
|
|
|
pub fn encode(survey: Survey) -> Json {
|
|
json.object([
|
|
#("signature", survey_signature.encode(survey.signature)),
|
|
#("symbol", waypoint_symbol.encode(survey.symbol)),
|
|
#("deposits", json.array(survey.deposits, survey_deposit.encode)),
|
|
#("expiration", json.string(birl.to_iso8601(survey.expiration))),
|
|
#("size", survey_size.encode(survey.size)),
|
|
])
|
|
}
|