use core::fmt;
#[derive(Debug, Clone)]
pub struct MsgPackError {
pub code: i32,
pub pos: usize,
pub message: String,
}
impl fmt::Display for MsgPackError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} at position {}", self.message, self.pos)
}
}
impl std::error::Error for MsgPackError {}
pub mod codes {
pub const UNEXPECTED_EOF: i32 = -100;
pub const EXPECTED_BOOL: i32 = -101;
pub const EXPECTED_ARRAY: i32 = -102;
pub const EXPECTED_BIN: i32 = -103;
pub const EXPECTED_INT: i32 = -104;
pub const INT_OVERFLOW: i32 = -105;
pub const COUNT_OVERFLOW: i32 = -106;
pub const SEQ_UNDERFLOW: i32 = -107;
pub const UNSUPPORTED: i32 = -1;
}
impl MsgPackError {
pub fn from_code(code: i32, pos: usize) -> Self {
let message = match code {
codes::UNEXPECTED_EOF => "unexpected end of input".to_string(),
codes::EXPECTED_BOOL => "expected bool (0xC2 or 0xC3)".to_string(),
codes::EXPECTED_ARRAY => "expected array tag (fixarray/array16/array32)".to_string(),
codes::EXPECTED_BIN => "expected bin tag (bin8/bin16/bin32)".to_string(),
codes::EXPECTED_INT => "expected integer tag".to_string(),
codes::INT_OVERFLOW => "integer value overflows target type".to_string(),
codes::COUNT_OVERFLOW => "count too large for platform".to_string(),
codes::SEQ_UNDERFLOW => "sequence underflow (internal error)".to_string(),
codes::UNSUPPORTED => "unsupported operation".to_string(),
_ => format!("unknown error code {}", code),
};
Self { code, pos, message }
}
}