Skip to main content

ssg/cmd/
error.rs

1// Copyright © 2023 - 2026 Static Site Generator (SSG). All rights reserved.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! CLI error types and the type-safe `LanguageCode` wrapper.
5
6use serde::{Deserialize, Serialize};
7
8/// Type-safe representation of a language code.
9///
10/// # Examples
11/// ```
12/// use ssg::cmd::LanguageCode;
13/// assert!(LanguageCode::new("en-GB").is_ok());
14/// assert!(LanguageCode::new("invalid").is_err());
15/// ```
16#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
17pub struct LanguageCode(String);
18
19impl LanguageCode {
20    /// Creates a new `LanguageCode` instance from a string.
21    pub fn new(code: &str) -> Result<Self, CliError> {
22        if code.len() != 5 || code.chars().nth(2) != Some('-') {
23            return Err(CliError::ValidationError(
24                "Invalid language code format".into(),
25            ));
26        }
27
28        let (lang, region) = code.split_at(2);
29        let region = &region[1..]; // Skip hyphen
30
31        if !lang.chars().all(|c| c.is_ascii_lowercase()) {
32            return Err(CliError::ValidationError(
33                "Language code must be lowercase".into(),
34            ));
35        }
36
37        if !region.chars().all(|c| c.is_ascii_uppercase()) {
38            return Err(CliError::ValidationError(
39                "Region code must be uppercase".into(),
40            ));
41        }
42
43        Ok(Self(code.to_string()))
44    }
45}
46
47impl std::fmt::Display for LanguageCode {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        write!(f, "{}", self.0)
50    }
51}
52
53/// Possible errors that can occur during CLI operations.
54#[derive(Debug)]
55#[non_exhaustive]
56pub enum CliError {
57    /// Error indicating an invalid path with additional details.
58    InvalidPath {
59        /// Field name where the path is used.
60        field: String,
61        /// Additional details about the invalid path.
62        details: String,
63    },
64
65    /// Error indicating a missing required argument.
66    MissingArgument(String),
67
68    /// Error indicating an invalid URL.
69    InvalidUrl(String),
70
71    /// Error indicating an I/O error.
72    IoError(std::io::Error),
73
74    /// Error indicating a TOML parsing error.
75    TomlError(toml::de::Error),
76
77    /// Error indicating a validation error.
78    ValidationError(String),
79}
80
81impl std::fmt::Display for CliError {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        match self {
84            Self::InvalidPath { field, details } => {
85                write!(f, "Invalid path '{field}': {details}")
86            }
87            Self::MissingArgument(arg) => {
88                write!(f, "Required argument missing: {arg}")
89            }
90            Self::InvalidUrl(url) => write!(f, "Invalid URL: {url}"),
91            Self::IoError(e) => write!(f, "IO error: {e}"),
92            Self::TomlError(e) => write!(f, "TOML parsing error: {e}"),
93            Self::ValidationError(msg) => {
94                write!(f, "Validation error: {msg}")
95            }
96        }
97    }
98}
99
100impl std::error::Error for CliError {
101    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
102        match self {
103            Self::IoError(e) => Some(e),
104            Self::TomlError(e) => Some(e),
105            _ => None,
106        }
107    }
108}
109
110impl From<std::io::Error> for CliError {
111    fn from(e: std::io::Error) -> Self {
112        Self::IoError(e)
113    }
114}
115
116impl From<toml::de::Error> for CliError {
117    fn from(e: toml::de::Error) -> Self {
118        Self::TomlError(e)
119    }
120}
121
122#[cfg(test)]
123#[allow(clippy::unwrap_used, clippy::expect_used)]
124mod tests {
125    use super::*;
126
127    #[test]
128    fn test_language_code() {
129        assert!(LanguageCode::new("en-GB").is_ok());
130        assert!(LanguageCode::new("en-gb").is_err());
131        assert!(LanguageCode::new("EN-GB").is_err());
132        assert!(LanguageCode::new("e-GB").is_err());
133    }
134
135    #[test]
136    fn test_language_code_display() {
137        let code = LanguageCode::new("en-GB").unwrap();
138        assert_eq!(code.to_string(), "en-GB");
139    }
140
141    #[test]
142    fn test_language_code_edge_cases() {
143        assert!(LanguageCode::new("enGB").is_err());
144        assert!(LanguageCode::new("e-G").is_err());
145        assert!(LanguageCode::new("").is_err());
146    }
147
148    // -----------------------------------------------------------------
149    // CliError Display impl -- each variant
150    // -----------------------------------------------------------------
151
152    #[test]
153    fn cli_error_display_invalid_path() {
154        let err = CliError::InvalidPath {
155            field: "content_dir".into(),
156            details: "contains backslashes".into(),
157        };
158        let msg = format!("{err}");
159        assert!(msg.contains("content_dir"));
160        assert!(msg.contains("contains backslashes"));
161    }
162
163    #[test]
164    fn cli_error_display_missing_argument() {
165        let err = CliError::MissingArgument("site_name".into());
166        let msg = format!("{err}");
167        assert!(msg.contains("site_name"));
168        assert!(msg.contains("missing"));
169    }
170
171    #[test]
172    fn cli_error_display_invalid_url() {
173        let err = CliError::InvalidUrl("bad://url".into());
174        let msg = format!("{err}");
175        assert!(msg.contains("bad://url"));
176    }
177
178    #[test]
179    fn cli_error_display_io_error() {
180        let io_err =
181            std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
182        let err = CliError::IoError(io_err);
183        let msg = format!("{err}");
184        assert!(msg.contains("file not found"));
185    }
186
187    #[test]
188    fn cli_error_display_toml_error() {
189        let toml_err: toml::de::Error =
190            toml::from_str::<crate::cmd::SsgConfig>("invalid {{{").unwrap_err();
191        let err = CliError::TomlError(toml_err);
192        let msg = format!("{err}");
193        assert!(msg.contains("TOML"));
194    }
195
196    #[test]
197    fn cli_error_display_validation_error() {
198        let err = CliError::ValidationError("name too long".into());
199        let msg = format!("{err}");
200        assert!(msg.contains("name too long"));
201        assert!(msg.contains("Validation"));
202    }
203}