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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//! Non-essential niceties
//!
//! # Closures support
//! There are several struct wrappers over closures which allow using closures
//! as `Fmt`s. All of them are defined for `FnMut` closures. The simplest form
//! of these is `Lazy`, which wraps a closure producing a single `Fmt` to
//! actually control the formatting. There are also several `AdHoc*`
//! structures, with arguments of each being a subset of arguments of the
//! `format` method on `Fmt`. The intent is to provide a way to get some
//! quick-and-dirty formatting without defining a new data type.
//!
//! Since all of these are defined not just for `Fn`s, but for `FnMut`s, it's
//! possible to do neat things like self-incrementing lists:
//! ```
//! use std::collections::HashMap;
//! 
//! use pfmt::{Fmt, FormatTable};
//! use pfmt::extras::Lazy;
//! 
//! let mut index = 0;
//! let mut counter = Lazy::new(move || { index += 1; index });
//! let mut table: HashMap<&str, &dyn Fmt> = HashMap::new();
//! table.insert("i", &counter);
//! let s = table.format("{i} - spam\n{i} - eggs").unwrap();
//! assert_eq!(&s, "1 - spam\n2 - eggs");
//! ```
//!
//! # Single-entry format tables
//! Sometimes all you need is a quick and dirty formatting, which clashes
//! somewhat with the nature of the format tables in the main module - their
//! construction is quite burdensome. Enter `Mono` and tuples. A `Mono` is a
//! tuple struct with a string key (or anything that derefs to a `str`) and
//! some `Fmt` value. It is also a format table. The following works:
//! ```
//! use pfmt::FormatTable;
//! use pfmt::extras::Mono;
//! 
//! let a = Mono("a", 5);
//! let b = Mono("b", "foobar");
//! let s = (&a, &b).format("{a}, {b}").unwrap();
//! assert_eq!(&s, "5, foobar");
//! ```
//!
//! They can be combined with hash tables and vectors in the same fashion, the
//! main module docs have an example.

use std::cell::RefCell;
use std::collections::HashMap;
use std::ops::Deref;

use {Fmt, SingleFmtError, FormatTable};

/* ---------- simple closure support ---------- */

/// A wrapper over a closure producing a `Fmt`.
///
/// Note that the closure can be `FnMut`, not just `Fn`.
/// Automatically honors any flags, arguments and options the produced `Fmt`
/// recognizes.
pub struct Lazy<T, F>
where
    T: Fmt,
    F: FnMut() -> T
{
    closure: RefCell<F>,
}

impl<T: Fmt, F: FnMut() -> T> Lazy<T, F> {
    pub fn new(closure: F) -> Self {
        Lazy {
            closure: RefCell::new(closure),
        }
    }
}

impl<T: Fmt, F: FnMut() -> T> Fmt for Lazy<T, F> {
    /// Call the wrapped closure to get a `Fmt`, then call `format` on it.
    fn format(
        &self,
        full_name: &[String],
        name: &[String],
        args: &[String],
        flags: &[char],
        options: &HashMap<String, String>,
    ) -> Result<String, SingleFmtError> {
        let mut r = self.closure.borrow_mut();
        let fmt = (&mut *r)();
        fmt.format(full_name, name, args, flags, options)
    }
}

/* ---------- ad-hoc closures with arguments ---------- */

/// A wrapper over a closure that accepts `full_name`, `name` and `args`
/// parameters of the `format` method on `Fmt`.
///
/// Note that the closure can be `FnMut`, not just `Fn`. 
/// Also note that while it is possible to call other `Fmt`s recursively from
/// the closure, the flags and options of the original `format` call will be
/// necessarily lost and will have to be replaced somehow by the closure.
pub struct AdHocArgs<F>
where
    F: FnMut(&[String], &[String], &[String]) -> Result<String, SingleFmtError>
{
    closure: RefCell<F>,
}

impl<F> AdHocArgs<F>
where
    F: FnMut(&[String], &[String], &[String]) -> Result<String, SingleFmtError>
{
    pub fn new(closure: F) -> Self {
        AdHocArgs {
            closure: RefCell::new(closure),
        }
    }
}

impl<F> Fmt for AdHocArgs<F>
where
    F: FnMut(&[String], &[String], &[String]) -> Result<String, SingleFmtError>
{
    fn format(
        &self,
        full_name: &[String],
        name: &[String],
        args: &[String],
        _flags: &[char],
        _options: &HashMap<String, String>,
    ) -> Result<String, SingleFmtError> {
        let mut closure = self.closure.borrow_mut();
        (&mut *closure)(full_name, name, args)
    }
}

/* ---------- ad-hoc closures with options ---------- */

/// A wrapper over a closure that accepts `full_name`, `name` and `options`
/// parameters of the `format` method on `Fmt`.
///
/// Note that the closure can be `FnMut`, not just `Fn`.
/// Also note that while it is possible to call other `Fmt`s recursively from
/// the closure, the arguments and flags of the original `format` call will be
/// lost and will have to be somehow replaced by the closure itself.
pub struct AdHocOpts<F>
where
    F: FnMut(&[String], &[String], &HashMap<String, String>) -> Result<String, SingleFmtError>
{
    closure: RefCell<F>,
}

impl<F> AdHocOpts<F>
where 
    F: FnMut(&[String], &[String], &HashMap<String, String>) -> Result<String, SingleFmtError>
{
    pub fn new(closure: F) -> Self {
        AdHocOpts {
            closure: RefCell::new(closure),
        }
    }
}

impl<F> Fmt for AdHocOpts<F>
where
    F: FnMut(&[String], &[String], &HashMap<String, String>) -> Result<String, SingleFmtError>
{
    fn format(
        &self,
        full_name: &[String],
        name: &[String],
        _args: &[String],
        _flags: &[char],
        options: &HashMap<String, String>,
    ) -> Result<String, SingleFmtError> {
        let mut closure = self.closure.borrow_mut();
        (&mut *closure)(full_name, name, options)
    }
}

/* ---------- ad-hoc closures with flags and options ---------- */

/// A wrapper over a closure that accepts `full_name`, `name`, `flags` and
/// `options` parameters of the `format` method on `Fmt`.
///
/// Note that the closure can be `FnMut`, not just `Fn`.
/// Also note that while it is possible to call other `Fmt`s from the closure,
/// the `args` parameter of the original `format` call will be lost and will
/// have to be replaced somehow by the closure.
pub struct AdHocFlagsOpts<F>
where
    F: FnMut(&[String], &[String], &[char], &HashMap<String, String>)
        -> Result<String, SingleFmtError>
{
    closure: RefCell<F>,
}

impl<F> AdHocFlagsOpts<F>
where 
    F: FnMut(&[String], &[String], &[char], &HashMap<String, String>)
        -> Result<String, SingleFmtError>
{
    pub fn new(closure: F) -> Self {
        AdHocFlagsOpts {
            closure: RefCell::new(closure),
        }
    }
}

impl<F> Fmt for AdHocFlagsOpts<F>
where
    F: FnMut(&[String], &[String], &[char], &HashMap<String, String>)
        -> Result<String, SingleFmtError>
{
    fn format(
        &self,
        full_name: &[String],
        name: &[String],
        _args: &[String],
        flags: &[char],
        opts: &HashMap<String, String>,
    ) -> Result<String, SingleFmtError> {
        let mut closure = self.closure.borrow_mut();
        (&mut *closure)(full_name, name, flags, opts)
    }
}

/* ---------- ad-hoc closures of maximum complexity ---------- */

/// A wrapper over a closure with the same signature as the `format` method on
/// `Fmt`.
///
/// The closure doesn't have to be `Fn`, it can be `FnMut`.
pub struct AdHocFull<F>
where
    F: FnMut(&[String], &[String], &[String], &[char], &HashMap<String, String>)
        -> Result<String, SingleFmtError>,
{
    closure: RefCell<F>,
}

impl<F> AdHocFull<F>
where
    F: FnMut(&[String], &[String], &[String], &[char], &HashMap<String, String>)
        -> Result<String, SingleFmtError>,
{
    pub fn new(closure: F) -> Self {
        AdHocFull {
            closure: RefCell::new(closure),
        }
    }
}

impl<F> Fmt for AdHocFull<F>
where
    F: FnMut(&[String], &[String], &[String], &[char], &HashMap<String, String>)
        -> Result<String, SingleFmtError>,
{
    fn format(
        &self,
        full_name: &[String],
        name: &[String],
        args: &[String],
        flags: &[char],
        options: &HashMap<String, String>,
    ) -> Result<String, SingleFmtError> {
        let mut closure = self.closure.borrow_mut();
        (&mut *closure)(full_name, name, args, flags, options)
    }
}

