Skip to main content

twrite_core/
error.rs

1use std::ops::Range;
2use thiserror::Error;
3
4/// Error type for editor buffer validation, range operations, and file I/O.
5#[derive(Debug, Error)]
6pub enum EditorError {
7    /// The specified byte offset is out of document bounds.
8    #[error("byte offset {offset} is out of bounds (document length: {len})")]
9    OutOfBounds {
10        /// The requested byte offset.
11        offset: usize,
12        /// The total length of the document in bytes.
13        len: usize,
14    },
15
16    /// The specified row index is out of document line bounds.
17    #[error("line index {row} is out of bounds (total lines: {total_lines})")]
18    InvalidRow {
19        /// The requested row index.
20        row: usize,
21        /// The total number of lines in the document.
22        total_lines: usize,
23    },
24
25    /// The range is invalid because start offset exceeds end offset or document bounds.
26    #[error("invalid byte range: {range:?} (document length: {len})")]
27    InvalidRange {
28        /// The requested byte range.
29        range: Range<usize>,
30        /// The total length of the document in bytes.
31        len: usize,
32    },
33
34    /// The byte offset does not land on a valid UTF-8 character boundary.
35    #[error("byte offset {offset} is not a valid UTF-8 character boundary")]
36    InvalidCharBoundary {
37        /// The invalid byte offset.
38        offset: usize,
39    },
40
41    /// A search was attempted with an empty pattern.
42    #[error("search pattern is empty")]
43    EmptySearchPattern,
44
45    /// A regex search pattern failed to compile.
46    #[error("invalid regex {pattern:?}: {message}")]
47    InvalidRegex {
48        /// The offending regex source.
49        pattern: String,
50        /// The underlying regex engine error message.
51        message: String,
52    },
53
54    /// An I/O error occurred during file reading or writing.
55    #[error("I/O error: {0}")]
56    Io(#[from] std::io::Error),
57}
58
59/// Specialized Result type for editor operations.
60pub type Result<T> = std::result::Result<T, EditorError>;