timsrust_centroid/error.rs
1/// Error type for the timsrust_centroid crate.
2///
3/// Wraps an error message string and implements `std::error::Error`.
4///
5/// # Example
6/// ```
7/// use timsrust_centroid::TimsError;
8/// let err = TimsError::new("Something went wrong");
9/// assert_eq!(format!("{}", err), "Something went wrong");
10/// ```
11#[derive(Debug, Clone, PartialEq, Eq, Default)]
12pub struct TimsError {
13 message: String,
14}
15
16impl TimsError {
17 /// Create a new `TimsError` with the given message.
18 ///
19 /// # Arguments
20 ///
21 /// * `message` - The error message to store.
22 ///
23 /// # Example
24 /// ```
25 /// use timsrust_centroid::TimsError;
26 /// let err = TimsError::new("error");
27 /// assert_eq!(format!("{}", err), "error");
28 /// ```
29 pub fn new(message: impl Into<String>) -> Self {
30 Self {
31 message: message.into(),
32 }
33 }
34}
35
36impl std::fmt::Display for TimsError {
37 /// Formats the error message for display.
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 write!(f, "{}", self.message)
40 }
41}
42
43impl std::error::Error for TimsError {}
44
45/// Result type for functions that can return a `TimsError`.
46///
47/// # Example
48/// ```
49/// use timsrust_centroid::{TimsResult, TimsError};
50/// fn foo() -> TimsResult<i32> {
51/// Err(TimsError::new("fail"))
52/// }
53/// assert!(foo().is_err());
54/// ```
55pub type TimsResult<T> = Result<T, TimsError>;