prism3_core/lang/argument/
error.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # Argument Validation Errors
10//!
11//! Defines error types related to argument validation.
12//!
13//! # Author
14//!
15//! Haixing Hu
16
17use std::fmt;
18
19/// Argument validation error
20///
21/// Returned when an argument does not satisfy validation conditions.
22///
23/// # Features
24///
25/// - Contains detailed error messages
26/// - Implements standard error traits
27/// - Supports conversion from strings
28///
29/// # Examples
30///
31/// ```rust,ignore
32/// use prism3_core::lang::argument::ArgumentError;
33///
34/// let error = ArgumentError::new("Invalid argument");
35/// assert_eq!(error.message(), "Invalid argument");
36/// ```
37///
38/// # Author
39///
40/// Haixing Hu
41///
42#[derive(Debug, Clone, PartialEq, Eq)]
43pub struct ArgumentError {
44    message: String,
45}
46
47impl ArgumentError {
48    /// Create a new argument error
49    ///
50    /// # Parameters
51    ///
52    /// * `message` - Error message
53    ///
54    /// # Examples
55    ///
56    /// ```rust,ignore
57    /// use prism3_core::lang::argument::ArgumentError;
58    ///
59    /// let error = ArgumentError::new("Value cannot be negative");
60    /// ```
61    pub fn new(message: impl Into<String>) -> Self {
62        Self {
63            message: message.into(),
64        }
65    }
66
67    /// Get the error message
68    ///
69    /// # Returns
70    ///
71    /// Returns a reference to the error message
72    pub fn message(&self) -> &str {
73        &self.message
74    }
75}
76
77impl fmt::Display for ArgumentError {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        write!(f, "{}", self.message)
80    }
81}
82
83impl std::error::Error for ArgumentError {}
84
85impl From<String> for ArgumentError {
86    fn from(message: String) -> Self {
87        Self::new(message)
88    }
89}
90
91impl From<&str> for ArgumentError {
92    fn from(message: &str) -> Self {
93        Self::new(message)
94    }
95}
96
97/// Argument validation result type
98///
99/// Unified result type for all argument validation operations.
100///
101/// # Examples
102///
103/// ```rust,ignore
104/// use prism3_core::lang::argument::{ArgumentResult, ArgumentError};
105///
106/// fn validate_positive(value: i32) -> ArgumentResult<i32> {
107///     if value > 0 {
108///         Ok(value)
109///     } else {
110///         Err(ArgumentError::new("Value must be positive"))
111///     }
112/// }
113/// ```
114///
115/// # Author
116///
117/// Haixing Hu
118///
119pub type ArgumentResult<T> = Result<T, ArgumentError>;