60 lines
1.3 KiB
Rust
60 lines
1.3 KiB
Rust
//! # Error
|
|
//!
|
|
//! This module contains the definition of the MachineError struct,
|
|
//! for error management in the Machine module.
|
|
//!
|
|
//! Basic usage:
|
|
//!
|
|
//! ```
|
|
//! fn example(x: bool) -> Result<(), MachineError> {
|
|
//! match x {
|
|
//! true => Ok(()),
|
|
//! _ => Err(MachineError::new("Machine failed because of ..."));
|
|
//! }
|
|
//! }
|
|
//! ```
|
|
|
|
use std::fmt;
|
|
|
|
/// Machine Error
|
|
/// This error serves as a specific exception handler for the Machine struct
|
|
#[derive(Debug, Clone)]
|
|
pub struct MachineError {
|
|
/// The error message
|
|
message: String
|
|
}
|
|
|
|
pub enum MachineOk {
|
|
Ok,
|
|
Shutdown
|
|
}
|
|
|
|
/// This impl allows this MachineError to be formatted into an empty format.
|
|
///
|
|
/// ```
|
|
/// // Result of printing a MachineError
|
|
/// let m = MachineError::new("Lorem Ipsum");
|
|
/// println!("Example: {}", m);
|
|
/// ```
|
|
///
|
|
/// Console output:Error}
|
|
/// ```
|
|
/// example Lorem Ipsum
|
|
/// ```
|
|
impl fmt::Display for MachineError {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "Machine error: {}", &self.message)
|
|
}
|
|
}
|
|
|
|
impl From<&str> for MachineError {
|
|
fn from(value: &str) -> Self {
|
|
MachineError { message: value.to_string() }
|
|
}
|
|
}
|
|
|
|
impl From<String> for MachineError {
|
|
fn from(value: String) -> Self {
|
|
MachineError { message: value }
|
|
}
|
|
} |