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

35 lines
962 B
Gleam

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