Skip to main content

numcodecs_onion/
lib.rs

1//! [![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs]
2//!
3//! [CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/numcodecs-rs/ci.yml?branch=main
4//! [workflow]: https://github.com/juntyr/numcodecs-rs/actions/workflows/ci.yml?query=branch%3Amain
5//!
6//! [MSRV]: https://img.shields.io/badge/MSRV-1.88.0-blue
7//! [repo]: https://github.com/juntyr/numcodecs-rs
8//!
9//! [Latest Version]: https://img.shields.io/crates/v/numcodecs-onion
10//! [crates.io]: https://crates.io/crates/numcodecs-onion
11//!
12//! [Rust Doc Crate]: https://img.shields.io/docsrs/numcodecs-onion
13//! [docs.rs]: https://docs.rs/numcodecs-onion/
14//!
15//! [Rust Doc Main]: https://img.shields.io/badge/docs-main-blue
16//! [docs]: https://juntyr.github.io/numcodecs-rs/numcodecs_onion
17//!
18//! Onion identity meta-codec implementation for the [`numcodecs`] API.
19
20use numcodecs::{
21    AnyArray, AnyArrayView, AnyArrayViewMut, AnyCowArray, Codec, DynCodec, ErasedDynCodec,
22    ErasedError, StaticCodec, StaticCodecConfig, StaticCodecVersion,
23};
24use numcodecs_registry::GlobalRegistry;
25use schemars::JsonSchema;
26use serde::{Deserialize, Serialize};
27use thiserror::Error;
28
29#[cfg(test)]
30use ::serde_json as _;
31
32#[derive(Clone, Serialize, Deserialize, JsonSchema)]
33#[serde(deny_unknown_fields)]
34/// Onion identity meta-codec which wraps an existing codec and passes the
35/// inputs and outputs unchanged to and from this codec during encoding and
36/// decoding.
37pub struct OnionCodec {
38    /// The configuration of the wrapped codec.
39    #[serde(serialize_with = "DynCodec::get_config")]
40    #[serde(deserialize_with = "GlobalRegistry::codec_from_config")]
41    #[schemars(schema_with = "ErasedDynCodec::codec_config_schema")]
42    pub codec: ErasedDynCodec,
43    /// The codec's encoding format version. Do not provide this parameter explicitly.
44    #[serde(default, rename = "_version")]
45    pub version: StaticCodecVersion<1, 0, 0>,
46}
47
48impl Codec for OnionCodec {
49    type Error = OnionCodecError;
50
51    fn encode(&self, data: AnyCowArray) -> Result<AnyArray, Self::Error> {
52        self.codec
53            .encode(data)
54            .map_err(|err| OnionCodecError { error: err })
55    }
56
57    fn decode(&self, encoded: AnyCowArray) -> Result<AnyArray, Self::Error> {
58        self.codec
59            .decode(encoded)
60            .map_err(|err| OnionCodecError { error: err })
61    }
62
63    fn decode_into(
64        &self,
65        encoded: AnyArrayView,
66        decoded: AnyArrayViewMut,
67    ) -> Result<(), Self::Error> {
68        self.codec
69            .decode_into(encoded, decoded)
70            .map_err(|err| OnionCodecError { error: err })
71    }
72}
73
74impl StaticCodec for OnionCodec {
75    const CODEC_ID: &'static str = "onion.rs";
76
77    type Config<'de> = Self;
78
79    fn from_config(config: Self::Config<'_>) -> Self {
80        config
81    }
82
83    fn get_config(&self) -> StaticCodecConfig<'_, Self> {
84        StaticCodecConfig::from(self)
85    }
86}
87
88#[derive(Debug, Error)]
89/// Error that may occur when applying the [`OnionCodec`].
90#[error(transparent)]
91pub struct OnionCodecError {
92    error: ErasedError,
93}