Add new decoder combinators

This commit is contained in:
LilyRose2798 2025-07-24 19:53:41 +10:00
parent 0a3a8fc81a
commit 421924a339

View file

@ -84,7 +84,7 @@ pub fn string(then next: NextFn(String, a)) -> Decoder(a) {
} }
} }
pub fn name_string(then next: NextFn(String, a)) -> Decoder(a) { pub fn name(then next: NextFn(String, a)) -> Decoder(a) {
fn(sexprs: List(SExpr)) { fn(sexprs: List(SExpr)) {
case sexprs { case sexprs {
[] -> Error(UnexpectedEndOfAttributes(String)) [] -> Error(UnexpectedEndOfAttributes(String))
@ -94,7 +94,7 @@ pub fn name_string(then next: NextFn(String, a)) -> Decoder(a) {
} }
} }
pub fn name_or_string(then next: NextFn(String, a)) -> Decoder(a) { pub fn name_string(then next: NextFn(String, a)) -> Decoder(a) {
fn(sexprs: List(SExpr)) { fn(sexprs: List(SExpr)) {
case sexprs { case sexprs {
[] -> Error(UnexpectedEndOfAttributes(String)) [] -> Error(UnexpectedEndOfAttributes(String))
@ -105,6 +105,31 @@ pub fn name_or_string(then next: NextFn(String, a)) -> Decoder(a) {
} }
} }
pub fn name_number(then next: NextFn(String, a)) -> Decoder(a) {
fn(sexprs: List(SExpr)) {
case sexprs {
[] -> Error(UnexpectedEndOfAttributes(String))
[parse.Name(value), ..sexprs] -> next(value)(sexprs)
[parse.Int(value), ..sexprs] -> next(int.to_string(value))(sexprs)
[parse.Float(value), ..sexprs] -> next(float.to_string(value))(sexprs)
[sexpr, ..] -> Error(IncorrectExprType(got: sexpr, expected: String))
}
}
}
pub fn name_number_string(then next: NextFn(String, a)) -> Decoder(a) {
fn(sexprs: List(SExpr)) {
case sexprs {
[] -> Error(UnexpectedEndOfAttributes(String))
[parse.Name(value), ..sexprs] | [parse.String(value), ..sexprs] ->
next(value)(sexprs)
[parse.Int(value), ..sexprs] -> next(int.to_string(value))(sexprs)
[parse.Float(value), ..sexprs] -> next(float.to_string(value))(sexprs)
[sexpr, ..] -> Error(IncorrectExprType(got: sexpr, expected: String))
}
}
}
pub fn float(then next: NextFn(Float, a)) -> Decoder(a) { pub fn float(then next: NextFn(Float, a)) -> Decoder(a) {
fn(sexprs: List(SExpr)) { fn(sexprs: List(SExpr)) {
case sexprs { case sexprs {