From 232512ad8da0e0a691f7ceba4f78fd42f080ceac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 7 May 2025 12:31:59 +0200 Subject: [PATCH 1/7] add functionality to export as JSON --- Cargo.lock | 329 ++++++++++++++++++++++++++++++++++++++++------------ Cargo.toml | 10 +- src/main.rs | 65 +++++++++-- 3 files changed, 318 insertions(+), 86 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9909fac..665ad9a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,17 @@ dependencies = [ "pom", ] +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + [[package]] name = "aho-corasick" version = "1.1.3" @@ -67,12 +78,13 @@ dependencies = [ [[package]] name = "anstyle-wincon" -version = "3.0.4" +version = "3.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" dependencies = [ "anstyle", - "windows-sys 0.52.0", + "once_cell", + "windows-sys 0.59.0", ] [[package]] @@ -100,9 +112,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.6.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" [[package]] name = "block-buffer" @@ -113,6 +125,30 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-padding" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bytecount" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce" + +[[package]] +name = "cbc" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" +dependencies = [ + "cipher", +] + [[package]] name = "cff-parser" version = "0.1.0" @@ -125,6 +161,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "clap" version = "4.5.37" @@ -192,6 +238,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.4.2" @@ -236,15 +291,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "deranged" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" -dependencies = [ - "powerfmt", -] - [[package]] name = "digest" version = "0.10.7" @@ -261,6 +307,15 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" +[[package]] +name = "ecb" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a8bfa975b1aec2145850fcaa1c6fe269a16578c44705a532ae3edc92b8881c7" +dependencies = [ + "cipher", +] + [[package]] name = "either" version = "1.13.0" @@ -345,6 +400,18 @@ dependencies = [ "version_check", ] +[[package]] +name = "getrandom" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + [[package]] name = "glob" version = "0.3.1" @@ -389,15 +456,24 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "block-padding", + "generic-array", +] + [[package]] name = "insta" -version = "1.41.1" +version = "1.43.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8" +checksum = "154934ea70c58054b556dd430b99a98c2a7ff5309ac9891597e339b5c28f4371" dependencies = [ "console", - "lazy_static", - "linked-hash-map", + "once_cell", "serde", "similar", "toml", @@ -436,6 +512,7 @@ dependencies = [ "insta-cmd", "pdf-extract", "regex", + "serde_json", "trycmd", ] @@ -462,12 +539,6 @@ dependencies = [ "redox_syscall", ] -[[package]] -name = "linked-hash-map" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" - [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -482,10 +553,14 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "lopdf" -version = "0.34.0" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5c8ecfc6c72051981c0459f75ccc585e7ff67c70829560cda8e647882a9abff" +checksum = "59fa2559e99ba0f26a12458aabc754432c805bbb8cba516c427825a997af1fb7" dependencies = [ + "aes", + "bitflags", + "cbc", + "ecb", "encoding_rs", "flate2", "indexmap", @@ -493,8 +568,12 @@ dependencies = [ "log", "md-5", "nom", + "nom_locate", + "rand", "rangemap", - "time", + "sha2", + "stringprep", + "thiserror", "weezl", ] @@ -514,12 +593,6 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - [[package]] name = "miniz_oxide" version = "0.8.0" @@ -531,25 +604,29 @@ dependencies = [ [[package]] name = "nom" -version = "7.1.3" +version = "8.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" dependencies = [ "memchr", - "minimal-lexical", ] [[package]] -name = "normalize-line-endings" -version = "0.3.0" +name = "nom_locate" +version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" +checksum = "0b577e2d69827c4740cba2b52efaad1c4cc7c73042860b199710b3575c68438d" +dependencies = [ + "bytecount", + "memchr", + "nom", +] [[package]] -name = "num-conv" -version = "0.1.0" +name = "normalize-line-endings" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" [[package]] name = "num-traits" @@ -562,9 +639,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.19.0" +version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "os_pipe" @@ -578,14 +655,15 @@ dependencies = [ [[package]] name = "pdf-extract" -version = "0.8.2" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87aa267a18864f2f75471f6d316ea430f13e78f0b5a882ce261ebbdfd389a76a" +checksum = "6c2f44c6c642e359e2fe7f662bf5438db3811b6b4be60afc6de04b619ce51e1a" dependencies = [ "adobe-cmap-parser", "cff-parser", "encoding_rs", "euclid", + "log", "lopdf", "postscript", "type1-encoding-parser", @@ -605,16 +683,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78451badbdaebaf17f053fd9152b3ffb33b516104eacb45e7864aaa9c712f306" [[package]] -name = "powerfmt" -version = "0.2.0" +name = "ppv-lite86" +version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" dependencies = [ "unicode-ident", ] @@ -628,6 +709,41 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" + +[[package]] +name = "rand" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom", +] + [[package]] name = "rangemap" version = "1.5.1" @@ -656,9 +772,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.3" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1" dependencies = [ "bitflags", ] @@ -761,6 +877,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shlex" version = "1.3.0" @@ -804,6 +931,17 @@ dependencies = [ "anstream", ] +[[package]] +name = "stringprep" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", + "unicode-properties", +] + [[package]] name = "strsim" version = "0.11.1" @@ -812,9 +950,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.76" +version = "2.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525" +checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" dependencies = [ "proc-macro2", "quote", @@ -835,34 +973,23 @@ dependencies = [ ] [[package]] -name = "time" -version = "0.3.36" +name = "thiserror" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" dependencies = [ - "deranged", - "itoa", - "num-conv", - "powerfmt", - "serde", - "time-core", - "time-macros", + "thiserror-impl", ] [[package]] -name = "time-core" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" - -[[package]] -name = "time-macros" -version = "0.2.18" +name = "thiserror-impl" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" dependencies = [ - "num-conv", - "time-core", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -944,6 +1071,12 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicode-bidi" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -959,6 +1092,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-properties" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e70f2a8b45122e719eb623c01822704c4e0907e7e426a05927e1a1cfff5b75d0" + [[package]] name = "utf8parse" version = "0.2.2" @@ -990,6 +1129,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "weezl" version = "0.1.8" @@ -1095,3 +1243,32 @@ checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" dependencies = [ "memchr", ] + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.8.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index 7c847f4..c7754ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "jep106-build" version = "0.1.0" -authors = ["Noah Huesser ", "Dominik Boehi "] +authors = [ + "Noah Huesser ", + "Dominik Boehi ", +] edition = "2018" description = "Helper tool to extract JEP106 codes from the provided PDF." repository = "https://github.com/tiwalun/jep106-parse" @@ -11,12 +14,13 @@ keywords = ["jep106", "embedded"] license = "MIT OR Apache-2.0" [dependencies] -pdf-extract = { version = "0.8.2" } +pdf-extract = { version = "0.9" } regex = "1.11.1" clap = { version = "4.5", features = ["derive"] } anyhow = "1.0.98" trycmd = "0.15.9" +serde_json = "1" [dev-dependencies] -insta = { version = "1.41.1", features = ["yaml", "toml"] } +insta = { version = "1.43", features = ["yaml", "toml"] } insta-cmd = "0.6.0" diff --git a/src/main.rs b/src/main.rs index f030ffb..c2b49cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ use std::path::PathBuf; use anyhow::{Context, Result}; use clap::Parser; +use regex::Regex; +use serde_json::Value; #[derive(clap::Parser)] struct Opts { @@ -11,6 +13,14 @@ struct Opts { pdf: PathBuf, #[arg(long = "jep_version", value_name = "VERSION")] jep_version: String, + #[arg(long, value_name = "FORMAT")] + format: Option, +} + +#[derive(clap::ValueEnum, Clone)] +enum Format { + Rust, + Json, } fn main() -> Result<()> { @@ -18,16 +28,16 @@ fn main() -> Result<()> { let dest_path = "codes.rs"; - let mut f = File::create(&dest_path)?; + let f = File::create(dest_path)?; let contents = pdf_extract::extract_text(&opts.pdf) .with_context(|| format!("Failed to extract text from file '{}'", opts.pdf.display()))?; let mut data: Vec>> = vec![]; + let re = Regex::new(r"^[0-9]+\s+(.*?)\s+([01]\s+){8}([0-9A-F]{2})\s+$").unwrap(); + for line in contents.lines() { - use regex::Regex; - let re = Regex::new(r"^[0-9]+\s+(.*?)\s+([01]\s+){8}([0-9A-F]{2})\s+$").unwrap(); if let Some(capture) = re.captures(line) { if &capture[3] == "01" { data.push(vec![None; 256]); @@ -38,6 +48,19 @@ fn main() -> Result<()> { } } + match opts.format.unwrap_or(Format::Rust) { + Format::Rust => make_rust(f, data, opts.jep_version)?, + Format::Json => make_json(f, data, opts.jep_version)?, + } + + Ok(()) +} + +fn make_rust( + mut f: File, + data: Vec>>, + jep_version: String, +) -> Result<(), anyhow::Error> { let _ = f.write_all( format!( "pub(crate) const CODES: [[Option<&'static str>; 256]; {}] = [", @@ -45,7 +68,6 @@ fn main() -> Result<()> { ) .as_bytes(), ); - for bank in data.iter() { let _ = f.write(b"["); for company in bank { @@ -57,9 +79,7 @@ fn main() -> Result<()> { } let _ = f.write_all(b"],"); } - let _ = f.write_all(b"];"); - f.write_all( format!( " @@ -68,10 +88,41 @@ fn main() -> Result<()> { \"{}\" }} ", - opts.jep_version + jep_version ) .as_bytes(), )?; + Ok(()) +} + +fn make_json( + f: File, + data: Vec>>, + jep_version: String, +) -> Result<(), anyhow::Error> { + let mut targets = vec![]; + + for bank in data.into_iter() { + for manufacturer in bank { + targets.push(if let Some(manufacturer) = manufacturer { + Value::Object({ + let mut map = serde_json::Map::new(); + map.insert("manufacturer".to_string(), Value::String(manufacturer)); + map + }) + } else { + Value::Null + }); + } + } + + let data = Value::Object({ + let mut map = serde_json::Map::new(); + map.insert("version".to_string(), Value::String(jep_version)); + map.insert("targets".to_string(), Value::Array(targets)); + map + }); + serde_json::to_writer_pretty(f, &data)?; Ok(()) } From bfef50a9ec808d30f4c064b9942117907f34287d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 7 May 2025 12:37:13 +0200 Subject: [PATCH 2/7] appease clippy --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index c2b49cb..f2b47d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,7 +63,7 @@ fn make_rust( ) -> Result<(), anyhow::Error> { let _ = f.write_all( format!( - "pub(crate) const CODES: [[Option<&'static str>; 256]; {}] = [", + "pub(crate) static CODES: [[Option<&'static str>; 256]; {}] = [", data.len() ) .as_bytes(), From b3b8a6341ca4a2d6573ccb1e60e6333ae3a12a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 7 May 2025 12:41:13 +0200 Subject: [PATCH 3/7] make the file ending .json if format is json and properly default the format via clap --- src/main.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index f2b47d7..a1d6241 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,8 +13,8 @@ struct Opts { pdf: PathBuf, #[arg(long = "jep_version", value_name = "VERSION")] jep_version: String, - #[arg(long, value_name = "FORMAT")] - format: Option, + #[arg(long, value_name = "FORMAT", default_value_t = default::FORMAT)] + format: Format, } #[derive(clap::ValueEnum, Clone)] @@ -23,10 +23,28 @@ enum Format { Json, } +impl std::fmt::Display for Format { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Format::Rust => "rust", + Format::Json => "json", + }) + } +} + +mod default { + use crate::Format; + + pub const FORMAT: Format = Format::Rust; +} + fn main() -> Result<()> { let opts = Opts::parse(); - let dest_path = "codes.rs"; + let dest_path = match opts.format { + Format::Rust => "codes.rs", + Format::Json => "codes.json", + }; let f = File::create(dest_path)?; @@ -48,7 +66,7 @@ fn main() -> Result<()> { } } - match opts.format.unwrap_or(Format::Rust) { + match opts.format { Format::Rust => make_rust(f, data, opts.jep_version)?, Format::Json => make_json(f, data, opts.jep_version)?, } From 48b9fff6e41dc9e205c91967049688fc3099392c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 7 May 2025 12:52:50 +0200 Subject: [PATCH 4/7] Add error hint --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index a1d6241..30761d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,7 @@ fn main() -> Result<()> { let f = File::create(dest_path)?; let contents = pdf_extract::extract_text(&opts.pdf) - .with_context(|| format!("Failed to extract text from file '{}'", opts.pdf.display()))?; + .with_context(|| format!("Failed to extract text from file '{}'. Usually errors happen when the PDF has a bad format. Try exporting to PDF/A.", opts.pdf.display()))?; let mut data: Vec>> = vec![]; From a3007afc6f5600a97fec083c9466d86107db5b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 7 May 2025 12:53:50 +0200 Subject: [PATCH 5/7] Flatten the manufacturer entries --- src/main.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 30761d3..228f625 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,11 +123,7 @@ fn make_json( for bank in data.into_iter() { for manufacturer in bank { targets.push(if let Some(manufacturer) = manufacturer { - Value::Object({ - let mut map = serde_json::Map::new(); - map.insert("manufacturer".to_string(), Value::String(manufacturer)); - map - }) + Value::String(manufacturer) } else { Value::Null }); From cdc573a79463795e04d26080f220e7edf029ab0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 7 May 2025 12:54:28 +0200 Subject: [PATCH 6/7] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7bc1b8d..2699561 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ # Generated file with codes codes.rs +codes.json \ No newline at end of file From b30cfc9b6d79c3b45e6fd0fc8fde5212f3bf214a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Wed, 7 May 2025 15:01:35 +0200 Subject: [PATCH 7/7] fix wrongly named target field and name it manufacturers --- src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 228f625..725de9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,11 +118,11 @@ fn make_json( data: Vec>>, jep_version: String, ) -> Result<(), anyhow::Error> { - let mut targets = vec![]; + let mut manufacturers = vec![]; for bank in data.into_iter() { for manufacturer in bank { - targets.push(if let Some(manufacturer) = manufacturer { + manufacturers.push(if let Some(manufacturer) = manufacturer { Value::String(manufacturer) } else { Value::Null @@ -133,7 +133,7 @@ fn make_json( let data = Value::Object({ let mut map = serde_json::Map::new(); map.insert("version".to_string(), Value::String(jep_version)); - map.insert("targets".to_string(), Value::Array(targets)); + map.insert("manufacturers".to_string(), Value::Array(manufacturers)); map });