Skip to main content

tftio_cli_common/
types.rs

1//! Shared types for Workhelix CLI tools.
2
3/// Repository information for CLI tools.
4///
5/// This structure holds basic repository metadata for identification purposes.
6#[derive(Debug, Clone)]
7pub struct RepoInfo {
8    /// Repository owner (e.g., "workhelix")
9    pub owner: &'static str,
10    /// Repository name (e.g., "prompter")
11    pub name: &'static str,
12}
13
14impl RepoInfo {
15    /// Create a new `RepoInfo` instance.
16    #[must_use]
17    pub const fn new(owner: &'static str, name: &'static str) -> Self {
18        Self { owner, name }
19    }
20}
21
22/// Health check result for doctor command.
23#[derive(Debug, Clone)]
24pub struct DoctorCheck {
25    /// Name of the check
26    pub name: String,
27    /// Whether the check passed
28    pub passed: bool,
29    /// Optional message
30    pub message: Option<String>,
31}
32
33impl DoctorCheck {
34    /// Create a new passing check.
35    #[must_use]
36    pub fn pass(name: impl Into<String>) -> Self {
37        Self {
38            name: name.into(),
39            passed: true,
40            message: None,
41        }
42    }
43
44    /// Create a new failing check with a message.
45    #[must_use]
46    pub fn fail(name: impl Into<String>, message: impl Into<String>) -> Self {
47        Self {
48            name: name.into(),
49            passed: false,
50            message: Some(message.into()),
51        }
52    }
53
54    /// Create a file existence check.
55    ///
56    /// # Errors
57    /// Returns a failing check if the file doesn't exist.
58    pub fn file_exists(path: impl AsRef<std::path::Path>) -> Self {
59        let path_ref = path.as_ref();
60        if path_ref.exists() && path_ref.is_file() {
61            Self::pass(format!("File exists: {}", path_ref.display()))
62        } else {
63            Self::fail(
64                format!("File check: {}", path_ref.display()),
65                format!("File not found: {}", path_ref.display()),
66            )
67        }
68    }
69
70    /// Create a directory existence check.
71    ///
72    /// # Errors
73    /// Returns a failing check if the directory doesn't exist.
74    pub fn dir_exists(path: impl AsRef<std::path::Path>) -> Self {
75        let path_ref = path.as_ref();
76        if path_ref.exists() && path_ref.is_dir() {
77            Self::pass(format!("Directory exists: {}", path_ref.display()))
78        } else {
79            Self::fail(
80                format!("Directory check: {}", path_ref.display()),
81                format!("Directory not found: {}", path_ref.display()),
82            )
83        }
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn test_repo_info_creation() {
93        let repo = RepoInfo::new("workhelix", "prompter");
94        assert_eq!(repo.owner, "workhelix");
95        assert_eq!(repo.name, "prompter");
96    }
97
98    #[test]
99    fn test_doctor_check_pass() {
100        let check = DoctorCheck::pass("test check");
101        assert!(check.passed);
102        assert_eq!(check.name, "test check");
103        assert!(check.message.is_none());
104    }
105
106    #[test]
107    fn test_doctor_check_fail() {
108        let check = DoctorCheck::fail("test check", "error message");
109        assert!(!check.passed);
110        assert_eq!(check.name, "test check");
111        assert_eq!(check.message, Some("error message".to_string()));
112    }
113}