Skip to content

Commit

Permalink
fix(client): Parse crate names from impl traits (#37)
Browse files Browse the repository at this point in the history
* fix(example): Make examples compile

* fix(client): Parse crate names from impl traits
  • Loading branch information
jan-auer authored Apr 25, 2018
1 parent 2bbb378 commit b2310fe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ rustc_version = { version = "0.2.2", optional = true }
[dev-dependencies]
failure_derive = "0.1.1"
pretty_env_logger = "0.2.2"

[[example]]
name = "error-chain-demo"
required-features = ["with_error_chain"]
44 changes: 39 additions & 5 deletions src/client/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ impl Default for ClientOptions {
}

lazy_static! {
static ref CRATE_RE: Regex = Regex::new(r"^([a-zA-Z0-9_]+?)::").unwrap();
static ref CRATE_RE: Regex = Regex::new(r"^(?:_<)?([a-zA-Z0-9_]+?)(?:\.\.|::)").unwrap();
}

/// Tries to parse the rust crate from a function name.
fn parse_crate_name(func_name: &str) -> Option<String> {
CRATE_RE
.captures(func_name)
.and_then(|caps| caps.get(1))
.map(|cr| cr.as_str().into())
}

/// Helper trait to convert an object into a client config
Expand Down Expand Up @@ -356,10 +364,7 @@ impl Client {

// set package if missing to crate prefix
if frame.package.is_none() {
frame.package = CRATE_RE
.captures(func_name)
.and_then(|caps| caps.get(1))
.map(|cr| cr.as_str().into());
frame.package = parse_crate_name(func_name);
}

match frame.in_app {
Expand Down Expand Up @@ -499,3 +504,32 @@ pub fn init<C: IntoClientConfig>(cfg: C) -> ClientInitGuard {
client
}))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_crate_name() {
assert_eq!(
parse_crate_name("futures::task_impl::std::set"),
Some("futures".into())
);
}

#[test]
fn test_parse_crate_name_impl() {
assert_eq!(
parse_crate_name("_<futures..task_impl..Spawn<T>>::enter::_{{closure}}"),
Some("futures".into())
);
}

#[test]
fn test_parse_crate_name_unknown() {
assert_eq!(
parse_crate_name("_<F as alloc..boxed..FnBox<A>>::call_box"),
None
);
}
}

0 comments on commit b2310fe

Please sign in to comment.