tomq 0.1.2

jq, but from TOML
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
400
401
402
403
use crate::dociter::TomqIter;
use crate::{output_to_stdout, DocumentIterator, TomlOpts};
use serde_json::{Map, Value};
use std::io::{Cursor, ErrorKind, Read};
use toml_edit::visit_mut::VisitMut;
use toml_edit::{ArrayOfTables, Document, Item};

use crate::error::Result;
use crate::error::TomqError;
use crate::opts::RootKey;
use crate::output::{output_to_bat, CopyFromReader};
use crate::toml_pp::Pretty;

/// Parses TOML document lazily from [`R` reader][Read] as it is consumed from
/// the returned iterator.
pub(crate) fn parse_toml_document<R: Read>(
    reader: R,
    opts: &TomlOpts,
) -> impl Iterator<Item = Result<Document>> {
    DocumentIterator::new(
        reader,
        format!("\n{}", opts.multi_doc_separator),
        opts.skip_invalid,
    )
}

/// Converts Json Document to Toml Document lazily from [`R` reader][Read]
/// as it is consumed from the returned iterator.
pub(crate) fn create_toml_doc_from_json<R: Read>(
    reader: R,
    ignore_error: bool,
    null_to_empty: bool,
    fallback_key: Option<String>,
) -> impl TomqIter<Document> {
    let des = serde_json::Deserializer::from_reader(reader);
    create_toml_doc_from_json_values(
        des.into_iter::<Value>()
            .map(|r| r.map_err(|e| TomqError::from(e))),
        ignore_error,
        null_to_empty,
        fallback_key,
    )
}

/// Converts Json Document to Toml Document lazily from [`TomqIter<serde_json::Value>` iterator][TomqIter]
/// as it is consumed from the returned iterator.
pub(crate) fn create_toml_doc_from_json_values(
    values: impl TomqIter<Value>,
    ignore_error: bool,
    null_to_empty: bool,
    fallback_key: Option<String>,
) -> impl TomqIter<Document> {
    values
        .map(move |v| {
            v.and_then(|value| match value {
                Value::Null if null_to_empty => Ok(Document::new()),
                value @ _ if !value.is_object() && fallback_key.is_some() => {
                    let value = Value::Object(Map::from_iter(
                        Some((fallback_key.clone().unwrap(), value)).into_iter(),
                    ));
                    toml_edit::ser::to_document(&value)
                        .map_err(|e| TomqError::TomlFromJson(e, value))
                }
                _ => toml_edit::ser::to_document(&value)
                    .map_err(|e| TomqError::TomlFromJson(e, value)),
            })
        })
        .filter(move |f| !ignore_error || f.is_ok())
}

/// Converts Json Document (or documents) to Toml Documents from [`R` reader][Read] and
/// outputs to stdout.
///
/// This is the equivalent to calling [`create_toml_doc_from_json`] and passing it to [`copy_toml`]
/// as the [`TomqIter<Document>`][`TomqIter`] with a selected [writer][`CopyFromReader`] based on whether it should
/// pipe the output to `bat` or stdout.
///
/// If you want to copy the toml document from a [`TomqIter<Document>`][`TomqIter`] to a [writer][`CopyFromReader`],
/// use [`copy_toml`].
pub(crate) fn print_toml_from_json_to_stdout<R: Read>(
    read: R,
    pretty: bool,
    root_key: &RootKey,
    fallback_key: Option<String>,
    multi_doc_separator: &str,
    skip_invalid: bool,
    null_to_empty_doc: bool,
    bat: bool,
) -> Result<()> {
    let toml = create_toml_doc_from_json(read, skip_invalid, null_to_empty_doc, fallback_key);
    print_toml(toml, pretty, root_key, &multi_doc_separator, bat)?;
    Ok(())
}

/// Converts Json Document (or documents) to Toml Documents from [`TomqIter<serde_json::Value>` iterator][TomqIter] and
/// outputs to stdout.
///
/// This is the equivalent to calling [`create_toml_doc_from_json_values`] and passing it to [`copy_toml`]
/// as the [`TomqIter<Document>`][`TomqIter`] with a selected [writer][`CopyFromReader`] based on whether it should
/// pipe the output to `bat` or stdout.
///
/// If you want to copy the toml document from a [`TomqIter<Document>`][`TomqIter`] to a [writer][`CopyFromReader`],
/// use [`copy_toml`].
pub(crate) fn print_toml_from_json_values(
    values: impl TomqIter<Value>,
    pretty: bool,
    root_key: &RootKey,
    fallback_key: Option<String>,
    multi_doc_separator: &str,
    skip_invalid: bool,
    null_to_empty_doc: bool,
    bat: bool,
) -> Result<()> {
    let toml =
        create_toml_doc_from_json_values(values, skip_invalid, null_to_empty_doc, fallback_key);
    print_toml(toml, pretty, root_key, &multi_doc_separator, bat)?;
    Ok(())
}

