report

Crates.ioreport
lib.rsreport
version1.0.0
sourcesrc
created_at2024-06-08 20:30:27.45959
updated_at2024-06-08 20:30:27.45959
descriptionContextual logging and error reporting
homepage
repositoryhttps://github.com/K83FJ3M4/report
max_upload_size
id1265951
size23,017
Phillip Mendel (K83FJ3M4)

documentation

https://docs.rs/report

README

Report

MIT licensed Latest version Documentation

Report is a simple logging and error-reporting library. It is designed to be:

  • Simple: There is almost no boilerplate required. It is not required to define error enums or implement traits.
  • Compatible: This library will work with other libraries that don't use the custom Result or Error types from this crate.
  • Efficient: Strings are only formatted when it is clear that they will actually be printed.

Example

use report::{Result, log, report, info, bail};
use std::fs::File;
use std::io::{BufRead, BufReader};

#[report]
#[log("Running experiments")]
fn main() {
    let path = "data.txt";
    
    #[report("Running task one on {path}")]
    task_one(path).ok();

    let path = "Cargo.toml";
    #[report("Running task two on {path}")]
    task_two(path).ok();
}

fn task_one(file: &str) -> Result {
    let _file = File::open(file)?; 
    bail!("File exists, even though it should not")
}

#[report]
fn task_two(file: &str) -> Result {
    let file = File::open(file)?;
    let metadata = file.metadata()?;

    info!("File size: {}", metadata.len());
    
    for line in BufReader::new(file).lines() {
        #[report("Reading line")]
        let line = line?;

        if line.starts_with("[") {
            info!("Found section: {line}");
        }
    }

    Ok(())
}

Output

╭───────────────────────────────────────────────────────────────────────────────────────╮
│ Running experinments                                                                  │
├─┬─────────────────────────────────────────────────────────────────────────────────────┤
│ ├── Running task one on data.txt                                                      │
│ │   ╰── error: No such file or directory (os error 2)                                 │
│ ╰── Running task two on Cargo.toml                                                    │
│     ├── info: File size: 552                                                          │
│     ├── info: Found section: [package]                                                │
│     ├── info: Found section: [workspace]                                              │
│     ├── info: Found section: [dependencies]                                           │
│     ╰── info: Found section: [features]                                               │
╰───────────────────────────────────────────────────────────────────────────────────────╯

Feature Flags

Flag Description
unicode Use unicode box drawing characters.
color Use colors for the log level.
frame Draw a frame around every report
Commit count: 3

cargo fmt