Expand description
Centralized error handling for Parcode.
This module provides a robust error handling system that strictly avoids panics,
ensuring that all failure conditions are properly propagated through the Result type.
§Design Philosophy
Parcode’s error handling is designed with the following principles:
-
No Panics: All error conditions are represented as
Resultvalues. The library enforces this through#![deny(clippy::panic)]and#![deny(clippy::unwrap_used)]. -
Contextual Information: Errors include descriptive messages that help diagnose the root cause of failures.
-
Error Chaining: Where possible, errors preserve the underlying cause through the
source()method, enabling full error traces. -
Cloneable Errors: The
ParcodeErrortype isClone, allowing errors to be shared across threads or stored for later analysis.
§Error Categories
Errors are categorized by their domain:
- I/O Errors (
ParcodeError::Io): Low-level file system operations - Serialization Errors (
ParcodeError::Serialization): Bincode encoding/decoding - Compression Errors (
ParcodeError::Compression): Compression/decompression failures - Format Errors (
ParcodeError::Format): Invalid file format or corruption - Internal Errors (
ParcodeError::Internal): Logic errors (should not occur in production)
§Usage Patterns
§Basic Error Handling
use parcode::{Parcode, ParcodeError, ParcodeObject};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, ParcodeObject)]
struct MyData { val: i32 }
let my_data = MyData { val: 10 };
match Parcode::save("data_err.par", &my_data) {
Ok(()) => println!("Saved successfully"),
Err(ParcodeError::Io(e)) => eprintln!("I/O error: {}", e),
Err(e) => eprintln!("Other error: {}", e),
}§Error Propagation with ?
use parcode::{Parcode, ParcodeObject};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, ParcodeObject)]
struct GameState { level: u32 }
fn save_game_state(state: &GameState) -> parcode::Result<()> {
Parcode::save("game_err.par", state)?;
Ok(())
}§Accessing Error Sources
use std::error::Error;
use parcode::{Parcode, ParcodeObject};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, ParcodeObject)]
struct MyData { val: i32 }
let my_data = MyData { val: 10 };
if let Err(e) = Parcode::save("data_source.par", &my_data) {
eprintln!("Error: {}", e);
if let Some(source) = e.source() {
eprintln!("Caused by: {}", source);
}
}Enums§
- Parcode
Error - The master error enum covering all failure domains in Parcode.
Type Aliases§
- Result
- A specialized
Resulttype for Parcode operations.