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

35 lines
962 B
Gleam

import gleam/dynamic/decode.{type Decoder}
import gleam/json.{type Json}
pub opaque type ShipComponentIntegrity {
ShipComponentIntegrity(Float)
}
pub const min: Float = 0.0
pub const max: Float = 1.0
pub fn parse(value: Float) -> Result(ShipComponentIntegrity, Nil) {
case value >=. min && value <=. max {
True -> Ok(ShipComponentIntegrity(value))
False -> Error(Nil)
}
}
pub fn decoder() -> Decoder(ShipComponentIntegrity) {
use value <- decode.then(decode.float)
case parse(value) {
Ok(ship_component_integrity) -> decode.success(ship_component_integrity)
Error(Nil) ->
decode.failure(ShipComponentIntegrity(0.0), "ShipComponentIntegrity")
}
}
pub fn to_float(ship_component_integrity: ShipComponentIntegrity) -> Float {
let ShipComponentIntegrity(value) = ship_component_integrity
value
}
pub fn encode(ship_component_integrity: ShipComponentIntegrity) -> Json {
json.float(to_float(ship_component_integrity))
}