use crate::path::GenericPath; use crate::path::error::{FileNameError, DirectoryNameError}; use crate::terminal::error::TerminalError; type ParseError<'a> = lalrpop_util::ParseError, &'a str>; #[derive(Debug)] pub enum Error { IO(std::io::Error), Parse(String), FileName(FileNameError), DirectoryName(DirectoryNameError), PathListHasEmptyComponents(String), PathLexicallyDirectory(GenericPath), PathLexicallyRelative(GenericPath), PathLexicallyInvalid(GenericPath), PathEmpiricallyFile(GenericPath), Terminal(TerminalError), } impl std::error::Error for Error { } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Error::IO(e) => e.fmt(f), Error::Parse(e) => e.fmt(f), Error::FileName(e) => e.fmt(f), Error::DirectoryName(e) => e.fmt(f), Error::PathListHasEmptyComponents(path_list) => f.write_fmt(format_args!( "Path list has empty components: {}", path_list)), Error::PathLexicallyDirectory(path) => f.write_fmt(format_args!( "The path {} ends in a slash, but is supposed to refer to a file, \ not a directory.", path)), Error::PathLexicallyRelative(path) => f.write_fmt(format_args!( "The path {} is relative, not absolute.", path)), Error::PathLexicallyInvalid(path) => f.write_fmt(format_args!( "This isn't a valid path. {}", path)), Error::PathEmpiricallyFile(path) => f.write_fmt(format_args!( "There's a file at {}, not a directory.", path)), Error::Terminal(e) => e.fmt(f), } } } impl From<()> for Error { fn from(_: ()) -> Error { unreachable!() } } impl From for Error { fn from(e: std::io::Error) -> Error { Error::IO(e) } } impl From> for Error { fn from(e: ParseError<'_>) -> Error { Error::Parse(format!("{}", e)) } } impl From for Error { fn from(e: FileNameError) -> Error { Error::FileName(e) } } impl From for Error { fn from(e: DirectoryNameError) -> Error { Error::DirectoryName(e) } } impl From for Error { fn from(e: TerminalError) -> Error { Error::Terminal(e) } }