This commit is contained in:
parent
8cc0640d6b
commit
54ae06ff67
3 changed files with 42 additions and 9 deletions
|
@ -1 +1,29 @@
|
|||
import gleam/result
|
||||
import kicad_sexpr/decode
|
||||
import kicad_sexpr/parse
|
||||
import kicad_sexpr/token
|
||||
|
||||
pub type ParseDecodeError {
|
||||
ParseError(parse.ParseError)
|
||||
DecodeError(decode.DecodeError)
|
||||
}
|
||||
|
||||
pub fn parse_decode(
|
||||
data: BitArray,
|
||||
decoder: decode.Decoder(a),
|
||||
) -> Result(a, ParseDecodeError) {
|
||||
use sexpr <- result.try(parse.run(data) |> result.map_error(ParseError))
|
||||
decode.run(decoder, sexpr) |> result.map_error(DecodeError)
|
||||
}
|
||||
|
||||
pub fn parse_footprint_file(
|
||||
data: BitArray,
|
||||
) -> Result(token.FootprintFile, ParseDecodeError) {
|
||||
parse_decode(data, token.footprint_file(decode.success))
|
||||
}
|
||||
|
||||
pub fn parse_symbol_library(
|
||||
data: BitArray,
|
||||
) -> Result(token.SymbolLibrary, ParseDecodeError) {
|
||||
parse_decode(data, token.symbol_library(decode.success))
|
||||
}
|
||||
|
|
|
@ -299,11 +299,8 @@ pub fn failure(error: DecodeError) -> Decoder(a) {
|
|||
fn(_: List(SExpr)) { Error(error) }
|
||||
}
|
||||
|
||||
pub fn run(
|
||||
decoder: fn(NextFn(a, a)) -> Decoder(b),
|
||||
on source: SExpr,
|
||||
) -> Result(b, DecodeError) {
|
||||
use #(value, rest) <- result.try(decoder(success)([source]))
|
||||
pub fn run(decoder: Decoder(a), on source: SExpr) -> Result(a, DecodeError) {
|
||||
use #(value, rest) <- result.try(decoder([source]))
|
||||
use <- bool.guard(rest != [], Error(UnexpectedTrailingAttributes(rest)))
|
||||
Ok(value)
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import gleam/string
|
|||
import gleam/time/duration
|
||||
import gleam/time/timestamp.{type Timestamp}
|
||||
import gleeunit
|
||||
import kicad_sexpr/decode.{type Decoder, type NextFn}
|
||||
import kicad_sexpr/decode
|
||||
import kicad_sexpr/parse
|
||||
import kicad_sexpr/token
|
||||
import simplifile
|
||||
|
@ -23,7 +23,11 @@ pub fn main() -> Nil {
|
|||
// |> list.take(1000)
|
||||
// |> list.sample(1000)
|
||||
// let footprint_files = ["test_files/test3.kicad_mod"]
|
||||
test_read_parse_decode(footprint_files, token.footprint_file, True)
|
||||
test_read_parse_decode(
|
||||
footprint_files,
|
||||
token.footprint_file(decode.success),
|
||||
True,
|
||||
)
|
||||
|
||||
io.println("\nTesting Symbol Libraries")
|
||||
let assert Ok(symbol_libraries) =
|
||||
|
@ -35,7 +39,11 @@ pub fn main() -> Nil {
|
|||
// |> list.take(20)
|
||||
// |> list.sample(20)
|
||||
// let symbol_libraries = ["test_files/test3.kicad_mod"]
|
||||
test_read_parse_decode(symbol_libraries, token.symbol_library, True)
|
||||
test_read_parse_decode(
|
||||
symbol_libraries,
|
||||
token.symbol_library(decode.success),
|
||||
True,
|
||||
)
|
||||
|
||||
gleeunit.main()
|
||||
}
|
||||
|
@ -78,7 +86,7 @@ fn print_stats(
|
|||
|
||||
fn test_read_parse_decode(
|
||||
file_names: List(String),
|
||||
decoder: fn(NextFn(a, a)) -> Decoder(a),
|
||||
decoder: decode.Decoder(a),
|
||||
print_errors: Bool,
|
||||
) -> Nil {
|
||||
let num_file_names = list.length(file_names)
|
||||
|
|
Loading…
Reference in a new issue