1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//! A crate to format and print strings with an embedded rust expression, 
//! similar to [f-string formatting in Python](https://docs.python.org/3/tutorial/inputoutput.html).
//! # Examples
//!
//! Using `:?` modifier.
//! ```
//! use expression_format::ex_format;
//! let v = vec![1, 2, 3];
//! assert_eq!(ex_format!("v = {:?v}"), "v = [1, 2, 3]");
//! ```
//!
//! ---
//!
//! Using other modifiers ([`std:fmt`](https://doc.rust-lang.org/std/fmt/index.html) for details).
//! ```
//! use expression_format::ex_format;
//! // Space after format specs if it doesn't ends in ?
//! assert_eq!(ex_format!(r#"Hello {:-<5 "x"}!"#), "Hello x----!");
//! assert_eq!(ex_format!("{:.5 12.3}"), "12.30000");
//! assert_eq!(ex_format!("{:#010x 27}!"), "0x0000001b!");
//! ```
//! No support for `*` and `$` parameters.
//! 
//! ---
//! 
//! Printing the contents of fields.
//! ```
//! use expression_format::ex_format;
//! let arg = ["ipsum", "sit"];
//! assert_eq!(ex_format!("lorem {arg[0]} dolor {arg[1]} amet"), "lorem ipsum dolor sit amet");
//! ```
//!
//! ---
//!
//! Short version of [`ex_format!`](macro.ex_format.html) with a complex expression.
//! ```
//! use expression_format::short::exf;
//!
//! assert_eq!(
//!     exf!(r#"Hello { { // Space after the first { since {{ is an escape sequence.
//!             let first = "Wo";
//!             let second = "rld";
//!             let mut result = String::from(first);
//!             result.push_str(second);
//!             result
//!      }}!"#),
//!     "Hello World!"
//! );
//! ```
//!
//! ---
//!
//! Print to standard output with a new line.
//! ```
//! use expression_format::short::expl; // Short name version of ex_println!
//! #[derive(Debug)]
//! struct Point {x: i32, y: i32}
//!
//! expl!("value of point = {:?Point {x: 1 + 2, y: 3 * 4 }}");
//! // stdout: value of point = Point { x: 3, y: 12 }
//! ```
//!
//! ---
//!
//! Escape brackets with `{{` and `}}`.
//! ```
//! use expression_format::short::exf;
//! let value = 10;
//! assert_eq!(exf!("{{value}} = {value}"), "{value} = 10");
//! ```

/// Formats and prints to std error any valid rust expression in a string.
///
/// Same as [`eprint!`](https://doc.rust-lang.org/std/macro.eprint.html) but with embedded parameters.
pub use expression_format_impl::ex_eprint;
/// Formats and prints to std error any valid rust expression in a string with a new line at the end.
///
/// Same as [`eprintln!`](https://doc.rust-lang.org/std/macro.eprintln.html) but with embedded parameters.
pub use expression_format_impl::ex_eprintln;
/// Formats any valid rust expression in a string.
///
/// Same as [`format!`](https://doc.rust-lang.org/std/macro.format.html) but with embedded parameters.
///
/// # Example
/// ```
/// use expression_format::ex_format;
/// let arg = "ipsum";
/// assert_eq!(ex_format!("lorem {arg}"), "lorem ipsum");
/// ```
pub use expression_format_impl::ex_format;
/// Formats and prints to std out any valid rust expression in a string.
///
/// Same as [`print!`](https://doc.rust-lang.org/std/macro.print.html) but with embedded parameters.
pub use expression_format_impl::ex_print;
/// Formats and prints to std out any valid rust expression in a string with a new line at the end.
///
/// Same as [`println!`](https://doc.rust-lang.org/std/macro.println.html) but with embedded parameters.
pub use expression_format_impl::ex_println;

/// Short name versions
pub mod short {
    /// Short name version of [`ex_eprint!`](../macro.ex_eprint.html)
    pub use expression_format_impl::ex_eprint as exep;
    /// Short name version of [`ex_format!`](../macro.ex_format.html)
    pub use expression_format_impl::ex_format as exf;
    /// Short name version of [`ex_print!`](../macro.ex_print.html)
    pub use expression_format_impl::ex_print as exp;
    /// Short name version of [`ex_println!`](../macro.ex_println.html)
    pub use expression_format_impl::ex_println as expl;
    /// Short name version of [`ex_eprintln!`](../macro.ex_eprintln.html)
    pub use expression_format_impl::ex_eprintln as exepl;
}

#[cfg(test)]
mod tests {
    use crate::short::exf;

