Skip to content

Commit b2310fe

Browse files
authored
fix(client): Parse crate names from impl traits (#37)
* fix(example): Make examples compile * fix(client): Parse crate names from impl traits
1 parent 2bbb378 commit b2310fe

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ rustc_version = { version = "0.2.2", optional = true }
5555
[dev-dependencies]
5656
failure_derive = "0.1.1"
5757
pretty_env_logger = "0.2.2"
58+
59+
[[example]]
60+
name = "error-chain-demo"
61+
required-features = ["with_error_chain"]

src/client/real.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@ impl Default for ClientOptions {
8787
}
8888

8989
lazy_static! {
90-
static ref CRATE_RE: Regex = Regex::new(r"^([a-zA-Z0-9_]+?)::").unwrap();
90+
static ref CRATE_RE: Regex = Regex::new(r"^(?:_<)?([a-zA-Z0-9_]+?)(?:\.\.|::)").unwrap();
91+
}
92+
93+
/// Tries to parse the rust crate from a function name.
94+
fn parse_crate_name(func_name: &str) -> Option<String> {
95+
CRATE_RE
96+
.captures(func_name)
97+
.and_then(|caps| caps.get(1))
98+
.map(|cr| cr.as_str().into())
9199
}
92100

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

357365
// set package if missing to crate prefix
358366
if frame.package.is_none() {
359-
frame.package = CRATE_RE
360-
.captures(func_name)
361-
.and_then(|caps| caps.get(1))
362-
.map(|cr| cr.as_str().into());
367+
frame.package = parse_crate_name(func_name);
363368
}
364369

365370
match frame.in_app {
@@ -499,3 +504,32 @@ pub fn init<C: IntoClientConfig>(cfg: C) -> ClientInitGuard {
499504
client
500505
}))
501506
}
507+
508+
#[cfg(test)]
509+
mod tests {
510+
use super::*;
511+
512+
#[test]
513+
fn test_parse_crate_name() {
514+
assert_eq!(
515+
parse_crate_name("futures::task_impl::std::set"),
516+
Some("futures".into())
517+
);
518+
}
519+
520+
#[test]
521+
fn test_parse_crate_name_impl() {
522+
assert_eq!(
523+
parse_crate_name("_<futures..task_impl..Spawn<T>>::enter::_{{closure}}"),
524+
Some("futures".into())
525+
);
526+
}
527+
528+
#[test]
529+
fn test_parse_crate_name_unknown() {
530+
assert_eq!(
531+
parse_crate_name("_<F as alloc..boxed..FnBox<A>>::call_box"),
532+
None
533+
);
534+
}
535+
}

0 commit comments

Comments
 (0)