37 lines
839 B
Gleam
37 lines
839 B
Gleam
import gleam/dynamic/decode.{type Decoder}
|
|
import gleam/json.{type Json}
|
|
|
|
pub type ContractType {
|
|
Procurement
|
|
Transport
|
|
Shuttle
|
|
}
|
|
|
|
pub fn parse(value: String) -> Result(ContractType, Nil) {
|
|
case value {
|
|
"PROCUREMENT" -> Ok(Procurement)
|
|
"TRANSPORT" -> Ok(Transport)
|
|
"SHUTTLE" -> Ok(Shuttle)
|
|
_ -> Error(Nil)
|
|
}
|
|
}
|
|
|
|
pub fn decoder() -> Decoder(ContractType) {
|
|
use value <- decode.then(decode.string)
|
|
case parse(value) {
|
|
Ok(contract_type) -> decode.success(contract_type)
|
|
Error(Nil) -> decode.failure(Procurement, "ContractType")
|
|
}
|
|
}
|
|
|
|
pub fn to_string(contract_type: ContractType) -> String {
|
|
case contract_type {
|
|
Procurement -> "PROCUREMENT"
|
|
Transport -> "TRANSPORT"
|
|
Shuttle -> "SHUTTLE"
|
|
}
|
|
}
|
|
|
|
pub fn encode(contract_type: ContractType) -> Json {
|
|
json.string(to_string(contract_type))
|
|
}
|