    #[test]
    fn test_single_argument() {
        let r = "ipsum";
        assert_eq!(exf!("lorem {r} dolor"), "lorem ipsum dolor");
    }

    #[test]
    fn test_raw_string() {
        assert_eq!(exf!(r#"lorem {r"{ipsum}"}"#), "lorem {ipsum}");
    }

    #[test]
    fn test_raw_string_with_depth() {
        assert_eq!(exf!(r###"lorem {r##"{ipsum"#}"##}"###), r##"lorem {ipsum"#}"##);
    }

    #[test]
    fn test_no_argument() {
        assert_eq!(exf!("lorem ipsum"), "lorem ipsum");
    }

    #[test]
    fn test_multiple_arguments() {
        let args = ["lorem ", "ipsum ", "dolor"];
        assert_eq!(exf!("{args[0]}{args[1]}{args[2]}"), "lorem ipsum dolor");
    }

    #[test]
    fn test_string() {
        assert_eq!(exf!(r#"lorem {"ip\"sum"}"#), "lorem ip\"sum");
    }

    #[test]
    fn test_char() {
        assert_eq!(exf!(r#"lorem {'\''}ipsum'"#), "lorem 'ipsum'");
    }

    #[test]
    fn test_line_comment() {
        assert_eq!(
            exf!(r#"lorem { {
                // line comment
                "ipsum"
            }}"#),
            "lorem ipsum"
        );
    }

    #[test]
    fn test_struct_in_expression() {
        #[derive(Debug)]
        struct Point {
            x: i32,
            y: i32,
        }

        assert_eq!(
            exf!("value of point = {:?Point {x: 1 + 2, y: 3 * 4}}"),
            "value of point = Point { x: 3, y: 12 }"
        );
    }

    #[test]
    fn test_escape_brackets() {
        let arg = "lorem";
        assert_eq!(exf!("{{{arg}}} {{{{ipsum}}}}"), "{lorem} {{ipsum}}");
    }

    #[test]
    fn test_block_comment() {
        assert_eq!(exf!(
            r#"lorem {/*/*inside comment*/still inside comment*/"ipsum"}"#),
            "lorem ipsum");
    }

    #[test]
    fn test_function_with_lifetime() {
        assert_eq!(
            exf!(
                r#"{
                {
                    fn foo() -> &'static str {
                        "lorem"
                    } 
                    foo() 
                }
            } ipsum"#
            ),
            "lorem ipsum"
        );
    }

    #[test]
    fn test_number_format_width() {
        assert_eq!(
            exf!("{:04 42}"),
            "0042"
        );
    }

    #[test]
    fn test_text_format_width() {
        assert_eq!(
            exf!(r#"Hello {:5 "x"}!"#),
            "Hello x    !"
        );
    }

    #[test]
    fn test_text_format_width_left_aligned() {
        assert_eq!(
            exf!(r#"Hello {:<5 "x"}!"#),
            "Hello x    !"
        );
    }

    #[test]
    fn test_text_format_width_left_aligned_with_dash() {
        assert_eq!(
            exf!(r#"Hello {:-<5 "x"}!"#),
            "Hello x----!"
        );
    }

    #[test]
    fn test_text_format_width_center_aligned() {
        assert_eq!(
            exf!(r#"Hello {:^5 "x"}!"#),
            "Hello   x  !"
        );
    }

    #[test]
    fn test_text_format_width_right_aligned() {
        assert_eq!(
            exf!(r#"Hello {:>5 "x"}!"#),
            "Hello     x!"
        );
    }

    #[test]
    fn test_sign() {
        assert_eq!(exf!("Hello {:+ 5}!"), "Hello +5!");
    }

    #[test]
    fn test_sharp_x() {
        assert_eq!(exf!("{:#x 27}!"), "0x1b!");
    }

    #[test]
    fn test_sharp_x_with_width() {
        assert_eq!(exf!("{:#010x 27}!"), "0x0000001b!");
    }

    #[test]
    fn test_precission() {
        assert_eq!(exf!("{:.5 12.3}"), "12.30000");
    }

    #[test]
    fn test_format_alignment_with_char() {
        assert_eq!(exf!(r#"{:'>10 "test"}"#), "''''''test");
    }

    #[test]
    fn test_format_alignment_with_quotes() {
        assert_eq!(exf!(r#"{:"<10 "test"}"#), r#"test"""""""#);
    }

    #[test]
    fn test_format_alignment_with_space() {
        assert_eq!(exf!(r#"{: <10 "test"}"#), r#"test      "#);
    }
}