33 lines
812 B
Gleam
33 lines
812 B
Gleam
import gleam/dynamic/decode.{type Decoder}
|
|
import gleam/json.{type Json}
|
|
import gleam/string
|
|
|
|
pub opaque type Constellation {
|
|
Constellation(String)
|
|
}
|
|
|
|
pub const min_length: Int = 1
|
|
|
|
pub fn parse(value: String) -> Result(Constellation, Nil) {
|
|
case string.length(value) >= min_length {
|
|
True -> Ok(Constellation(value))
|
|
False -> Error(Nil)
|
|
}
|
|
}
|
|
|
|
pub fn decoder() -> Decoder(Constellation) {
|
|
use value <- decode.then(decode.string)
|
|
case parse(value) {
|
|
Ok(constellation) -> decode.success(constellation)
|
|
Error(Nil) -> decode.failure(Constellation("invalid"), "Constellation")
|
|
}
|
|
}
|
|
|
|
pub fn to_string(constellation: Constellation) -> String {
|
|
let Constellation(value) = constellation
|
|
value
|
|
}
|
|
|
|
pub fn encode(constellation: Constellation) -> Json {
|
|
json.string(to_string(constellation))
|
|
}
|