SerializationError

Enum SerializationError 

Source
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 validation
  • message - 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

§field: String

Name of the field that failed validation

§message: String

Human-readable message explaining why validation failed

§

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 dimensions
  • found_dims - Actual tensor dimensions found

§Common Causes

  • Incorrect tensor serialization
  • Version incompatibilities
  • Manual data corruption
  • Serialization format changes
  • Dimension calculation errors

Fields

§expected_dims: Vec<usize>

Expected tensor dimensions

§found_dims: Vec<usize>

Actual tensor dimensions found

§

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 requested
  • available_memory - Number of bytes available (if known)

§Common Causes

  • Insufficient system memory
  • Memory fragmentation
  • Resource limits exceeded
  • Large tensor allocations
  • Memory pool exhaustion

Fields

§requested_size: usize

Number of bytes that were requested

§available_memory: Option<usize>

Number of bytes available (if known)

§

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.

Fields

§message: String
§column: Option<usize>
§

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.

Fields

§message: String
§position: Option<usize>
§

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.

Fields

§expected: u32
§found: u32
§

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.

Fields

§expected: u32
§found: u32

Implementations§

Source§

impl SerializationError

Source

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 problem
  • line - 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

Source

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 problem
  • position - Byte position where the error occurred (if available)
§Returns

A SerializationError::Binary variant with the specified format error

Source

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 supports
  • found - Actual format version found in the binary data
§Returns

A SerializationError::Binary variant with the specified version mismatch error

Source

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 format
  • found - Actual magic number found at the beginning of the data
§Returns

A SerializationError::Binary variant with the specified invalid magic error

Trait Implementations§

Source§

impl Debug for SerializationError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for SerializationError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for SerializationError

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<&str> for SerializationError

Source§

fn from(message: &str) -> Self

Convert a string slice to a SerializationError

This implementation allows string slices to be automatically converted to SerializationError::Custom instances, providing a convenient way to create custom error messages from string literals.

Source§

impl From<Error> for SerializationError

Source§

fn from(err: Error) -> Self

Convert an I/O error to a SerializationError

This implementation allows I/O errors to be automatically converted to SerializationError instances, enabling seamless error propagation in serialization operations.

Source§

impl From<String> for SerializationError

Source§

fn from(message: String) -> Self

Convert a String to a SerializationError

This implementation allows String values to be automatically converted to SerializationError::Custom instances, providing a convenient way to create custom error messages.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.