Add name_string decoder
This commit is contained in:
parent
7cc26adf58
commit
a28955b789
1 changed files with 12 additions and 2 deletions
|
@ -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) {
|
pub fn float(then next: NextFn(Float, a)) -> Decoder(a) {
|
||||||
fn(sexprs: List(SExpr)) {
|
fn(sexprs: List(SExpr)) {
|
||||||
case sexprs {
|
case sexprs {
|
||||||
|
@ -254,10 +264,10 @@ pub fn failure(error: DecodeError) -> Decoder(a) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(
|
pub fn run(
|
||||||
structurer: fn(NextFn(a, a)) -> Decoder(b),
|
decoder: fn(NextFn(a, a)) -> Decoder(b),
|
||||||
on source: SExpr,
|
on source: SExpr,
|
||||||
) -> Result(b, DecodeError) {
|
) -> 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)))
|
use <- bool.guard(rest != [], Error(UnexpectedTrailingAttributes(rest)))
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue