pub enum SerializationError {
Io(Error),
Json(JsonError),
Binary(BinaryError),
ValidationFailed {
field: String,
message: String,
},
ShapeMismatch {
expected_dims: Vec<usize>,
found_dims: Vec<usize>,
},
AllocationFailed {
requested_size: usize,
available_memory: Option<usize>,
},
Custom(String),
JsonFormat {
message: String,
line: Option<usize>,
column: Option<usize>,
},
BinaryFormat {
message: String,
position: Option<usize>,
},
VersionMismatch {
expected: u32,
found: u32,
},
InvalidMagic {
expected: u32,
found: u32,
},
}Expand description
Main error type for serialization operations
This enum covers all possible failure modes during serialization and deserialization, providing detailed context for debugging and error recovery. Format-specific errors are delegated to their respective modules while maintaining a unified error interface.
§Error Variants
The enum provides comprehensive coverage of serialization failure modes:
- I/O errors: File operations, network issues, stream handling
- Format errors: JSON and binary format-specific parsing errors
- Validation errors: Data validation failures during deserialization
- Shape errors: Tensor dimension mismatches
- Memory errors: Allocation failures and resource constraints
- Custom errors: Generic errors with custom messages
§Error Context
Each error variant provides detailed context to aid in debugging:
- Field names and validation messages for validation errors
- Expected vs actual tensor dimensions for shape errors
- Memory allocation details for allocation failures
- Format-specific error information for parsing errors
§Thread Safety
This type is thread-safe and can be shared across threads without additional synchronization.
Variants§
Io(Error)
I/O errors during file operations, network issues, or stream handling
This variant wraps standard I/O errors that occur during serialization operations, such as file reading/writing, network communication, or stream processing. The underlying I/O error is preserved for detailed error analysis.
§Common Causes
- File not found or permission denied
- Disk space exhaustion
- Network connectivity issues
- Stream corruption or interruption
- Device I/O failures
Json(JsonError)
JSON-specific format and parsing errors
This variant delegates to the JSON module’s error type for format-specific JSON parsing and validation errors. It provides detailed information about JSON syntax errors, structural issues, and parsing failures.
§Common Causes
- Invalid JSON syntax
- Malformed JSON structure
- Encoding issues
- Unexpected token types
- JSON depth limits exceeded
Binary(BinaryError)
Binary-specific format and parsing errors
This variant delegates to the binary module’s error type for format-specific binary parsing and validation errors. It provides detailed information about binary format issues, corruption, and parsing failures.
§Common Causes
- Invalid binary format
- Corrupted binary data
- Version mismatches
- Magic number validation failures
- Truncated binary streams
ValidationFailed
Data validation failed during deserialization
This variant indicates that data validation failed during the deserialization process. It provides the specific field name and a human-readable message explaining why the validation failed.
§Fields
field- Name of the field that failed validationmessage- Human-readable message explaining why validation failed
§Common Causes
- Invalid field values
- Missing required fields
- Type conversion failures
- Constraint violations
- Business logic validation failures
Fields
ShapeMismatch
Tensor shape or size mismatch
This variant indicates that a tensor’s dimensions don’t match the expected shape during deserialization or validation. It provides both the expected and actual dimensions for debugging.
§Fields
expected_dims- Expected tensor dimensionsfound_dims- Actual tensor dimensions found
§Common Causes
- Incorrect tensor serialization
- Version incompatibilities
- Manual data corruption
- Serialization format changes
- Dimension calculation errors
Fields
AllocationFailed
Memory allocation failed
This variant indicates that a memory allocation request failed during serialization or deserialization. It provides details about the requested size and available memory (if known).
§Fields
requested_size- Number of bytes that were requestedavailable_memory- Number of bytes available (if known)
§Common Causes
- Insufficient system memory
- Memory fragmentation
- Resource limits exceeded
- Large tensor allocations
- Memory pool exhaustion
Fields
Custom(String)
Generic error with custom message
This variant provides a catch-all for generic serialization errors that don’t fit into the other specific categories. It allows for custom error messages while maintaining the unified error interface.
§Common Uses
- Custom validation logic
- Business rule violations
- Unsupported operations
- Configuration errors
- Third-party integration issues
JsonFormat
@deprecated Use Json(JsonError::Format) instead
This variant is deprecated and will be removed in a future version.
Use Json(JsonError::Format) instead for JSON format errors.
§Migration
Replace usage with the new format-specific error variants.
BinaryFormat
@deprecated Use Binary(BinaryError::Format) instead
This variant is deprecated and will be removed in a future version.
Use Binary(BinaryError::Format) instead for binary format errors.
§Migration
Replace usage with the new format-specific error variants.
VersionMismatch
@deprecated Use Binary(BinaryError::VersionMismatch) instead
This variant is deprecated and will be removed in a future version.
Use Binary(BinaryError::VersionMismatch) instead for version mismatch errors.
§Migration
Replace usage with the new format-specific error variants.
InvalidMagic
@deprecated Use Binary(BinaryError::InvalidMagic) instead
This variant is deprecated and will be removed in a future version.
Use Binary(BinaryError::InvalidMagic) instead for invalid magic number errors.
§Migration
Replace usage with the new format-specific error variants.
Implementations§
Source§impl SerializationError
impl SerializationError
Sourcepub fn json_format(
message: String,
line: Option<usize>,
column: Option<usize>,
) -> Self
pub fn json_format( message: String, line: Option<usize>, column: Option<usize>, ) -> Self
Create a JSON format error
This method provides a convenient way to create JSON format errors with optional line and column information for debugging.
§Arguments
message- Human-readable error message describing the JSON format problemline- Line number where the error occurred (1-based, if available)column- Column number where the error occurred (1-based, if available)
§Returns
A SerializationError::Json variant with the specified format error
Sourcepub fn binary_format(message: String, position: Option<usize>) -> Self
pub fn binary_format(message: String, position: Option<usize>) -> Self
Create a binary format error
This method provides a convenient way to create binary format errors with optional position information for debugging.
§Arguments
message- Human-readable error message describing the binary format problemposition- Byte position where the error occurred (if available)
§Returns
A SerializationError::Binary variant with the specified format error
Sourcepub fn binary_version_mismatch(expected: u32, found: u32) -> Self
pub fn binary_version_mismatch(expected: u32, found: u32) -> Self
Create a binary version mismatch error
This method provides a convenient way to create binary version mismatch errors when the expected and actual format versions don’t match.
§Arguments
expected- Expected format version that the deserializer supportsfound- Actual format version found in the binary data
§Returns
A SerializationError::Binary variant with the specified version mismatch error
Sourcepub fn binary_invalid_magic(expected: u32, found: u32) -> Self
pub fn binary_invalid_magic(expected: u32, found: u32) -> Self
Create a binary invalid magic error
This method provides a convenient way to create binary invalid magic errors when the binary data doesn’t start with the expected magic number.
§Arguments
expected- Expected magic number value for the binary formatfound- Actual magic number found at the beginning of the data
§Returns
A SerializationError::Binary variant with the specified invalid magic error