Module error

Module error 

Source
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:

  1. No Panics: All error conditions are represented as Result values. The library enforces this through #![deny(clippy::panic)] and #![deny(clippy::unwrap_used)].

  2. Contextual Information: Errors include descriptive messages that help diagnose the root cause of failures.

  3. Error Chaining: Where possible, errors preserve the underlying cause through the source() method, enabling full error traces.

  4. Cloneable Errors: The ParcodeError type is Clone, allowing errors to be shared across threads or stored for later analysis.

§Error Categories

Errors are categorized by their domain:

§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§

ParcodeError
The master error enum covering all failure domains in Parcode.

Type Aliases§

Result
A specialized Result type for Parcode operations.