/* ---------- single-entry format tables ---------- */

/// A format table with a single entry, accessed by a string key.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct Mono<S: Deref<Target = str>, T: Fmt>(pub S, pub T);

impl<'a, S: Deref<Target = str>, T: Fmt + 'a> FormatTable<'a> for Mono<S, T> {
    type Item = &'a dyn Fmt;

    fn get_fmt(&'a self, name: &str) -> Option<Self::Item> {
        if name == self.0.deref() {
            Some(&self.1)
        } else {
            None
        }
    }
}

/* ---------- tests ---------- */

#[cfg(test)]
mod extras_tests {
    test_suite! {
        name closures;
        use std::cmp::Ordering;
        use std::collections::HashMap;
        use galvanic_assert::matchers::*;

        use {FormatTable, Fmt, SingleFmtError};
        use extras::*;
        use util;

        test lazy() {
            let i = Lazy::new(|| 10_i32);
            let mut table: HashMap<&str, &dyn Fmt> = HashMap::new();
            table.insert("i", &i);
            let s = table.format("{i}").expect("Failed to format");
            assert_that!(&s.as_str(), eq("10"));
        }

        test mutable_lazy() {
            let mut i = 0;
            let c = Lazy::new(move || { i += 1; i });
            let mut table: HashMap<&str, &dyn Fmt> = HashMap::new();
            table.insert("i", &c);
            let s = table.format("{i}, {i}, {i}").expect("Failed to format");
            assert_that!(&s.as_str(), eq("1, 2, 3"));
        }

        test ad_hoc_args() {
            // Convert the first argument to upper case. If there is no
            // argument, return '!!!'.
            let f = AdHocArgs::new(|_, _, args| {
                if args.is_empty() {
                    Ok("!!!".to_string())
                } else {
                    Ok(args[0].to_uppercase())
                }
            });
            let mut table: HashMap<&str, &dyn Fmt> = HashMap::new();
            table.insert("f", &f);
            let s = table.format("{f{asdf}}, {f}").expect("Failed to format");
            assert_that!(&s.as_str(), eq("ASDF, !!!"));
        }

        test ad_hoc_opts() {
            // Return 'defined' if 'opt' is set, 'undefined' otherwise.
            let f = AdHocOpts::new(|_, _, opts| {
                if opts.contains_key("opt") {
                    Ok("defined".to_string())
                } else {
                    Ok("undefined".to_string())
                }
            });
            let mut table: HashMap<&str, &dyn Fmt> = HashMap::new();
            table.insert("f", &f);
            let s = table.format("{f::opt=}, {f}").expect("Failed to format");
            assert_that!(&s.as_str(), eq("defined, undefined"));
        }

        test ad_hoc_full() {
            // This thing converts its first argument to upper case.
            let f = AdHocFull::new(|full_name, _, args: &[String], _, _| {
                if args.is_empty() {
                    Err(SingleFmtError::WrongNumberOfArguments(
                            util::join_name(full_name),
                            Ordering::Equal,
                            1))
                } else {
                    let s = &args[0];
                    Ok(s.to_uppercase())
                }
            });
            let mut table: HashMap<&str, &dyn Fmt> = HashMap::new();
            table.insert("f", &f);
            let s = table.format("{f{asdf}}").expect("Failed to format");
            assert_that!(&s.as_str(), eq("ASDF"));
        }

    }

    test_suite! {
        name mono;
        use std::collections::HashMap;
        use galvanic_assert::matchers::*;

        use {FormatTable, Fmt};
        use extras::Mono;

        test several_monos() {
            let a = Mono("a", 5_i32);
            let b = Mono("b", "foobar");
            let s = (&a, &b).format("{a}, {b}").expect("Failed to format");
            assert_that!(&s.as_str(), eq("5, foobar"));
        }

        test monos_and_hashmaps() {
            let a = Mono("foobar", 10);
            let t = {
                let mut res: HashMap<&str, &dyn Fmt> = HashMap::new();
                res.insert("a", &10);
                res
            };
            let s = (&a, &t).format("{foobar}, {a}").expect("Failed to format");
            assert_that!(&s.as_str(), eq("10, 10"));
        }

    }
}