/// Converts Json Document (or documents) to Toml Documents from [`R` reader][Read] and
/// outputs to stdout.
///
/// This is the equivalent to calling [`create_toml_doc_from_json`] and passing it to [`copy_toml`]
/// as the [`TomqIter<Document>`][`TomqIter`] with a selected [writer][`CopyFromReader`] based on whether it should
/// pipe the output to `bat` or stdout.
///
/// If you want to copy the toml document from a [`TomqIter<Document>`][`TomqIter`] to a [writer][`CopyFromReader`],
/// use [`copy_toml`].
pub(crate) fn print_toml(
    toml: impl TomqIter<Document>,
    pretty: bool,
    root_key: &RootKey,
    multi_doc_separator: &str,
    bat: bool,
) -> Result<()> {
    if bat {
        copy_toml(
            toml,
            output_to_bat("toml"),
            pretty,
            root_key,
            &multi_doc_separator,
        )?;
    } else {
        copy_toml(
            toml,
            output_to_stdout(),
            pretty,
            root_key,
            &multi_doc_separator,
        )?;
    }
    Ok(())
}

/// Copies the toml from the [reader][DocIter] to the [writer][CopyFromReader].
pub(crate) fn copy_toml(
    reader: impl TomqIter<Document>,
    mut writer: impl CopyFromReader,
    pretty: bool,
    root_key: &RootKey,
    multi_doc_separator: &str,
) -> Result<()> {
    match root_key {
        RootKey::Flatten => {
            let txt = stream_toml_with_multi_doc(reader, pretty, multi_doc_separator.to_string())
                .map(|s| s.map(|s| Cursor::new(s.into_bytes())));

            writer.copy_from_reader(txt.iter_as_read())
        }
        RootKey::Key(key) => {
            let txt = copy_toml_with_root_key(reader, pretty, key)
                .map(|s| s.map(|s| Cursor::new(s.into_bytes())));
            writer.copy_from_reader(txt.iter_as_read())
        }
    }
}

/// Takes Toml documents from the [reader][DocIter] and lazily transforms those documents into
/// multi-documents using the [Tomq Multi Document Extension]() (non-Toml spec compliant)
/// as the returned iterator is consumed.
///
/// This creates a **lazy** iterator that only walks the input [`iter`][`TomqIter`] when
/// the returned iterator is advanced, this way it avoids pre-allocating memory until it is
/// really needed.
fn stream_toml_with_multi_doc(
    iter: impl TomqIter<Document>,
    pretty: bool,
    multi_doc_separator: String,
) -> impl TomqIter<String> {
    iter.enumerate().map(move |(idx, v)| {
        v.and_then(|mut doc| toml_doc_to_string(&mut doc, pretty))
            .map(|s| {
                if idx > 0 {
                    format!("\n{}\n{}", multi_doc_separator.clone(), s)
                } else {
                    s
                }
            })
    })
}

/// Takes Toml documents from the [reader][DocIter] and adds those documents into an
/// [ArrayOfTables](https://toml.io/en/v1.0.0#array-of-tables)
/// with a common key (Toml spec compliant) as the returned iterator is consumed.
///
/// This operation is not **lazy** like the other ones, at the moment it computes all the values
/// and add them to a single [ArrayOfTables](https://toml.io/en/v1.0.0#array-of-tables).
///
/// TODO: Make it lazy. The fix is very easy to say, since the way Toml is designed
/// TODO: we don't need to know about the others tables that belongs to the same array,
/// TODO: we just need to add a `[[{KEY}]]` before the table and prepend the key in all the others.
fn copy_toml_with_root_key(
    iter: impl TomqIter<Document>,
    pretty: bool,
    key: &str,
) -> impl TomqIter<String> {
    let key = key.to_string();
    Some(iter.fold(Ok(Vec::<Document>::new()), |acc, v| match acc {
        Ok(mut tab) => match v {
            Ok(v) => {
                tab.push(v);
                Ok(tab)
            }
            Err(e) => Err(e),
        },
        Err(e) => Err(e),
    }))
    .into_iter()
    .map(move |v| {
        v.map(|v| match v.len() {
            0 => Document::new(),
            // NOTE: Always use common key when asked for, to avoid unexpected behavior, even if
            // we correctly document this, people still might get confused when it happens.
            //1 => v.into_iter().next().unwrap(),
            _ => {
                let mut doc = Document::new();
                let table = doc.as_table_mut();
                let mut arr = ArrayOfTables::new();
                for x in v.into_iter() {
                    arr.push(x.as_table().clone());
                }
                table.insert(key.as_str(), Item::ArrayOfTables(arr));
                doc
            }
        })
    })
    .map(move |v| v.and_then(|mut doc| toml_doc_to_string(&mut doc, pretty)))
}

