elichai / log-derive

A procedural macro for auto logging output of functions
https://docs.rs/log-derive
Apache License 2.0
189 stars 12 forks source link

Result<T, E> detection doesn't work reliably #2

Closed TedDriggs closed 5 years ago

TedDriggs commented 5 years ago

Currently, Result detection looks to see if the first term in the path == "Result". This is brittle, as the code below demonstrates:

#[logfn(INFO, fmt = "fibonacci() -> {:?}", err = "Error", ok = "Info")]
fn fibonacci(n: u32) -> std::result::Result<u32, u32> {
    match n {
        0 => Ok(1),
        1 => Ok(1),
        3 => Err(3),
        _ => Ok(fibonacci(n - 1)? + fibonacci(n - 2)?),
    }
}

This means functions that return mod-specific Result types such as io::Result or fmt::Result won't have the correct log level.

Unfortunately, there's no good way to tell if a particular type is an alias of Result. This leaves three options for how to proceed:

  1. Check if the last ident term in the return type path is Result. This would catch most cases.
  2. Assume that the return type is a result if err and/or ok are specified. Since these don't make sense to use otherwise, this is a lightweight way to solve the problem.
  3. Add a new meta item to the attribute which opts into the Result behavior. This approach preserves compatibility with existing attribute usage, but at the expense of conciseness.
elichai commented 5 years ago

Hmm Maybe i'll use a combination of 1 and 2, because I still want that #[logfn(INFO)] will work on Results too. On the other hand, it will work even without matching through it just like Ok(T) Err(E). I'll need to think about this a bit

TedDriggs commented 5 years ago

I've got a fix for this; going to put up a PR now.