query-forge 0.9.0

Run SQL queries and dataset diffs on XLSX/XML/CSV/JSON/JSONL/Markdown/HTML/Feather/Parquet inputs and export results as text, CSV, JSONL, Markdown, XML, HTML, XLSX, Feather, or Parquet
Documentation
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
404
405
406
407
use std::{fs, io::Write as _, path::Path, path::PathBuf};

use anyhow::{Result, anyhow, bail};

use crate::cli::OutputFormat;
use crate::clipboard;
use crate::input_spec::split_explicit_format;

/// Parses an optional `|type` suffix from a raw output-path string.
///
/// Returns `(Some(cleaned_path), Some(format_hint))` when a `|type` suffix is
/// present, `(original_path_as_buf, None)` when there is none.
///
/// This function only performs parsing — it does not validate whether the path
/// or format is legal (e.g. it does not reject clipboard paths with extensions).
/// Validation happens later in [`write_query_result`].
pub(crate) fn parse_output_path_spec(raw: Option<&Path>) -> (Option<PathBuf>, Option<String>) {
    let Some(raw_path) = raw else {
        return (None, None);
    };

    let path_str = raw_path.to_string_lossy();
    let (path_part, fmt) = split_explicit_format(&path_str);
    if let Some(fmt) = fmt {
        (
            Some(PathBuf::from(path_part)),
            Some(fmt.to_ascii_lowercase()),
        )
    } else {
        (Some(raw_path.to_path_buf()), None)
    }
}

pub(crate) fn write_query_result(
    result: &query_forge::QueryResult,
    output_path: Option<&Path>,
    explicit_format: Option<OutputFormat>,
    path_format_hint: Option<&str>,
) -> Result<()> {
    // For clipboard output, reject the old extension-based syntax.
    if let Some(path) = output_path {
        if clipboard::is_clipboard_path(path) && path.extension().is_some() {
            let ext = path
                .extension()
                .unwrap_or_default()
                .to_string_lossy()
                .to_ascii_lowercase();
            bail!(
                "clipboard output must not use a file extension for format selection; \
                 use '@clipboard|{ext}' instead"
            );
        }
    }

    match resolve_output_format(output_path, explicit_format, path_format_hint)? {
        OutputFormat::Table | OutputFormat::Text => {
            write_streamed_text_output(output_path, |writer| {
                query_forge::write_text(result, writer)
            })?;
        }
        OutputFormat::Csv => {
            write_streamed_text_output(output_path, |writer| {
                query_forge::write_csv(result, writer)
            })?;
        }
        OutputFormat::Json => {
            let rendered = query_forge::render_json(result);
            write_or_print(&rendered, output_path)?;
        }
        OutputFormat::Jsonl => {
            write_streamed_text_output(output_path, |writer| {
                query_forge::write_jsonl(result, writer)
            })?;
        }
        OutputFormat::Markdown => {
            let rendered = query_forge::render_markdown(result);
            write_or_print(&rendered, output_path)?;
        }
        OutputFormat::Html => {
            let rendered = query_forge::render_html(result);
            write_or_print(&rendered, output_path)?;
        }
        OutputFormat::Xml => {
            let rendered = query_forge::render_xml(result);
            write_or_print(&rendered, output_path)?;
        }
        OutputFormat::Xlsx => {
            let output_path = output_path
                .ok_or_else(|| anyhow!("--output is required when --format xlsx is selected"))?;
            if clipboard::is_clipboard_path(output_path) {
                bail!("clipboard output does not support xlsx; write to a file instead");
            }
            query_forge::write_xlsx(result, output_path)?;
        }
        OutputFormat::Feather => {
            let output_path = output_path
                .ok_or_else(|| anyhow!("--output is required when --format feather is selected"))?;
            if clipboard::is_clipboard_path(output_path) {
                bail!("clipboard output does not support feather; write to a file instead");
            }
            query_forge::write_feather(result, output_path)?;
        }
        OutputFormat::Parquet => {
            let output_path = output_path
                .ok_or_else(|| anyhow!("--output is required when --format parquet is selected"))?;
            if clipboard::is_clipboard_path(output_path) {
                bail!("clipboard output does not support parquet; write to a file instead");
            }
            query_forge::write_parquet(result, output_path)?;
        }
    }

    Ok(())
}

