Skip to main content

qubit_codec/value/
value_decoder.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Owned-value decoder trait.
9
10/// Decodes a borrowed input value into an owned representation.
11///
12/// This trait is a convenience-layer API. Use [`crate::Codec`] for low-level
13/// single-value buffer decoding and [`crate::Transcoder`] for batch
14/// conversion over caller-provided buffers.
15///
16/// # Examples
17///
18/// ```
19/// use qubit_codec::ValueDecoder;
20///
21/// struct Identity;
22/// impl ValueDecoder<str> for Identity {
23///     type Output = String;
24///     type Error = core::convert::Infallible;
25///
26///     fn decode(&mut self, input: &str) -> Result<Self::Output, Self::Error> {
27///         Ok(input.to_owned())
28///     }
29/// }
30///
31/// let mut decoder = Identity;
32/// assert_eq!(decoder.decode("hello").unwrap(), "hello");
33/// ```
34pub trait ValueDecoder<Input: ?Sized> {
35    /// Decoded output type.
36    type Output;
37    /// Decoding error type.
38    type Error;
39
40    /// Decodes `input`.
41    ///
42    /// # Parameters
43    /// - `input`: Source value to decode.
44    ///
45    /// # Returns
46    /// Decoded output.
47    ///
48    /// # Errors
49    /// Returns an error when the input is malformed or unsupported by the
50    /// codec.
51    fn decode(&mut self, input: &Input) -> Result<Self::Output, Self::Error>;
52}