memscope_rs/export/binary/
error.rs1use std::fmt;
4
5#[derive(Debug)]
7pub enum BinaryExportError {
8 Io(std::io::Error),
10
11 InvalidFormat,
13
14 UnsupportedVersion(u32),
16
17 CorruptedData(String),
19
20 InvalidMagic {
22 expected: String,
24 actual: String,
26 },
27
28 SerializationError(String),
30
31 CompressionError(String),
33}
34
35impl fmt::Display for BinaryExportError {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 match self {
38 BinaryExportError::Io(err) => write!(f, "File I/O error: {err}"),
39 BinaryExportError::InvalidFormat => write!(f, "Invalid file format"),
40 BinaryExportError::UnsupportedVersion(version) => {
41 write!(f, "Unsupported version: {version}")
42 }
43 BinaryExportError::CorruptedData(reason) => {
44 write!(f, "Data corruption: {reason}")
45 }
46 BinaryExportError::InvalidMagic { expected, actual } => {
47 write!(
48 f,
49 "Invalid magic bytes: expected '{expected}', got '{actual}'",
50 )
51 }
52 BinaryExportError::SerializationError(msg) => {
53 write!(f, "Serialization error: {msg}")
54 }
55 BinaryExportError::CompressionError(msg) => {
56 write!(f, "Compression error: {msg}")
57 }
58 }
59 }
60}
61
62impl std::error::Error for BinaryExportError {
63 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
64 match self {
65 BinaryExportError::Io(err) => Some(err),
66 _ => None,
67 }
68 }
69}
70
71impl From<std::io::Error> for BinaryExportError {
72 fn from(err: std::io::Error) -> Self {
73 BinaryExportError::Io(err)
74 }
75}
76
77impl From<BinaryExportError> for crate::core::types::TrackingError {
79 fn from(err: BinaryExportError) -> Self {
80 crate::core::types::TrackingError::ExportError(err.to_string())
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 #[test]
89 fn test_error_display() {
90 let err = BinaryExportError::InvalidFormat;
91 assert_eq!(err.to_string(), "Invalid file format");
92
93 let err = BinaryExportError::UnsupportedVersion(2);
94 assert_eq!(err.to_string(), "Unsupported version: 2");
95 }
96
97 #[test]
98 fn test_io_error_conversion() {
99 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
100 let binary_err = BinaryExportError::from(io_err);
101
102 match binary_err {
103 BinaryExportError::Io(_) => (),
104 _ => panic!("Expected Io error"),
105 }
106 }
107}