brec_node_lib 0.1.0

A flexible binary format for storing and streaming structured data as packets with CRC protection and recoverability from corruption. Built for extensibility and robustness.
Documentation
use crate::NapiFieldHint;
use thiserror::Error;

/// Error details for Rust <-> Node.js conversion in `napi` helpers.
#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum NapiError {
    /// The provided JavaScript value is not a valid object for conversion.
    #[error("Invalid JS object: {0}")]
    InvalidObject(String),
    /// The JavaScript object does not have an expected field.
    #[error("Missing field: {0}")]
    MissingField(String),
    /// A field value could not be converted to the target Rust type.
    #[error("Invalid field value for {0}: {1}")]
    InvalidField(String, String),
    /// The provided JavaScript object shape is invalid for an aggregator.
    #[error("Invalid aggregator object shape: {0}")]
    InvalidAggregatorShape(String),
}

impl NapiError {
    #[inline]
    pub fn invalid_field(hint: NapiFieldHint, err: impl ToString) -> NapiError {
        Self::InvalidField(hint.id().to_string(), err.to_string())
    }

    #[inline]
    pub fn invalid_field_name(name: impl Into<String>, err: impl ToString) -> NapiError {
        Self::InvalidField(name.into(), err.to_string())
    }
}