qubit-codec 0.17.4

Core codec traits and buffer conversion primitives for Rust
Documentation
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Serde JSON bytes decoder for one owned value.

use core::marker::PhantomData;

use serde::de::DeserializeOwned;
use serde_json::Error as JsonError;
use serde_json::from_slice;

use super::ValueDecoder;

/// Decodes JSON bytes into one owned value via [`serde_json`].
/// Its stateless marker implements `Clone`, `Copy`, and `Default` without
/// requiring those traits on `T`.
///
/// # Type Parameters
///
/// - `T`: Target value type deserialized through Serde.
///
/// # Examples
///
/// ```rust
/// use qubit_codec::{JsonBytesValueDecoder, ValueDecoder};
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize, PartialEq)]
/// struct Point {
///     x: i32,
///     y: i32,
/// }
///
/// let mut decoder = JsonBytesValueDecoder::<Point>::new();
/// let point = decoder.decode(br#"{"x":1,"y":2}"#).unwrap();
/// assert_eq!(point, Point { x: 1, y: 2 });
/// ```
#[must_use]
#[derive(Debug)]
pub struct JsonBytesValueDecoder<T> {
    /// Associates the decoder with `T` without retaining a value.
    _marker: PhantomData<T>,
}

impl<T> Copy for JsonBytesValueDecoder<T> {}

impl<T> Clone for JsonBytesValueDecoder<T> {
    /// Returns an identical stateless decoder without requiring `T: Clone`.
    ///
    /// # Returns
    ///
    /// Returns a decoder associated with the same target type.
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Default for JsonBytesValueDecoder<T> {
    /// Creates a decoder for `T` without constructing a value of that type.
    ///
    /// # Returns
    ///
    /// Returns an uninitialized stateless decoder associated with `T`.
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<T> JsonBytesValueDecoder<T> {
    /// Creates a JSON bytes decoder for values of type `T`.
    ///
    /// # Returns
    ///
    /// Returns a stateless decoder without allocating or constructing a `T`.
    #[inline]
    pub const fn new() -> Self {
        Self { _marker: PhantomData }
    }
}

impl<T> ValueDecoder<[u8]> for JsonBytesValueDecoder<T>
where
    T: DeserializeOwned,
{
    /// JSON syntax or target-value deserialization failure.
    type Error = JsonError;
    /// Owned value deserialized independently of the input byte slice.
    type Output = T;

    /// Deserializes `input` using [`serde_json::from_slice`].
    ///
    /// # Parameters
    ///
    /// - `input`: One complete JSON value, optionally surrounded by whitespace.
    ///   String-content validation follows `T`'s Serde deserialization
    ///   implementation.
    ///
    /// # Returns
    ///
    /// Returns an owned `T`; decoding may allocate according to the target
    /// type.
    ///
    /// # Errors
    ///
    /// Returns a JSON error for malformed or incomplete input, trailing
    /// non-whitespace data, or a value incompatible with `T`. Validation
    /// follows serde_json and `T`'s `Deserialize` implementation; byte-oriented
    /// targets may accept non-UTF-8 string contents.
    #[inline]
    fn decode(&mut self, input: &[u8]) -> Result<Self::Output, Self::Error> {
        from_slice(input)
    }
}