Debian Codegraph @

src/macros.rs

raw symbols SCIP
1 /// Creates a `String` using interpolation of runtime expressions at runtime.
2 ///
3 /// The first argument `formatx!` receives is a format string.
4 /// The power of the formatting string is in the `{}`s contained.
5 ///
6 /// Additional parameters passed to `formatx!` replace the `{}`s within the
7 /// formatting string in the order given unless named or positional parameters
8 /// are used; see [std::fmt](std::fmt) for more information.
9 ///
10 /// A common use for `formatx!` is concatenation and interpolation of strings at runtime.
11 /// The same convention is used with [print!](std::print) and [write!](core::write) macros,
12 /// depending on the intended destination of the string.
13 ///
14 /// To convert a single value to a string, use the `to_string` method. This
15 /// will use the [Display](core::fmt::Display) formatting trait.
16 ///
17 /// # Panics
18 ///
19 /// `formatx!` panics if a formatting trait implementation returns an error.
20 /// This indicates an incorrect implementation
21 /// since `fmt::Write for String` never returns an error itself.
22 /// Additonally `formatx!` returns a `Result<String, formatx::Error>` type which can be resolved later.
23 ///
24 /// # Examples
25 ///
26 /// Any type could be formatted if it implements [Display](std::fmt::Display) + [Debug](std::fmt::Debug) traits.
27 ///
28 /// ```
29 /// use formatx::formatx;
30 ///
31 /// formatx!("test").unwrap();
32 /// formatx!("hello {}", "world!").unwrap();
33 /// formatx!("x = {}, y = {y}", 10, y = 30).unwrap();
34 /// ```
35 #[macro_export]
36 macro_rules! formatxrust-analyzer cargo formatx 0.2.4 macros/formatx! {
37 ($template: expr $(,)?) => {
38 || -> ::std::result::Result<String, $crate::Error> {
39 $crate::Template::new($template)?.text()
40 }()
41 };
42
43 ($template: expr, $($values: tt)*) => {
44 || -> ::std::result::Result<String, $crate::Error> {
45 let mut template = $crate::Template::new($template)?;
46 $crate::_formatx_internal!(template, $($values)*);
47 template.text()
48 }()
49 };
50 }
51
52 #[macro_export]
53 #[doc(hidden)]
54 macro_rules! _formatx_internalrust-analyzer cargo formatx 0.2.4 macros/_formatx_internal! {
55 ($template: expr, $name: ident = $value: expr $(,)?) => {
56 $template.replace(stringify!($name), $value);
57 };
58
59 ($template: expr, $value: expr $(,)?) => (
60 $template.replace_positional($value);
61 );
62
63 ($template: expr, $name: ident = $value: expr, $($values: tt)*) => {
64 $template.replace(stringify!($name), $value);
65 $crate::_formatx_internal!($template, $($values)*);
66 };
67
68 ($template: expr, $value:expr, $($values: tt)*) => {
69 $template.replace_positional($value);
70 $crate::_formatx_internal!($template, $($values)*);
71 };
72 }
73
74 #[cfg(test)]
75 mod testsrust-analyzer cargo formatx 0.2.4 macros/tests/ {
76 use craterust-analyzer cargo formatx 0.2.4 crate/::formatxrust-analyzer cargo formatx 0.2.4 macros/formatx!;
77
78 macro_rules! formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test! {
79 ($template: expr, $($values: tt)*) => {
80 assert_eq!(formatx!($template, $($values)*).unwrap(), format!($template, $($values)*))
81 }
82 }
83
84 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
85 fn textrust-analyzer cargo formatx 0.2.4 macros/tests/text().() {
86 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatxrust-analyzer cargo formatx 0.2.4 macros/formatx!!("Hello").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Hello"));
87 }
88
89 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
90 fn positionalrust-analyzer cargo formatx 0.2.4 macros/tests/positional().() {
91 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello, {}!", "world");
92 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("The number is {}", 1);
93 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("{} {}", 1, 2);
94 }
95
96 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
97 fn namedrust-analyzer cargo formatx 0.2.4 macros/tests/named().() {
98 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("{value}", value = 4);
99 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("{argument}", argument = "test");
100 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("{a} {c} {b}", a = "a", b = 'b', c = 3);
101 }
102
103 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
104 fn intermixedrust-analyzer cargo formatx 0.2.4 macros/tests/intermixed().() {
105 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("{name} {}", 1, name = 2);
106 }
107
108 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
109 fn widthrust-analyzer cargo formatx 0.2.4 macros/tests/width().() {
110 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {:5}!", "x");
111 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {:05}!", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().5);
112 }
113
114 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
115 fn zero_widthrust-analyzer cargo formatx 0.2.4 macros/tests/zero_width().() {
116 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("{:04}", 42);
117 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {:05}!", 5);
118 }
119
120 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
121 fn alignrust-analyzer cargo formatx 0.2.4 macros/tests/align().() {
122 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {:<5}!", "x");
123 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {:-<5}!", "x");
124 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {:^5}!", "x");
125 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {:>5}!", "x");
126 }
127
128 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
129 fn signrust-analyzer cargo formatx 0.2.4 macros/tests/sign().() {
130 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {:+}!", 5)
131 }
132
133 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
134 fn precisionrust-analyzer cargo formatx 0.2.4 macros/tests/precision().() {
135 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {0} is {1:.5}", "x", 0.01)
136 }
137
138 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
139 fn escapingrust-analyzer cargo formatx 0.2.4 macros/tests/escaping().() {
140 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatxrust-analyzer cargo formatx 0.2.4 macros/formatx!!("Hello {{}}").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Hello {{}}"));
141 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatxrust-analyzer cargo formatx 0.2.4 macros/formatx!!("{{ Hello").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{{ Hello"));
142 }
143
144 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
145 fn debugrust-analyzer cargo formatx 0.2.4 macros/tests/debug().() {
146 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("{} {:?}", 3, 4);
147 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("{} {:?}", 'a', 'b');
148 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("{} {:?}", "foo\n", "bar\n");
149 }
150
151 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
152 fn trailing_commasrust-analyzer cargo formatx 0.2.4 macros/tests/trailing_commas().() {
153 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello",);
154 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {}", "world",);
155 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {1} {0}", "1", "0",);
156 formatx_testrust-analyzer cargo formatx 0.2.4 macros/tests/formatx_test!!("Hello {name}", name = "John",);
157 }
158 }