Skip to content

Commit

Permalink
replace panic with error
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnikita committed Oct 5, 2023
1 parent 544ac19 commit f4b6457
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
24 changes: 12 additions & 12 deletions src/patch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod patch_cli;

use globset::Glob;
use regex::Regex;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -571,13 +570,9 @@ fn make_cpu(cmod: &Hash) -> Result<CpuBuilder> {

/// Find left and right indices of enumeration token in specification string
///
/// # Panics
///
/// Panics if the expression contains no or more than one token.
///
/// # Examples
///
/// ```
/// ```ignore
/// let cases = [
/// ("RELOAD?", (6, 0)),
/// ("TMR[1-57]_MUX", (3, 4)),
Expand All @@ -586,20 +581,25 @@ fn make_cpu(cmod: &Hash) -> Result<CpuBuilder> {
/// ("CSPT[1][7-9],CSPT[2][0-5]", (4, 0)),
/// ];
/// for (spec, (li, ri)) in cases {
/// assert_eq!(patch::spec_ind(spec), (li, ri));
/// assert_eq!(spec_ind(spec), Some((li, ri)));
/// }
/// ```
///
fn spec_ind(spec: &str) -> (usize, usize) {
fn spec_ind(spec: &str) -> Option<(usize, usize)> {
use once_cell::sync::Lazy;
use regex::Regex;
let spec = spec.split(',').next().unwrap_or(spec);
let re =
Regex::new(r"^\w*((?:[\?*]|\[\d+(?:-\d+)?\]|\[[a-zA-Z]+(?:-[a-zA-Z]+)?\])+)\w*$").unwrap();
let caps = re.captures(spec).unwrap();
static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^\w*((?:[\?*]|\[\d+(?:-\d+)?\]|\[[a-zA-Z]+(?:-[a-zA-Z]+)?\])+)\w*$").unwrap()
});
let Some(caps) = RE.captures(spec) else {
return None;
};
let spec = caps.get(0).unwrap();
let token = caps.get(1).unwrap();
let li = token.start();
let ri = spec.end() - token.end();
(li, ri)
Some((li, ri))
}

fn check_offsets(offsets: &[u32], dim_increment: u32) -> bool {
Expand Down
26 changes: 21 additions & 5 deletions src/patch/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,11 @@ fn collect_in_array(
return Err(anyhow!("{path}: registers {rspec} not found"));
}
registers.sort_by_key(|r| r.address_offset);
let (li, ri) = spec_ind(rspec);
let Some((li, ri)) = spec_ind(rspec) else {
return Err(anyhow!(
"`{rspec}` contains no tokens or contains more than one token"
));
};
let dim = registers.len();
let dim_index = if rmod.contains_key(&"_start_from_zero".to_yaml()) {
(0..dim).map(|v| v.to_string()).collect::<Vec<_>>()
Expand Down Expand Up @@ -1241,10 +1245,18 @@ fn collect_in_cluster(
.iter()
.map(|r| {
let match_rspec = matchsubspec(&r.name, rspec).unwrap();
let (li, ri) = spec_ind(match_rspec);
r.name[li..r.name.len() - ri].to_string()
let Some((li, ri)) = spec_ind(match_rspec) else {
return Err(anyhow!(
"`{match_rspec}` contains no tokens or contains more than one token"
));
};
Ok(r.name[li..r.name.len() - ri].to_string())
})
.collect::<Vec<_>>();
.collect::<Result<Vec<_>, _>>();
let new_dim_index = match new_dim_index {
Ok(v) => v,
Err(e) => return Err(e),
};
if first {
dim = registers.len();
dim_index = new_dim_index;
Expand Down Expand Up @@ -1316,7 +1328,11 @@ fn collect_in_cluster(
reg.name = if let Some(name) = rmod.get_str("name")? {
name.into()
} else {
let (li, ri) = spec_ind(&rspec);
let Some((li, ri)) = spec_ind(&rspec) else {
return Err(anyhow!(
"`{rspec}` contains no tokens or contains more than one token"
));
};
format!("{}{}", &rspec[..li], &rspec[rspec.len() - ri..])
};
if let Some(desc) = rmod.get_str("description")? {
Expand Down
6 changes: 5 additions & 1 deletion src/patch/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,11 @@ impl RegisterExt for Register {
return Err(anyhow!("{}: fields {fspec} not found", self.name));
}
fields.sort_by_key(|f| f.bit_range.offset);
let (li, ri) = spec_ind(fspec);
let Some((li, ri)) = spec_ind(fspec) else {
return Err(anyhow!(
"`{fspec}` contains no tokens or contains more than one token"
));
};
let dim = fields.len();
let dim_index = if fmod.contains_key(&"_start_from_zero".to_yaml()) {
(0..dim).map(|v| v.to_string()).collect::<Vec<_>>()
Expand Down

0 comments on commit f4b6457

Please sign in to comment.