error.rs

Overview

[error.rs](/projects/287/67767) defines the `DeserializeError` struct and its associated methods, which represent errors encountered during JSON deserialization in the project. This file provides a unified error representation tailored to handle two different JSON parsing backends controlled via Cargo features: the default parser (non-`yyjson`) and the `yyjson` parser.

The primary purpose of this file is to encapsulate error information, including descriptive messages and precise error locations within the input data, to aid debugging and error reporting during deserialization workflows.


Detailed Documentation

Struct: DeserializeError<'a>

Represents an error encountered while deserializing JSON data.

Fields


Associated Methods

invalid(message: Cow<'a, str>) -> Self

Creates a generic `DeserializeError` with an invalid or unspecified position.

let err = DeserializeError::invalid(Cow::from("Invalid JSON format"));
println!("{}", err.message);

from_json(message: Cow<'a, str>, line: usize, column: usize, data: &'a str) -> Self

*(Only available if `yyjson` feature is NOT enabled)*

Creates a `DeserializeError` from a JSON parser that tracks line and column positions.

let err = DeserializeError::from_json(
    Cow::from("Unexpected token"),
    12,
    5,
    r#"{"key": "value", "bad_json": [1, 2,}"#,
);

from_yyjson(message: Cow<'a, str>, pos: i64, data: &'a str) -> Self

*(Only available if `yyjson` feature IS enabled)*

Creates a `DeserializeError` from the `yyjson` parser error positional information.

let err = DeserializeError::from_yyjson(
    Cow::from("Invalid character"),
    45,
    r#"{"key": "value", "bad_json": [1, 2,}"#,
);

pos(&self) -> i64

Returns the error position as a character offset within the JSON data.

let position = error.pos();
println!("Error at character offset: {}", position);

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Mermaid Diagram: Structure of DeserializeError

classDiagram
    class DeserializeError<'a> {
        +message: Cow<'a, str>
        +line: usize [conditional, no "yyjson"]
        +column: usize [conditional, no "yyjson"]
        +data: Option<&'a str>
        +pos: i64 [conditional, "yyjson"]
        +invalid(message: Cow<'a, str>) DeserializeError<'a>
        +from_json(message: Cow<'a, str>, line: usize, column: usize, data: &'a str) DeserializeError<'a> [conditional, no "yyjson"]
        +from_yyjson(message: Cow<'a, str>, pos: i64, data: &'a str) DeserializeError<'a> [conditional, "yyjson"]
        +pos() i64
    }

Summary

The [error.rs](/projects/287/67767) file provides a crucial abstraction for JSON deserialization errors, unifying error details across multiple JSON parsing backends with precise location tracking. It is implemented with careful attention to UTF-8 correctness and performance considerations and integrates seamlessly into the broader deserialization and error handling framework of the system.