1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
//! Holds Error type for the crate
use std::rc::Rc;
pub type Result<T> = std::result::Result<T, EdError>;
#[macro_use]
mod internal;
pub use internal::*;
// Define structs and traits for UI and IO errors
pub trait UIErrorTrait: std::error::Error + as_any::AsAny + 'static {}
#[derive(Clone, Debug)]
pub struct UIError {
pub inner: Rc<dyn UIErrorTrait>,
}
impl UIError {
/// Helper for downcasting into the internal error type
///
/// Due to how finnicky this is to get right, with coercing the Rc<T> into &T
/// before downcasting, I very much recommend using this helper.
pub fn downcast_ref<T: UIErrorTrait>(&self) -> Option<&T> {
use as_any::Downcast;
(&*self.inner).downcast_ref::<T>()
}
}
pub trait IOErrorTrait: std::error::Error + as_any::AsAny + 'static {}
#[derive(Clone, Debug)]
pub struct IOError {
pub inner: Rc<dyn IOErrorTrait>,
}
impl IOError {
/// Helper for downcasting into the internal error type
///
/// Due to how finnicky this is to get right, with coercing the Rc<T> into &T
/// before downcasting, I very much recommend using this helper.
pub fn downcast_ref<T: IOErrorTrait>(&self) -> Option<&T> {
use as_any::Downcast;
(&*self.inner).downcast_ref::<T>()
}
}
// Large trait implementations in their own files
mod display;
mod partialeq;
/// A basic enum Error implementation
///
/// Whilst it does implement `PartialEq`, partial is key in that. Since UI and
/// IO errors aren't sure to be comparable they are assumed to be equal, so
/// library users can easily identify UI resp. IO errors and downcast them for
/// the proper comparison for the abstracted type.
#[derive(Debug, Clone)]
pub enum EdError {
/// Internal error, usually from something OS related.
Internal(InternalError),
/// A holder for errors from the IO implementation.
IO(IOError),
/// A holder for errors from the UI implementation.
UI(UIError),
/// Execution recursed more times than [`Ed.recursion_limit`], indicating
/// infinite recursion.
///
/// Contains no details until someone writes stack unwinding for it.
InfiniteRecursion,
// Selection/index interpretation/validation errors
/// Given index exceeds size of buffer.
IndexTooBig{index: usize, buffer_len: usize},
/// Index 0 isn't a valid line.
Line0Invalid,
/// Selection empty or inverted.
/// Holds the interpreted bad selection.
SelectionEmpty((usize, usize)),
/// Given command doesn't allow any selection and one was given.
SelectionForbidden,
// Command+argument+flag interpretation errors
/// Unsaved changes when about to non-forcibly drop/delete buffer.
UnsavedChanges,
/// Selection and arguments were given that makes its command do nothing.
NoOp,
/// Given number of undo steps exceeds existing history.
UndoStepsInvalid{undo_steps: isize, undo_range: std::ops::Range<isize>},
/// Tried to set a shell escape as default file.
/// Holds given path string.
DefaultFileInvalid(String),
/// `k` or `K` command received an invalid character to tag with.
/// Holds given argument string.
TagInvalid(String),
/// Given tag found no match.
/// Holds the used tag.
TagNoMatch(char),
/// Any regex operation received an invalid regex or substitution.
RegexInvalid{regex: String, error: regex::Error},
/// Given regex found no match.
/// Holds the used regex.
RegexNoMatch(String),
/// Flags asked to print after the whole buffer was deleted.
PrintAfterWipe,
// Errors related to unset state variables
/// Tried to get default shell command, but it isn't yet set
DefaultFileUnset,
/// Tried to get default shell command, but it isn't yet set
DefaultShellCommandUnset,
/// Tried to get default `s` arguments, but it isn't yet set
DefaultSArgsUnset,
// Index parsing errors
/// Special index character found after start of index.
IndexSpecialAfterStart{prior_index: String, special_index: char},
/// Given index couldn't be parsed as a number.
/// Holds its text.
IndexNotInt(String),
/// Offset part of index couldn't be parsed as a number.
/// Holds its text.
OffsetNotInt(String),
/// Multiple indices with unclear relation (for example `'x2`)
IndicesUnrelated{prior_index: String, unrelated_index: String},
/// Unfinished index, a special index without its arguments.
/// Holds its text.
IndexUnfinished(String),
// Command and argument parsing errors
/// The given command doesn't exist.
/// Holds given command char.
CommandUndefined(char),
/// Argument list ended with `\`.
/// Holds whole argument list.
ArgumentListEscapedEnd(String),
/// Wrong number of argument.
ArgumentsWrongNr{expected: &'static str, received: usize},
/// `z` command received a non numeric number of lines to scroll.
/// Holds given argument.
ScrollNotInt(String),
/// `u` or `U` command couldn't interpret nr of steps to undo/redo as integer.
/// Holds given argument.
UndoStepsNotInt(String),
/// `J` command received a non numeric number of columns to reflow within.
/// Holds given argument.
ReflowNotInt{error: String, text: String},
/// The macro invoked wasn't found.
/// Holds given macro name.
MacroUndefined(String),
// Flag parsing errors
/// Same flag appears more than once.
/// Holds duplicated flag.
FlagDuplicate(char),
/// Unexpected flag was received.
/// Holds undefined flag.
FlagUndefined(char),
}
impl std::error::Error for EdError {}
impl From<UIError> for EdError {
fn from(e: UIError) -> Self {
Self::UI(e)
}
}
impl<E: UIErrorTrait> From<E> for UIError {
fn from(e: E) -> Self {
Self{ inner: Rc::new(e) }
}
}
// Causes conflicting trait bounds error for now. Instead use
// Into::<UIError>::into(error).into() to convert via UIError.
//impl<E: UIErrorTrait> From<E> for EdError {
// fn from(e: E) -> Self {
// Self::UI(e.into())
// }
//}
impl From<IOError> for EdError {
fn from(e: IOError) -> Self {
Self::IO(e)
}
}
impl<E: IOErrorTrait> From<E> for IOError {
fn from(e: E) -> Self {
Self{ inner: Rc::new(e) }
}
}
// Causes conflicting trait bounds error for now. Instead use
// Into::<IOError>::into(error).into() to convert via IOError.
//impl<E: IOErrorTrait> From<E> for EdError {
// fn from(e: E) -> Self {
// Self::IO(e.into())
// }
//}
impl From<InternalError> for EdError {
fn from(e: InternalError) -> Self {
Self::Internal(e)
}
}
impl EdError {
pub fn regex_error<S: Into<String>>(error: regex::Error, regex: S) -> Self {
Self::RegexInvalid{regex: regex.into(), error: error}
}
}