fmtools/
lib.rs

1/*!
2Fast, minimal, feature-rich, extended formatting syntax for Rust!
3
4Features include:
5
6* Arbitrary expressions inside the formatting braces
7* Generates optimized Rust code at compiletime
8* Supports rust-analyzer auto complete, refactoring and more!
9* Supports Rust's standard formatting specifiers
10* Single package, no proc-macro, no_std compatible, no extra dependencies
11* Control flow allows conditional and repeated formatting
12* Capture variables by value or by reference
13* Escape hatch to inject custom formatting code
14
15See [fmt!] for more information.
16*/
17
18#![cfg_attr(not(any(test, feature = "std")), no_std)]
19
20use core::fmt as core_fmt;
21
22mod template;
23mod prelude;
24
25mod join;
26pub use self::join::*;
27
28// Formattable object holder.
29//
30// Exported but hidden to support `Copy` + `Clone` if the closure implements these traits.
31// This is otherwise not supported by existential impl trait types.
32#[doc(hidden)]
33#[allow(non_camel_case_types)]
34#[derive(Copy, Clone)]
35#[repr(transparent)]
36pub struct fmt<F: Fn(&mut core_fmt::Formatter) -> core_fmt::Result> {
37	closure: F,
38}
39impl<F: Fn(&mut core_fmt::Formatter) -> core_fmt::Result> core_fmt::Display for fmt<F> {
40	fn fmt(&self, f: &mut core_fmt::Formatter) -> core_fmt::Result {
41		(self.closure)(f)
42	}
43}
44impl<F: Fn(&mut core_fmt::Formatter) -> core_fmt::Result> core_fmt::Debug for fmt<F> {
45	fn fmt(&self, f: &mut core_fmt::Formatter) -> core_fmt::Result {
46		(self.closure)(f)
47	}
48}
49
50/// Returns a displayable object using the closure argument as its implementation.
51///
52/// ```
53/// let s = fmtools::fmt(|f| {
54/// 	f.write_str("display")
55/// });
56/// assert_eq!(s.to_string(), "display");
57/// ```
58///
59/// This is useful to insert ad-hoc formatting code:
60///
61/// ```
62/// println!("Hello {}!", fmtools::fmt(|f| {
63/// 	f.write_str("world")
64/// }));
65/// // Prints `Hello world!`
66/// ```
67pub fn fmt<F: Fn(&mut core_fmt::Formatter) -> core_fmt::Result>(closure: F) -> fmt<F> {
68	fmt { closure }
69}
70
71#[cfg(feature = "obfstr")]
72#[doc(hidden)]
73pub use obfstr::obfstr;
74
75#[cfg(not(feature = "obfstr"))]
76#[doc(hidden)]
77#[macro_export]
78macro_rules! obfstr {
79	($s:expr) => { $s };
80}
81
82#[cfg(doc)]
83#[doc = include_str!("../readme.md")]
84fn readme() {}