ifmt/lib.rs
1/*! A small crate which brings inline string interpolation to rust's standard formatting macros.
2
3# Examples
4```rust
5use ifmt::iprintln;
6let four = 4;
7iprintln!("four plus four is: " four + 4);
8// four plus four is: 8
9iprintln!("here's a hex number: 0x" 0xb0bi64 * 1321517i64 ;x);
10// here's a hex number: 0xdeadbeef
11iprintln!("here's a debugging value: " Some(four);?);
12// here's a debugging value: Some(4)
13```
14
15# Supported macros
16```ignore
17format! -> iformat!
18print! -> iprint!
19println! -> iprintln!
20eprint! -> ieprint!
21eprintln! -> ieprintln!
22write! -> iwrite!
23writeln! -> iwriteln!
24panic! -> ipanic!
25format_args! -> iformat_args!
26```
27*/
28
29use proc_macro_hack::proc_macro_hack;
30
31/// Creates a String by interpolating inline expressions.
32///
33/// Works by expanding to `format!`.
34///
35/// ```
36/// # let foo = Some(vec![1, 2, 3]);
37/// # let bar = Some(vec![4, 5, 6]);
38/// # let x = 6;
39/// # let y = 20;
40/// # use ifmt::iformat;
41/// # assert!(
42/// // Out-of-literal style (preferred)
43/// iformat!("x plus y is " x + y ".") == format!("x plus y is {}.", x + y)
44/// # &&
45/// iformat!("foo: " foo;? ", bar: " bar;#?) == format!("foo: {:?}, bar: {:#?}", foo, bar)
46/// # &&
47///
48/// // In-literal style (old)
49/// iformat!("x plus y is {x + y}.") == format!("x plus y is {}.", x + y)
50/// # &&
51/// iformat!("foo: {foo:?}, bar: {bar:#?}") == format!("foo: {:?}, bar: {:#?}", foo, bar)
52/// # );
53/// ```
54///
55/// Out-of-literal format specs are roughly the same as in `std::fmt`, except:
56///
57/// * they are preceded by a `;` rather than a `:`
58/// * format parameterization (for width/precision) is not yet supported, literals must be used
59/// * since `3.4e`/`3.4E` (as in `format!("{:3.4e}", x)`) is invalid in rust (exponent is missing),
60/// `3.4 e` (with a space) or `3.4s` must be used instead
61/// * The fill character for padding must be a char literal:
62/// `iformat!("padded: " inside_dashes;'-'^30)`
63///
64/// In-literal format specs are identical to those found in `std::fmt`, except that they also do not
65/// support format parameterization.
66#[proc_macro_hack(fake_call_site)]
67pub use ifmt_impl::iformat;
68
69/// Print an [`iformat!`][iformat]-ed string to standard out.
70#[proc_macro_hack(fake_call_site)]
71pub use ifmt_impl::iprint;
72
73/// Print an [`iformat!`][iformat]-ed string to standard out, followed by `\n`.
74#[proc_macro_hack(fake_call_site)]
75pub use ifmt_impl::iprintln;
76
77/// Print an [`iformat!`][iformat]-ed string to standard error.
78#[proc_macro_hack(fake_call_site)]
79pub use ifmt_impl::ieprint;
80
81/// Print an [`iformat!`][iformat]-ed string to standard error, followed by `\n`.
82#[proc_macro_hack(fake_call_site)]
83pub use ifmt_impl::ieprintln;
84
85/// Print an [`iformat!`][iformat]-ed string to the given buffer.
86#[proc_macro_hack(fake_call_site)]
87pub use ifmt_impl::iwrite;
88
89/// Print an [`iformat!`][iformat]-ed string to the given buffer, followed by `\n`.
90#[proc_macro_hack(fake_call_site)]
91pub use ifmt_impl::iwriteln;
92
93/// Create a `fmt::Arguments` value a la `format_args!` with inlined expressions (using the same syntax as [`iformat!`][iformat]).
94#[proc_macro_hack(fake_call_site)]
95pub use ifmt_impl::iformat_args;
96
97/// Panic with an [`iformat!`][iformat]-ed message.
98#[proc_macro_hack(fake_call_site)]
99pub use ifmt_impl::ipanic;