diff --git a/src/kicad_sexpr/decode.gleam b/src/kicad_sexpr/decode.gleam index 89c9c88..acab231 100644 --- a/src/kicad_sexpr/decode.gleam +++ b/src/kicad_sexpr/decode.gleam @@ -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)) { case sexprs { [] -> 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)) { case sexprs { [] -> 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) { fn(sexprs: List(SExpr)) { case sexprs {