#text-formatting #string-formatting #formatting #iterator #join #replace #format-text

fmttools

Tools for modifying text without allocating any intermediate buffers or unsafe code

6 releases

0.2.2 Nov 30, 2023
0.2.1 Nov 30, 2023
0.1.2 Nov 28, 2023

#737 in Algorithms

38 downloads per month

MIT/Apache

26KB
486 lines

Fmttools

build_status crates.io

Tools for efficient modification of text as part of a single write! call.

  • No allocation is performed
  • Implemented using only safe Rust

Examples

Joining iterator elements

use fmttools::join;

let elements = vec!["abc", "\n", "123"];
assert_eq!("abc, \n, 123", format!("{}", join(&elements, ", ")));
assert_eq!("\"abc\", \"\\n\", \"123\"", format!("{:?}", join(&elements, ", ")));

Join elements with custom formatting

use fmttools::join_fmt;

// Alternatively, a closure can be used
fn format_element(x: &i32, f: &mut Formatter<'_>) -> fmt::Result {
    if *x > 3 {
        return write!(f, "3+");
    }

    write!(f, "{}", x)
}

let elements = vec![1, 2, 3, 4, 5];
assert_eq!("1, 2, 3, 3+, 3+", format!("{}", join_fmt(&elements, ", ", format_element)));

Replace arbitrary patterns

use fmttools::replace;

#[derive(Debug)]
struct FooBar {
    a: String,
}

let value = FooBar { a: "Bar".to_string() };
assert_eq!("FooBiz { a: \"Biz\" }", format!("{:?}", replace(&value, "Bar", "Biz")));

Format with extra data

use fmttools::{DebugWith, ToFormatWith};

type RegistryKey = u32;

struct Registry {
    key_names: HashMap<RegistryKey, String>,
}

struct FooEntry {
    key: RegistryKey,
}

impl DebugWith<Registry> for FooEntry {
    fn fmt(&self, f: &mut Formatter<'_>, registry: &Registry) -> fmt::Result {
        let key_name = registry.key_names.get(&self.key)
            .map(|x| x.as_str())
            .unwrap_or("unknown");

        write!(f, "FooEntry {{ key: {:?} }}", key_name)
    }
}

let registry = Registry {
    key_names: HashMap::from([
        (2, "FooA".to_string()),
        (5, "FooB".to_string()),
        (9, "Bar".to_string()),
    ]),
};

let entry = FooEntry { key: 5 };

assert_eq!("FooEntry { key: \"FooB\" }", format!("{:?}", entry.fmt_with(&registry)));

License

Licensed under the Apache License, Version 2.0 https://www.apache.org/licenses/LICENSE-2.0 or the MIT license https://opensource.org/licenses/MIT, at your option. This file may not be copied, modified, or distributed except according to those terms.

No runtime deps