use core::fmt;
#[derive(Debug, Clone)]
pub struct XdrError {
pub code: i32,
pub pos: usize,
pub message: String,
}
impl fmt::Display for XdrError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} at position {}", self.message, self.pos)
}
}
impl std::error::Error for XdrError {}
pub mod codes {
pub const UNEXPECTED_EOF: i32 = -100;
pub const INVALID_BOOL: i32 = -101;
pub const INVALID_OPTIONAL: i32 = -102;
pub const INVALID_VARIANT: i32 = -103;
pub const INVALID_UTF8: i32 = -104;
pub const UNSUPPORTED_TYPE: i32 = -105;
pub const ALIGNMENT_ERROR: i32 = -106;
}
impl XdrError {
pub fn from_code(code: i32, pos: usize) -> Self {
let message = match code {
codes::UNEXPECTED_EOF => "unexpected end of input".to_string(),
codes::INVALID_BOOL => "invalid boolean value (must be 0 or 1)".to_string(),
codes::INVALID_OPTIONAL => "invalid optional discriminant (must be 0 or 1)".to_string(),
codes::INVALID_VARIANT => "invalid enum discriminant".to_string(),
codes::INVALID_UTF8 => "invalid UTF-8 in string".to_string(),
codes::UNSUPPORTED_TYPE => "unsupported type for XDR".to_string(),
codes::ALIGNMENT_ERROR => "position not aligned to 4 bytes".to_string(),
_ => format!("unknown error code {}", code),
};
Self { code, pos, message }
}
pub fn new(code: i32, pos: usize, message: impl Into<String>) -> Self {
Self {
code,
pos,
message: message.into(),
}
}
}
#[derive(Debug)]
pub struct XdrSerializeError {
pub message: String,
}
impl fmt::Display for XdrSerializeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for XdrSerializeError {}
impl XdrSerializeError {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}