tracing icon indicating copy to clipboard operation
tracing copied to clipboard

Integration With Anyhow

Open amab8901 opened this issue 9 months ago • 1 comments

Feature Request

Motivation

Reduce noise in the code

Proposal

The code would look cleaner if I could write like this:

use std::str::FromStr;

use anyhow::Context;
use num::BigUint;
use num::ToPrimitive;
use tracing::error;

fn main() {
    let uint = BigUint::from_str("1").unwrap();
    let floating_uint = uint
        .to_f64()
        .error("Failed to convert `BigUint` to `f64`.") // diff
        .ok()
        .unwrap();
}

Doesn't have to be called error. Some other suggestions are tracing_error, t_error, tracing_err. And same thing for the other log levels (info, warn, trace, etc)

Alternatives

Here is how I'm currently writing the code:

use std::str::FromStr;

use anyhow::Context;
use num::BigUint;
use num::ToPrimitive;
use tracing::error;

fn main() {
    let uint = BigUint::from_str("1").unwrap();
    let floating_uint = uint
        .to_f64()
        .context("Failed to convert `BigUint` to `f64`.") // diff
        .map_err(|err| error!("{err}")) // diff
        .ok()
        .unwrap();
}

amab8901 avatar May 28 '24 09:05 amab8901