// Created on savesnippets.com ยท https://savesnippets.com/8Nehuo2rtQIQrW // Cargo.toml: thiserror = "1" use thiserror::Error; use std::io; #[derive(Error, Debug)] pub enum AppError { #[error("config file not found: {0}")] ConfigMissing(String), #[error("I/O error: {0}")] Io(#[from] io::Error), // auto-convert from std::io::Error #[error("invalid value for {field}: {value}")] Invalid { field: String, value: String }, } fn load(path: &str) -> Result { std::fs::read_to_string(path)? // io::Error auto-converts to AppError .lines().next() .map(|s| s.to_string()) .ok_or_else(|| AppError::ConfigMissing(path.to_string())) } fn main() { match load("config.toml") { Ok(line) => println!("first line: {line}"), Err(e) => eprintln!("error: {e}"), } }