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