we (web engine): Experimental web browser project to understand the limits of Claude
at builtins-fix 70 lines 2.0 kB view raw
1//! Encoding error types. 2 3use std::fmt; 4 5/// Errors that can occur during encoding/decoding operations. 6#[derive(Debug, Clone, PartialEq, Eq)] 7pub enum EncodingError { 8 /// An invalid byte sequence was encountered in fatal (strict) mode. 9 InvalidSequence { 10 encoding: &'static str, 11 position: usize, 12 }, 13 /// The requested encoding label is not recognized. 14 UnknownLabel(String), 15 /// The encoding does not support the encode operation. 16 /// Per WHATWG spec, UTF-16BE/LE are decode-only. 17 EncodeNotSupported { encoding: &'static str }, 18} 19 20impl fmt::Display for EncodingError { 21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 22 match self { 23 Self::InvalidSequence { encoding, position } => { 24 write!( 25 f, 26 "invalid byte sequence in {encoding} at position {position}" 27 ) 28 } 29 Self::UnknownLabel(label) => { 30 write!(f, "unknown encoding label: {label}") 31 } 32 Self::EncodeNotSupported { encoding } => { 33 write!(f, "encode not supported for {encoding}") 34 } 35 } 36 } 37} 38 39pub type Result<T> = std::result::Result<T, EncodingError>; 40 41#[cfg(test)] 42mod tests { 43 use super::*; 44 45 #[test] 46 fn display_invalid_sequence() { 47 let err = EncodingError::InvalidSequence { 48 encoding: "UTF-8", 49 position: 5, 50 }; 51 assert_eq!( 52 err.to_string(), 53 "invalid byte sequence in UTF-8 at position 5" 54 ); 55 } 56 57 #[test] 58 fn display_unknown_label() { 59 let err = EncodingError::UnknownLabel("bogus".to_string()); 60 assert_eq!(err.to_string(), "unknown encoding label: bogus"); 61 } 62 63 #[test] 64 fn display_encode_not_supported() { 65 let err = EncodingError::EncodeNotSupported { 66 encoding: "UTF-16LE", 67 }; 68 assert_eq!(err.to_string(), "encode not supported for UTF-16LE"); 69 } 70}