fn write_streamed_text_output<F>(output_path: Option<&Path>, mut render: F) -> Result<()>
where
    F: FnMut(&mut dyn std::io::Write) -> std::io::Result<()>,
{
    if let Some(output_path) = output_path {
        if clipboard::is_clipboard_path(output_path) {
            clipboard::ensure_supported_clipboard_output(output_path)?;
            let mut rendered = Vec::new();
            render(&mut rendered)?;
            let rendered = String::from_utf8(rendered)
                .map_err(|error| anyhow!("rendered output was not valid UTF-8: {error}"))?;
            clipboard::write_text(output_path, &rendered)?;
        } else {
            let file = fs::File::create(output_path)?;
            let mut writer = std::io::BufWriter::new(file);
            render(&mut writer)?;
            writer.flush()?;
        }
    } else {
        let stdout = std::io::stdout();
        let mut stdout_lock = stdout.lock();
        render(&mut stdout_lock)?;
        stdout_lock.write_all(b"\n")?;
        stdout_lock.flush()?;
    }

    Ok(())
}

fn write_or_print(rendered: &str, output_path: Option<&Path>) -> Result<()> {
    if let Some(output_path) = output_path {
        if clipboard::is_clipboard_path(output_path) {
            clipboard::ensure_supported_clipboard_output(output_path)?;
            clipboard::write_text(output_path, rendered)?;
        } else {
            fs::write(output_path, rendered)?;
        }
    } else {
        println!("{rendered}");
    }

    Ok(())
}