/// Produces toml output from the document, using a forked version of `toml_edit` pretty printing
/// code (since it is not exposed to other crates).
fn toml_doc_to_string(value: &mut Document, pretty_print: bool) -> Result<String> {
    if pretty_print {
        Pretty.visit_document_mut(value);
        Ok(value.to_string())
    } else {
        Ok(value.to_string())
    }
}

/// Simple helper that interfaces [`Read::read`] calls
/// with [`Iterator::<Item = Result<V>>::next`][`Iterator::next`].
struct IterToRead<I: Iterator, V> {
    iter: I,
    current: Option<V>,
}

impl<I: Iterator, V> IterToRead<I, V> {
    fn new(iter: I) -> Self {
        Self {
            iter,
            current: None,
        }
    }
}

trait IterAsRead: Iterator {
    fn iter_as_read<V>(self) -> IterToRead<Self, V>
    where
        Self: Sized,
    {
        IterToRead::new(self)
    }
}

impl<I> IterAsRead for I where I: Iterator {}

impl<I, V> Read for IterToRead<I, V>
where
    I: Iterator<Item = Result<V>>,
    V: Read,
{
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        loop {
            match self.current.as_mut() {
                None => match self.iter.next() {
                    None => return Ok(0),
                    Some(n) => {
                        self.current =
                            Some(n.map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e))?);
                    }
                },
                Some(r) => {
                    let n = r.read(buf)?;
                    if n == 0 {
                        self.current = None;
                    } else {
                        return Ok(n);
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::opts::RootKey;
    use crate::{parse_toml_document, TomlOpts};
    use std::io::{BufReader, Cursor};

    macro_rules! assert_ok {
        ($cond:expr $(,)?) => {
            match $cond {
                Some(Ok(d)) => {
                    d
                },
                Some(Err(e)) => {
                    assert!(false, "Unexpected error: {}", e)
                },
                None => panic!(),
            }
        };
        ($cond:expr, $($arg:tt)+) => {
            match $cond {
                Some(Ok(d)) => {
                    d
                },
                Some(Err(e)) => {
                    panic!("Expected successful result, got error: {}. Message: {}", e, format!($($arg)+))
                },
                None => panic!($($arg)+),
            }
        };
    }

    macro_rules! assert_not_empty {
        ($cond:expr $(,)?) => {
            assert!(assert_ok!($cond).iter().next().is_some());
        };
        ($cond:expr, $($arg:tt)+) => {
            assert!(assert_ok!($cond, $($arg)+).iter().next().is_some(), $($arg)+);
        };
    }

    macro_rules! assert_empty {
        ($cond:expr $(,)?) => {
            assert!(assert_ok!($cond).iter().next().is_none());
        };
        ($cond:expr, $($arg:tt)+) => {
            assert!(assert_ok!($cond, $($arg)+).iter().next().is_none(), $($arg)+);
        };
    }

    /// Test against the strange corner case which the last empty document is treated
    /// as an entire toml document instead of multi-document separator.
    #[test]
    fn multi_document_last_empty() {
        let doc = r#"
name = "tomq"

---
name = "tomq2"

---
"#;
        let reader = BufReader::with_capacity(1, Cursor::new(doc.as_bytes()));
        let mut doc = parse_toml_document(
            reader,
            &TomlOpts {
                jq_filter: None,
                toml_it: false,
                bat: false,
                retranscode: false,
                skip_invalid: false,
                single_doc: false,
                null_to_empty_doc: false,
                multi_doc_separator: "---".to_string(),
                root_key: RootKey::Flatten,
                fallback_key: None,
                pretty_print: false,
                jq_args: vec![],
            },
        );

        assert_not_empty!(doc.next(), "First document parse");
        assert_not_empty!(doc.next(), "Second document parse");
        assert_empty!(doc.next(), "Third empty document parse");
        assert!(doc.next().is_none(), "Should stop parsing");
        assert!(doc.next().is_none(), "Should stop parsing");
    }
}