Crate dbg_pls

source ·
Expand description

Syntax aware debug printing.

Makes use of syn and prettyplease in order to provide the most canonincal rust debug lines as possible, quickly.

Example usage

use dbg_pls::{pretty, DebugPls};

#[derive(DebugPls, Copy, Clone)]
pub struct Demo {
    foo: i32,
    bar: &'static str,
}

let mut val = [Demo { foo: 5, bar: "hello" }; 10];
val[6].bar = "Hello, world! I am a very long string";

let output = format!("{}", pretty(&val));
let expected = r#"[
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo {
        foo: 5,
        bar: "Hello, world! I am a very long string",
    },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
]"#;

assert_eq!(output, expected);

Example with highlighting

use dbg_pls::{color, DebugPls};

#[derive(DebugPls, Copy, Clone)]
pub struct Demo {
    foo: i32,
    bar: &'static str,
}

let mut val = [Demo { foo: 5, bar: "hello" }; 10];
val[6].bar = "Hello, world! I am a very long string";

println!("{}", color(&val));

Outputs:

Why

For the sake of demonstration, let’s take a look at the snippet from above. It provides an array of 10 Demo structs. You could imagine this to be representative of a complex deep struct.

#[derive(Debug, Copy, Clone)]
pub struct Demo {
    foo: i32,
    bar: &'static str,
}

let mut val = [Demo { foo: 5, bar: "hello" }; 10];
val[6].bar = "Hello, world! I am a very long string";

println!("{:?}", val);

This outputs

[Demo { foo: 5, bar: "hello" }, Demo { foo: 5, bar: "hello" }, Demo { foo: 5, bar: "hello" }, Demo { foo: 5, bar: "hello" }, Demo { foo: 5, bar: "hello" }, Demo { foo: 5, bar: "hello" }, Demo { foo: 5, bar: "Hello, world! I am a very long string" }, Demo { foo: 5, bar: "hello" }, Demo { foo: 5, bar: "hello" }, Demo { foo: 5, bar: "hello" }]

Switching to the alternative output format {:#?} you get the following

[
   Demo {
       foo: 5,
       bar: "hello",
   },
   Demo {
       foo: 5,
       bar: "hello",
   },
   Demo {
       foo: 5,
       bar: "hello",
   },
   Demo {
       foo: 5,
       bar: "hello",
   },
   Demo {
       foo: 5,
       bar: "hello",
   },
   Demo {
       foo: 5,
       bar: "hello",
   },
   Demo {
       foo: 5,
       bar: "Hello, world! I am a very long string",
   },
   Demo {
       foo: 5,
       bar: "hello",
   },
   Demo {
       foo: 5,
       bar: "hello",
   },
   Demo {
       foo: 5,
       bar: "hello",
   },
]

Both of these are very unweildy to read through. Compare that to our pretty formatting:

println!("{}", pretty(&val));

And you will see

[
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo {
        foo: 5,
        bar: "Hello, world! I am a very long string",
    },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
    Demo { foo: 5, bar: "hello" },
]

How it works

All DebugPls implementations are forced to output only valid syn::Expr values. These are then formatted using prettyplease::unparse. Finally, it uses syntect to provide syntax highlighting, with theme provided by https://github.com/jonschlinkert/sublime-monokai-extended

Macros

  • colorcolors
    Prints and returns the value of a given expression for quick and dirty debugging. Same as std::dbg
  • prettypretty
    Prints and returns the value of a given expression for quick and dirty debugging. Same as std::dbg

Structs

Traits

  • Syntax aware pretty-printed debug formatting.

Functions

Derive Macros

  • DebugPlsderive
    Derives the standard DebugPls implementation.