numcodecs_identity/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.82.0-blue
7//! [repo]: https://github.com/juntyr/numcodecs-rs
8//!
9//! [Latest Version]: https://img.shields.io/crates/v/numcodecs-identity
10//! [crates.io]: https://crates.io/crates/numcodecs-identity
11//!
12//! [Rust Doc Crate]: https://img.shields.io/docsrs/numcodecs-identity
13//! [docs.rs]: https://docs.rs/numcodecs-identity/
14//!
15//! [Rust Doc Main]: https://img.shields.io/badge/docs-main-blue
16//! [docs]: https://juntyr.github.io/numcodecs-rs/numcodecs_identity
17//!
18//! Identity codec implementation for the [`numcodecs`] API.
19
20use numcodecs::{
21 AnyArray, AnyArrayAssignError, AnyArrayView, AnyArrayViewMut, AnyCowArray, Codec, StaticCodec,
22 StaticCodecConfig,
23};
24use schemars::JsonSchema;
25use serde::{Deserialize, Serialize};
26use thiserror::Error;
27
28#[derive(Clone, Serialize, Deserialize, JsonSchema)]
29#[serde(deny_unknown_fields)]
30/// Identity codec which applies the identity function, i.e. passes through the
31/// input unchanged during encoding and decoding.
32pub struct IdentityCodec {
33 // empty
34}
35
36impl Codec for IdentityCodec {
37 type Error = IdentityCodecError;
38
39 fn encode(&self, data: AnyCowArray) -> Result<AnyArray, Self::Error> {
40 Ok(data.into_owned())
41 }
42
43 fn decode(&self, encoded: AnyCowArray) -> Result<AnyArray, Self::Error> {
44 Ok(encoded.into_owned())
45 }
46
47 fn decode_into(
48 &self,
49 encoded: AnyArrayView,
50 mut decoded: AnyArrayViewMut,
51 ) -> Result<(), Self::Error> {
52 Ok(decoded.assign(&encoded)?)
53 }
54}
55
56impl StaticCodec for IdentityCodec {
57 const CODEC_ID: &'static str = "identity";
58
59 type Config<'de> = Self;
60
61 fn from_config(config: Self::Config<'_>) -> Self {
62 config
63 }
64
65 fn get_config(&self) -> StaticCodecConfig<Self> {
66 StaticCodecConfig::from(self)
67 }
68}
69
70#[derive(Debug, Error)]
71/// Errors that may occur when applying the [`IdentityCodec`].
72pub enum IdentityCodecError {
73 /// [`IdentityCodec`] cannot decode into the provided array
74 #[error("Identity cannot decode into the provided array")]
75 MismatchedDecodeIntoArray {
76 /// The source of the error
77 #[from]
78 source: AnyArrayAssignError,
79 },
80}