use std::io;
use masterror::AppError;
#[derive(Debug)]
pub struct IoError {
source: io::Error
}
impl From<IoError> for AppError {
fn from(err: IoError) -> Self {
AppError::internal(format!("IO error: {}", err.source))
}
}
#[derive(Debug)]
pub struct ParseError {
source: syn::Error
}
impl From<ParseError> for AppError {
fn from(err: ParseError) -> Self {
AppError::bad_request(format!("Parse error: {}", err.source))
}
}
#[derive(Debug)]
pub struct InvalidConfigError {
message: String
}
impl From<InvalidConfigError> for AppError {
fn from(err: InvalidConfigError) -> Self {
AppError::bad_request(format!("Invalid configuration: {}", err.message))
}
}
#[derive(Debug)]
pub struct FileNotFoundError {
path: String
}
impl From<FileNotFoundError> for AppError {
fn from(err: FileNotFoundError) -> Self {
AppError::not_found(format!("File not found: {}", err.path))
}
}
impl From<io::Error> for IoError {
fn from(source: io::Error) -> Self {
Self {
source
}
}
}
impl From<syn::Error> for ParseError {
fn from(source: syn::Error) -> Self {
Self {
source
}
}
}
impl InvalidConfigError {
pub fn new(message: String) -> Self {
Self {
message
}
}
}
impl FileNotFoundError {
pub fn new(path: String) -> Self {
Self {
path
}
}
}
#[cfg(test)]
mod tests {
use std::io;
use super::*;
#[test]
fn test_io_error_from_std_io() {
let std_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let io_error = IoError::from(std_err);
let _app_error: AppError = io_error.into();
}
#[test]
fn test_parse_error_from_syn() {
let syn_err = syn::Error::new(proc_macro2::Span::call_site(), "parse failed");
let parse_error = ParseError::from(syn_err);
let _app_error: AppError = parse_error.into();
}
#[test]
fn test_invalid_config_error_new() {
let config_err = InvalidConfigError::new("invalid setting".to_string());
let _app_error: AppError = config_err.into();
}
#[test]
fn test_file_not_found_error_new() {
let not_found_err = FileNotFoundError::new("/path/to/file.rs".to_string());
let _app_error: AppError = not_found_err.into();
}
}