Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get string with formatted object #4

Closed
Alonely0 opened this issue Jul 18, 2021 · 1 comment
Closed

Get string with formatted object #4

Alonely0 opened this issue Jul 18, 2021 · 1 comment

Comments

@Alonely0
Copy link

I have my own macros for printing stuff on different situations and not putting conditionals everywhere, for example:

macro_rules! println_on_debug {
    ($($args:tt)*) => {
        if std::env::var("DEBUG").unwrap_or_else(|_| { "false".to_string() }) == "true" ||
           env!("CARGO_PKG_VERSION").ends_with("dev") {
               println!("DEBUG-MSG: {}", format!{ $($args)* })
        }
    };
}

I'd like to make that macro print objects with the same formatting as pprint, the problem is that pprint is not a macro that sends args to a formatter and redirects the output to stdout, pprint is a function that just formats & prints a complex structure. My suggestion consists in creating a function that instead of printing the formatted structure to stdout, just returns it, so I can call that macro like this:

println_on_debug!("struct: {}", pformat(complex_structure));
@aDotInTheVoid
Copy link
Owner

pprint literaly does this. It returns a string. https://docs.rs/debug2/0.1.0/debug2/fn.pprint.html

I guess the name is confusing.

Closing, but feel free to reopen if I misunderstood

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants