ctb_formats_checksum/
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 checksum computations.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use crate as ctb_formats_checksum;
30use crate::HashAlgorithm;
31use std::path::Path;
32
33pub fn csum<FRead>(
34    algo: HashAlgorithm,
35    file: &Path,
36    prefix_0x: bool,
37    read_file_or_stdin: FRead,
38) -> Result<ToolResult>
39where
40    FRead: Fn(&Path) -> Result<Vec<u8>>,
41{
42    let data = read_file_or_stdin(file)?;
43    let output = format!(
44        "{}\n",
45        ctb_formats_checksum::hash_hex(&data, algo, prefix_0x)
46    );
47    Ok(ToolResult::immediate_ok(output.into_bytes()))
48}
49
50#[cfg(test)]
51#[allow(
52    clippy::panic,
53    clippy::expect_used,
54    clippy::unwrap_used,
55    clippy::unwrap_in_result,
56    clippy::panic_in_result_fn,
57    clippy::indexing_slicing,
58    clippy::arithmetic_side_effects,
59    reason = "Standard repository test boilerplate"
60)]
61mod tests {
62    use super::*;
63
64    #[crate::ctb_test]
65    fn test_csum_command() {
66        let temp_dir = tempfile::tempdir().expect("Create temp dir");
67        let temp_file_path = temp_dir.path().join("csum_test_temp.txt");
68        std::fs::write(&temp_file_path, b"hello world")
69            .expect("Write temp file");
70
71        let result =
72            super::csum(HashAlgorithm::XxHash32, &temp_file_path, false, |p| {
73                Ok(std::fs::read(p)?)
74            })
75            .expect("Run csum command");
76        match result {
77            ToolResult::Immediate { stdout, .. } => {
78                assert_eq!(String::from_utf8_lossy(&stdout), "cebb6622\n");
79            }
80            _ => panic!("Expected Immediate ToolResult"),
81        }
82
83        let result_0x =
84            super::csum(HashAlgorithm::XxHash32, &temp_file_path, true, |p| {
85                Ok(std::fs::read(p)?)
86            })
87            .expect("Run csum command");
88        match result_0x {
89            ToolResult::Immediate { stdout, .. } => {
90                assert_eq!(String::from_utf8_lossy(&stdout), "0xcebb6622\n");
91            }
92            _ => panic!("Expected Immediate ToolResult"),
93        }
94
95        let result_fnv =
96            super::csum(HashAlgorithm::Fnv1a_64, &temp_file_path, false, |p| {
97                Ok(std::fs::read(p)?)
98            })
99            .expect("Run csum command with fnv1a_64");
100        match result_fnv {
101            ToolResult::Immediate { stdout, .. } => {
102                assert_eq!(
103                    String::from_utf8_lossy(&stdout),
104                    format!(
105                        "{}\n",
106                        ctb_formats_checksum::fnv::fnv1a_64_hex(b"hello world")
107                    )
108                );
109            }
110            _ => panic!("Expected Immediate ToolResult"),
111        }
112    }
113}