Add name_string decoder

This commit is contained in:
Lily Rose 2025-07-24 17:45:33 +10:00
parent 7cc26adf58
commit a28955b789

View file

@ -84,6 +84,16 @@ pub fn string(then next: NextFn(String, a)) -> Decoder(a) {
}
}
pub fn name_string(then next: NextFn(String, a)) -> Decoder(a) {
fn(sexprs: List(SExpr)) {
case sexprs {
[] -> Error(UnexpectedEndOfAttributes(String))
[parse.Name(value), ..sexprs] -> next(value)(sexprs)
[sexpr, ..] -> Error(IncorrectExprType(got: sexpr, expected: String))
}
}
}
pub fn float(then next: NextFn(Float, a)) -> Decoder(a) {
fn(sexprs: List(SExpr)) {
case sexprs {
@ -254,10 +264,10 @@ pub fn failure(error: DecodeError) -> Decoder(a) {
}
pub fn run(
structurer: fn(NextFn(a, a)) -> Decoder(b),
decoder: fn(NextFn(a, a)) -> Decoder(b),
on source: SExpr,
) -> Result(b, DecodeError) {
use #(value, rest) <- result.try(structurer(success)([source]))
use #(value, rest) <- result.try(decoder(success)([source]))
use <- bool.guard(rest != [], Error(UnexpectedTrailingAttributes(rest)))
Ok(value)
}