ipfrs_cli/commands/
common.rs

1//! Common utilities shared across command modules
2//!
3//! This module provides validation helpers and utility functions
4//! used by multiple command handlers.
5
6use anyhow::Result;
7
8use crate::output::{self, format_bytes};
9
10// ============================================================================
11// Validation Helpers
12// ============================================================================
13
14/// Validate that a CID string has valid format
15#[allow(dead_code)]
16pub fn validate_cid_format(cid_str: &str) -> Result<()> {
17    if cid_str.is_empty() {
18        return Err(anyhow::anyhow!("CID cannot be empty"));
19    }
20
21    // Check common CID prefixes
22    if !cid_str.starts_with("Qm") && !cid_str.starts_with("bafy") && !cid_str.starts_with("bafk") {
23        output::warning(
24            "CID may have invalid format. Expected to start with 'Qm', 'bafy', or 'bafk'",
25        );
26    }
27
28    Ok(())
29}
30
31/// Check if a path is readable
32#[allow(dead_code)]
33pub fn validate_path_readable(path: &str) -> Result<()> {
34    let path_obj = std::path::Path::new(path);
35
36    if !path_obj.exists() {
37        return Err(anyhow::anyhow!(
38            "Path does not exist: {}\nPlease check the path and try again.",
39            path
40        ));
41    }
42
43    // Try to open the file to check read permissions
44    if path_obj.is_file() {
45        std::fs::File::open(path_obj).map_err(|e| {
46            anyhow::anyhow!(
47                "Cannot read file: {}\nError: {}\n\nCheck file permissions.",
48                path,
49                e
50            )
51        })?;
52    }
53
54    Ok(())
55}
56
57/// Warn about potentially large operations
58#[allow(dead_code)]
59pub fn check_file_size_warning(size: u64) {
60    const LARGE_FILE_THRESHOLD: u64 = 100 * 1024 * 1024; // 100 MB
61    const HUGE_FILE_THRESHOLD: u64 = 1024 * 1024 * 1024; // 1 GB
62
63    if size > HUGE_FILE_THRESHOLD {
64        output::warning(&format!(
65            "Very large file detected: {}. This operation may take significant time and memory.",
66            format_bytes(size)
67        ));
68    } else if size > LARGE_FILE_THRESHOLD {
69        output::warning(&format!(
70            "Large file detected: {}. This may take a while.",
71            format_bytes(size)
72        ));
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_validate_cid_format_empty() {
82        assert!(validate_cid_format("").is_err());
83    }
84
85    #[test]
86    fn test_validate_cid_format_valid_qm() {
87        assert!(validate_cid_format("QmXvBJfNuZ9A8Y3EqJkT").is_ok());
88    }
89
90    #[test]
91    fn test_validate_cid_format_valid_bafy() {
92        assert!(validate_cid_format("bafybeigdyrzt5sfp7udm7").is_ok());
93    }
94
95    #[test]
96    fn test_validate_path_readable_nonexistent() {
97        assert!(validate_path_readable("/nonexistent/path/to/file").is_err());
98    }
99}