#formatting #fmt #adapter #traits #offers #recursion #debugging

no-std manyfmt

Easily create more varieties of formatting than the std::fmt traits offer

1 unstable release

0.1.0 Oct 7, 2023

#246 in No standard library

Download history 12/week @ 2024-01-06 40/week @ 2024-01-13 66/week @ 2024-01-20 24/week @ 2024-01-27 57/week @ 2024-02-03 133/week @ 2024-02-10 88/week @ 2024-02-17 131/week @ 2024-02-24 44/week @ 2024-03-02 55/week @ 2024-03-09 66/week @ 2024-03-16 97/week @ 2024-03-23 88/week @ 2024-03-30 15/week @ 2024-04-06 34/week @ 2024-04-13 23/week @ 2024-04-20

166 downloads per month
Used in 7 crates (via all-is-cubes)

MIT/Apache

10KB
76 lines

manyfmt

This Rust library, manyfmt, provides adapters with which you can easily create more varieties of formatting than the std::fmt traits (Display, Debug, Binary, LowerHex, Pointer) offer, without having to write any more boilerplate than absolutely necessary. You can also easily pass additional data down through the formatting recursion.

This library is no_std compatible; it can run anywhere Rust can.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Adapters with which you can easily create more varieties of formatting than the std::fmt traits (fmt::Display, fmt::Debug, fmt::Binary, fmt::Pointer, etc.) offer, without having to write any more boilerplate than absolutely necessary. You can also easily pass additional data down through the formatting recursion.

To create a new format, declare a struct (struct MyFormat;) and implement Fmt<MyFormat> for the type you want to be able to format. Then call [Refmt::refmt()] to apply the format as a wrapper type around your data.

Example

The following code implements a minimal, non-validating TOML emitter. This demonstrates how manyfmt can be used to produce complex formatting, that operates within the std::fmt system without allocating, without writing a new by-reference wrapper type for each case.

use std::collections::BTreeMap;
use std::fmt;

use manyfmt::{Fmt, Refmt};

struct TomlFile;
struct TomlTable;

impl<S: AsRef<str>, T: Fmt<TomlTable>> Fmt<TomlFile> for BTreeMap<S, T> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>, _: &TomlFile) -> fmt::Result {
        for (key, table) in self {
            writeln!(
                fmt,
                "[{key}]\n{table}",
                key = key.as_ref(),
                table = table.refmt(&TomlTable)
            )?;
        }
        Ok(())
    }
}

impl<S: AsRef<str>, T: fmt::Debug> Fmt<TomlTable> for BTreeMap<S, T> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>, _: &TomlTable) -> fmt::Result {
        for (key, value) in self {
            // A real implementation would use TOML-specific value formatting
            // rather than `Debug`, which promises nothing.
            writeln!(fmt, "{key} = {value:?}", key = key.as_ref())?;
        }
        Ok(())
    }
}

let data = BTreeMap::from([
    ("package", BTreeMap::from([("name", "manyfmt"), ("edition", "2021")])),
    ("lib", BTreeMap::from([("name", "manyfmt")])),
]);

let text = data.refmt(&TomlFile).to_string();

assert_eq!(text,
r#"[lib]
name = "manyfmt"

[package]
edition = "2021"
name = "manyfmt"

"#);

No runtime deps