Skip to content

Commit

Permalink
Make --strip-path-prefix actully use regex (as promised in docs)
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
  • Loading branch information
ggwpez committed Mar 29, 2024
1 parent f7f398e commit d980447
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ clap.workspace = true
syn = { version = "2.0.55", features = ["parsing", "full"] }
comfy-table = { version = "7.1.0", default-features = false }
serde = { version = "1.0.197", features = [ "derive" ] }
fancy-regex = "0.13.0"

[dev-dependencies]
assert_cmd = "2.0.14"
Expand Down
11 changes: 6 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,18 @@ pub struct FormatParams {

/// Non-regex string to strip common path prefixes from the file paths.
///
/// Example: `--strip-path-prefix "^runtime/*/src/weights/"`.
/// Uses the `fancy_regex` crate.
/// Example: `--strip-path-prefix ".*/"` to strip everything but the file-name.
#[clap(long)]
strip_path_prefix: Option<String>,
}

impl FormatParams {
pub fn filter_path(&self, path: String) -> String {
match self.strip_path_prefix.as_ref() {
Some(prefix) => path.strip_prefix(prefix).unwrap_or(&path).to_string(),
None => path,
if let Some(prefix) = self.strip_path_prefix.as_ref() {
let re = fancy_regex::Regex::new(prefix).expect("Invalid regex");
re.replace_all(&path, "").to_string()
} else {
path
}
}
}
Expand Down

0 comments on commit d980447

Please sign in to comment.