use numcodecs::{
AnyArray, AnyArrayAssignError, AnyArrayView, AnyArrayViewMut, AnyCowArray, Codec, StaticCodec,
StaticCodecConfig, StaticCodecVersion,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct IdentityCodec {
#[serde(default, rename = "_version")]
pub version: StaticCodecVersion<1, 0, 0>,
}
impl Codec for IdentityCodec {
type Error = IdentityCodecError;
fn encode(&self, data: AnyCowArray) -> Result<AnyArray, Self::Error> {
Ok(data.into_owned())
}
fn decode(&self, encoded: AnyCowArray) -> Result<AnyArray, Self::Error> {
Ok(encoded.into_owned())
}
fn decode_into(
&self,
encoded: AnyArrayView,
mut decoded: AnyArrayViewMut,
) -> Result<(), Self::Error> {
Ok(decoded.assign(&encoded)?)
}
}
impl StaticCodec for IdentityCodec {
const CODEC_ID: &'static str = "identity.rs";
type Config<'de> = Self;
fn from_config(config: Self::Config<'_>) -> Self {
config
}
fn get_config(&self) -> StaticCodecConfig<'_, Self> {
StaticCodecConfig::from(self)
}
}
#[derive(Debug, Error)]
pub enum IdentityCodecError {
#[error("Identity cannot decode into the provided array")]
MismatchedDecodeIntoArray {
#[from]
source: AnyArrayAssignError,
},
}