reovim_driver_codec/error.rs
1//! Codec error types.
2
3use std::fmt;
4
5/// Errors that can occur during codec operations.
6///
7/// Codec operations can fail for various reasons: invalid byte sequences
8/// during decoding, unmappable characters during encoding, or general
9/// I/O issues.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum CodecError {
12 /// Invalid byte sequence at the given offset.
13 ///
14 /// Returned when the decoder encounters bytes that don't form a valid
15 /// character in the expected encoding.
16 InvalidSequence {
17 /// Byte offset where the invalid sequence starts.
18 offset: usize,
19 /// Human-readable description of the error.
20 detail: String,
21 },
22
23 /// Character cannot be represented in the target encoding.
24 ///
25 /// Returned when the encoder encounters a Unicode character that has
26 /// no mapping in the target encoding (e.g., a CJK character in Latin-1).
27 UnmappableCharacter {
28 /// Byte offset in the source string.
29 offset: usize,
30 /// Human-readable description of the error.
31 detail: String,
32 },
33
34 /// General codec error.
35 Other(String),
36}
37
38impl fmt::Display for CodecError {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 match self {
41 Self::InvalidSequence { offset, detail } => {
42 write!(f, "invalid byte sequence at offset {offset}: {detail}")
43 }
44 Self::UnmappableCharacter { offset, detail } => {
45 write!(f, "unmappable character at offset {offset}: {detail}")
46 }
47 Self::Other(msg) => write!(f, "{msg}"),
48 }
49 }
50}
51
52impl std::error::Error for CodecError {}
53
54#[cfg(test)]
55#[path = "error_tests.rs"]
56mod tests;