From 25226a374ee7eb8700e0036599fcb6d1740cff4c Mon Sep 17 00:00:00 2001 From: Markus Krause Date: Sat, 9 Mar 2024 23:07:12 +0100 Subject: [PATCH] add regex parsing for jlc and lcsc urls --- Cargo.lock | 9 +++++---- Cargo.toml | 1 + src/app.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 55cdb0c..3310ecd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -940,6 +940,7 @@ dependencies = [ "env_logger", "image", "log", + "regex", "reqwest", "serde", "serde_json", @@ -2536,9 +2537,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.2" +version = "1.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" dependencies = [ "aho-corasick", "memchr", @@ -2548,9 +2549,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.3" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" dependencies = [ "aho-corasick", "memchr", diff --git a/Cargo.toml b/Cargo.toml index 9b0fb58..c9a505f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ reqwest = { version = "0.11.25", features = ["blocking"] } urlencoding = "2.1.3" subprocess = "0.2.9" serde_json = "1.0.114" +regex = "1.10.3" # You only need serde if you want app persistence: serde = { version = "1", features = ["derive"] } diff --git a/src/app.rs b/src/app.rs index 7a97a46..b29f847 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,14 +1,18 @@ +use regex::Regex; + /// We derive Deserialize/Serialize so we can persist app state on shutdown. #[derive(serde::Deserialize, serde::Serialize)] #[serde(default)] // if we add new fields, give them default values when deserializing old state pub struct MyApp { part: String, + current_part: String, } impl Default for MyApp { fn default() -> Self { Self { part: "C11702".to_owned(), + current_part: "".to_owned(), } } } @@ -27,6 +31,34 @@ impl MyApp { Default::default() } + + fn get_part(search_term: &str) -> Option<&str> { + let term = search_term.trim(); + let re_jlc = Regex::new(r"/(C\d+)$").unwrap(); + let re_lcsc = Regex::new(r"_(C\d+)[^/]*\.html$").unwrap(); + let mut lcscnumber = ""; + + // case one, we got passed a URL + if term.contains("http") { + if term.contains("jlcpcb.com") { + if let Some(captures) = re_jlc.captures(term) { + lcscnumber = captures.get(1).unwrap().as_str(); // safe because index 0 + } + } else if term.contains("lcsc.com") { + if let Some(captures) = re_lcsc.captures(term) { + lcscnumber = captures.get(1).unwrap().as_str(); // safe because index 0 + } + } + } else if term.starts_with("C") { + lcscnumber = term; + } + if !lcscnumber.is_empty() { + return Some(lcscnumber); + } else { + return None; + } + // "https://cart.jlcpcb.com/shoppingCart/smtGood/getComponentDetail?componentCode={self.part}" + } } impl eframe::App for MyApp { @@ -66,6 +98,12 @@ impl eframe::App for MyApp { ui.horizontal(|ui| { ui.label("LCSC number or part URL: "); ui.text_edit_singleline(&mut self.part); + ui.label(self.current_part.as_str()); + if ui.button("Search").clicked() { + if let Some(lcscnumber) = Self::get_part(self.part.as_str()) { + self.current_part = lcscnumber.to_string(); + } + } }); ui.separator();