How to create custom error types

Create custom error types in Rust by defining an enum and implementing the Display and Error traits.

Define a custom error type using an enum and implement the std::error::Error trait to provide context and formatting.

use std::fmt;

#[derive(Debug)]
enum AppError {
    FileNotFound(String),
    InvalidFormat(String),
}

impl fmt::Display for AppError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            AppError::FileNotFound(path) => write!(f, "File not found: {}", path),
            AppError::InvalidFormat(msg) => write!(f, "Invalid format: {}", msg),
        }
    }
}

impl std::error::Error for AppError {}