35 lines
962 B
Gleam
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))
|
|
}
|