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) }
|
fn(_: List(SExpr)) { Error(error) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(
|
pub fn run(decoder: Decoder(a), on source: SExpr) -> Result(a, DecodeError) {
|
||||||
decoder: fn(NextFn(a, a)) -> Decoder(b),
|
use #(value, rest) <- result.try(decoder([source]))
|
||||||
on source: SExpr,
|
|
||||||
) -> Result(b, DecodeError) {
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import gleam/string
|
||||||
import gleam/time/duration
|
import gleam/time/duration
|
||||||
import gleam/time/timestamp.{type Timestamp}
|
import gleam/time/timestamp.{type Timestamp}
|
||||||
import gleeunit
|
import gleeunit
|
||||||
import kicad_sexpr/decode.{type Decoder, type NextFn}
|
import kicad_sexpr/decode
|
||||||
import kicad_sexpr/parse
|
import kicad_sexpr/parse
|
||||||
import kicad_sexpr/token
|
import kicad_sexpr/token
|
||||||
import simplifile
|
import simplifile
|
||||||
|
@ -23,7 +23,11 @@ pub fn main() -> Nil {
|
||||||
// |> list.take(1000)
|
// |> list.take(1000)
|
||||||
// |> list.sample(1000)
|
// |> list.sample(1000)
|
||||||
// let footprint_files = ["test_files/test3.kicad_mod"]
|
// 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")
|
io.println("\nTesting Symbol Libraries")
|
||||||
let assert Ok(symbol_libraries) =
|
let assert Ok(symbol_libraries) =
|
||||||
|
@ -35,7 +39,11 @@ pub fn main() -> Nil {
|
||||||
// |> list.take(20)
|
// |> list.take(20)
|
||||||
// |> list.sample(20)
|
// |> list.sample(20)
|
||||||
// let symbol_libraries = ["test_files/test3.kicad_mod"]
|
// 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()
|
gleeunit.main()
|
||||||
}
|
}
|
||||||
|
@ -78,7 +86,7 @@ fn print_stats(
|
||||||
|
|
||||||
fn test_read_parse_decode(
|
fn test_read_parse_decode(
|
||||||
file_names: List(String),
|
file_names: List(String),
|
||||||
decoder: fn(NextFn(a, a)) -> Decoder(a),
|
decoder: decode.Decoder(a),
|
||||||
print_errors: Bool,
|
print_errors: Bool,
|
||||||
) -> Nil {
|
) -> Nil {
|
||||||
let num_file_names = list.length(file_names)
|
let num_file_names = list.length(file_names)
|
||||||
|
|
Loading…
Reference in a new issue