ctb_formats_wfscan/
cli.rs1#[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}