yarrow_rs/systemtrayerror.rs
1// ref https://github.com/Cameleon00722/horizon/blob/ebbe48975c62ee5886698ee550d337578f5a7cb7/src/systemtrayerror.rs
2
3use std::{
4 error::Error,
5 fmt::{self, Display},
6};
7/// Represents custom error types for the SystemTray application.
8#[derive(Debug)]
9pub struct SystemTrayError {
10 /// A human-readable error message describing the nature of the error.
11 pub message: String,
12 /// An error code indicating the specific type of error.
13 pub code: i32,
14}
15
16impl SystemTrayError {
17 /// Creates a new instance of `SystemTrayError` with the specified error code.
18 ///
19 /// # Parameters
20 ///
21 /// - `code`: An integer representing the error code.
22 ///
23 /// # Returns
24 ///
25 /// Returns a `SystemTrayError` instance with the given error code.
26 ///
27 /// # Examples
28 ///
29 /// ```rust
30 /// let error = yarrow_rs::SystemTrayError::new(1);
31 /// println!("{:?}", error);
32 /// ```
33 pub fn new(code: i32) -> SystemTrayError {
34 let message = match code {
35 1 => "Index out of bounds exception".to_string(),
36 2 => "Can't open file".to_string(),
37 3 => "No mac address found".to_string(),
38 4 => "Seed too short (10 char)".to_string(),
39 5 => "Key is too short".to_string(),
40 6 => "Character not found in character set".to_string(),
41 7 => "Error when dividing by 8".to_string(),
42 8 => "Error no processus found".to_string(),
43 _ => format!("Unknown error with code {}", code),
44 };
45
46 SystemTrayError { message, code }
47 }
48}
49/// Implements the `Error` trait for the custom error type `SystemTrayError`.
50impl Error for SystemTrayError {}
51/// Implements the `Display` trait for the custom error type `SystemTrayError`.
52impl Display for SystemTrayError {
53 /// Formats the error message for display.
54 ///
55 /// # Parameters
56 ///
57 /// - `f`: A mutable reference to a `fmt::Formatter` used for formatting.
58 ///
59 /// # Returns
60 ///
61 /// Returns a `fmt::Result` indicating the success or failure of the formatting.
62 ///
63 /// # Examples
64 ///
65 /// ```rust
66 /// let error = yarrow_rs::SystemTrayError::new(1);
67 /// let formatted_message = format!("{}", error);
68 /// println!("{}", formatted_message);
69 /// ```
70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 write!(f, "{}", self.message)
72 }
73}