56 lines
1.2 KiB
Rust
56 lines
1.2 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::{self, Error};
|
|
|
|
/// 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
|
|
}
|
|
|
|
impl MachineError {
|
|
|
|
/// MachineError constructor
|
|
pub fn new(message: &str) -> MachineError {
|
|
MachineError {
|
|
message: message.to_string()
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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 }
|
|
}
|
|
} |