Skip to content

Commit

Permalink
add regex parsing for jlc and lcsc urls
Browse files Browse the repository at this point in the history
  • Loading branch information
markusdd committed Mar 9, 2024
1 parent 90570a0 commit 25226a3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
38 changes: 38 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -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(),
}
}
}
Expand All @@ -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") {

Check failure on line 52 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

single-character string constant used as pattern
lcscnumber = term;
}
if !lcscnumber.is_empty() {
return Some(lcscnumber);

Check failure on line 56 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

unneeded `return` statement
} else {
return None;

Check failure on line 58 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

unneeded `return` statement
}
// "https://cart.jlcpcb.com/shoppingCart/smtGood/getComponentDetail?componentCode={self.part}"
}
}

impl eframe::App for MyApp {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 25226a3

Please sign in to comment.