ctb_formats_wfscan/
cli.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! CLI execution helpers for wfscan and wfparser.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use std::path::Path;
30
31pub fn wfparser<FRead>(
32    file: &Path,
33    read_file_or_stdin: FRead,
34) -> Result<ToolResult>
35where
36    FRead: Fn(&Path) -> Result<Vec<u8>>,
37{
38    let data = read_file_or_stdin(file)?;
39    let output = crate::wfparse(&data)?;
40    Ok(ToolResult::immediate_ok(output))
41}
42
43pub fn wfscan<FRead>(
44    file: &Path,
45    read_file_or_stdin: FRead,
46) -> Result<ToolResult>
47where
48    FRead: Fn(&Path) -> Result<Vec<u8>>,
49{
50    let data = read_file_or_stdin(file)?;
51    let output = crate::wfscan(&data)?;
52    Ok(ToolResult::immediate_ok(output))
53}
54
55#[cfg(test)]
56#[allow(
57    clippy::panic,
58    clippy::expect_used,
59    clippy::unwrap_used,
60    clippy::unwrap_in_result,
61    clippy::panic_in_result_fn,
62    clippy::indexing_slicing,
63    clippy::arithmetic_side_effects,
64    reason = "Standard repository test boilerplate"
65)]
66mod tests {
67    use super::*;
68
69    #[crate::ctb_test]
70    fn test_wfparser_wfscan_commands() {
71        let temp_dir = tempfile::tempdir().expect("Create temp dir");
72        let temp_file_path = temp_dir.path().join("wf_test.pan");
73        std::fs::write(&temp_file_path, b"(Hello <tag> World)")
74            .expect("Write temp file");
75
76        let parser_result =
77            super::wfparser(&temp_file_path, |p| Ok(std::fs::read(p)?))
78                .expect("Run parser command");
79        match parser_result {
80            ToolResult::Immediate { stdout, .. } => {
81                assert_eq!(
82                    String::from_utf8_lossy(&stdout),
83                    "(Hello   World)\n"
84                );
85            }
86            _ => panic!("Expected Immediate ToolResult"),
87        }
88
89        let scan_result =
90            super::wfscan(&temp_file_path, |p| Ok(std::fs::read(p)?))
91                .expect("Run scan command");
92        match scan_result {
93            ToolResult::Immediate { stdout, .. } => {
94                assert_eq!(String::from_utf8_lossy(&stdout), " hello world \n");
95            }
96            _ => panic!("Expected Immediate ToolResult"),
97        }
98    }
99}