pub(crate) fn resolve_output_format(
    output: Option<&Path>,
    format: Option<OutputFormat>,
    path_format_hint: Option<&str>,
) -> Result<OutputFormat> {
    if let Some(format) = format {
        if matches!(
            format,
            OutputFormat::Xlsx | OutputFormat::Feather | OutputFormat::Parquet
        ) && output.is_none()
        {
            let name = if matches!(format, OutputFormat::Xlsx) {
                "xlsx"
            } else if matches!(format, OutputFormat::Feather) {
                "feather"
            } else {
                "parquet"
            };
            bail!("--output is required when --format {name} is selected");
        }
        return Ok(format);
    }

    // Resolve the effective extension/format token: |type hint takes priority
    // over the file extension.
    let ext = path_format_hint
        .or_else(|| output.and_then(|p| p.extension()).and_then(|e| e.to_str()))
        .unwrap_or("");

    if ext.eq_ignore_ascii_case("xlsx") {
        return Ok(OutputFormat::Xlsx);
    }

    if ext.eq_ignore_ascii_case("feather") {
        return Ok(OutputFormat::Feather);
    }

    if ext.eq_ignore_ascii_case("parquet") {
        return Ok(OutputFormat::Parquet);
    }

    if ext.eq_ignore_ascii_case("csv") {
        return Ok(OutputFormat::Csv);
    }

    if ext.eq_ignore_ascii_case("json") {
        return Ok(OutputFormat::Json);
    }

    if ext.eq_ignore_ascii_case("jsonl") || ext.eq_ignore_ascii_case("ndjson") {
        return Ok(OutputFormat::Jsonl);
    }

    if ext.eq_ignore_ascii_case("md") || ext.eq_ignore_ascii_case("markdown") {
        return Ok(OutputFormat::Markdown);
    }

    if ext.eq_ignore_ascii_case("xml") {
        return Ok(OutputFormat::Xml);
    }

    if ext.eq_ignore_ascii_case("html") || ext.eq_ignore_ascii_case("htm") {
        return Ok(OutputFormat::Html);
    }

    Ok(OutputFormat::Table)
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use crate::cli::OutputFormat;
    use crate::clipboard;

    use super::{parse_output_path_spec, resolve_output_format, write_query_result};

    #[test]
    fn infers_output_format_from_extension() {
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.xlsx")), None, None).expect("xlsx format"),
            OutputFormat::Xlsx
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.feather")), None, None)
                .expect("feather format"),
            OutputFormat::Feather
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.parquet")), None, None)
                .expect("parquet format"),
            OutputFormat::Parquet
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.csv")), None, None).expect("csv format"),
            OutputFormat::Csv
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.jsonl")), None, None)
                .expect("jsonl format"),
            OutputFormat::Jsonl
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.json")), None, None).expect("json format"),
            OutputFormat::Json
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.md")), None, None).expect("md format"),
            OutputFormat::Markdown
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.html")), None, None).expect("html format"),
            OutputFormat::Html
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.htm")), None, None).expect("htm format"),
            OutputFormat::Html
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.txt")), None, None).expect("text format"),
            OutputFormat::Table
        ));
        assert!(matches!(
            resolve_output_format(None, None, None).expect("default format"),
            OutputFormat::Table
        ));
    }

    #[test]
    fn explicit_path_hint_overrides_extension() {
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.txt")), None, Some("json"))
                .expect("json hint overrides txt extension"),
            OutputFormat::Json
        ));
        assert!(matches!(
            resolve_output_format(Some(Path::new("result.txt")), None, Some("CSV"))
                .expect("CSV hint case-insensitive"),
            OutputFormat::Csv
        ));
    }

    #[test]
    fn explicit_format_flag_overrides_path_hint() {
        assert!(matches!(
            resolve_output_format(
                Some(Path::new("result.txt")),
                Some(OutputFormat::Json),
                Some("csv")
            )
            .expect("--format wins over |type hint"),
            OutputFormat::Json
        ));
    }

    #[test]
    fn requires_output_path_for_feather_format() {
        let error = resolve_output_format(None, Some(OutputFormat::Feather), None)
            .expect_err("feather format should require output");
        assert!(
            error
                .to_string()
                .contains("--output is required when --format feather is selected")
        );
    }

    #[test]
    fn parse_output_path_spec_strips_pipe_type() {
        let (path, hint) = parse_output_path_spec(Some(Path::new("result.txt|json")));
        assert_eq!(path.as_deref().and_then(|p| p.to_str()), Some("result.txt"));
        assert_eq!(hint.as_deref(), Some("json"));
    }

    #[test]
    fn parse_output_path_spec_no_pipe_returns_original() {
        let (path, hint) = parse_output_path_spec(Some(Path::new("result.csv")));
        assert_eq!(path.as_deref().and_then(|p| p.to_str()), Some("result.csv"));
        assert_eq!(hint, None);
    }

    #[test]
    fn parse_output_path_spec_none_returns_none() {
        let (path, hint) = parse_output_path_spec(None);
        assert!(path.is_none());
        assert!(hint.is_none());
    }

    #[test]
    fn writes_text_output_to_clipboard() {
        clipboard::set_test_text("");
        let result = query_forge::QueryResult {
            columns: vec!["product".to_owned()],
            rows: vec![vec![query_forge::QueryValue::Text("Keyboard".to_owned())]],
        };

        write_query_result(
            &result,
            Some(Path::new("@clipboard")),
            Some(OutputFormat::Csv),
            None,
        )
        .expect("clipboard output should succeed");

        let clipboard_text = clipboard::get_test_text();
        assert!(clipboard_text.contains("product"));
        assert!(clipboard_text.contains("Keyboard"));
    }

    #[test]
    fn clipboard_with_extension_is_rejected_on_output() {
        let result = query_forge::QueryResult {
            columns: vec!["product".to_owned()],
            rows: vec![vec![query_forge::QueryValue::Text("Keyboard".to_owned())]],
        };

        let error = write_query_result(
            &result,
            Some(Path::new("@clipboard.csv")),
            Some(OutputFormat::Csv),
            None,
        )
        .expect_err("clipboard extension syntax should be rejected");

        assert!(error.to_string().contains("@clipboard|csv"));
    }

    #[test]
    fn rejects_binary_output_to_clipboard() {
        let result = query_forge::QueryResult {
            columns: vec!["product".to_owned()],
            rows: vec![vec![query_forge::QueryValue::Text("Keyboard".to_owned())]],
        };

        let error = write_query_result(
            &result,
            Some(Path::new("@clipboard")),
            None,
            Some("parquet"),
        )
        .expect_err("clipboard parquet output should fail");

        assert!(
            error
                .to_string()
                .contains("clipboard output does not support parquet")
        );
    }
}