use std::num::NonZeroU64;
use std::sync::Arc;
use zarrs_plugin::ZarrVersion;
use crate::array::{ChunkShape, DataType, FillValue};
#[cfg(feature = "async")]
use zarrs_codec::AsyncArrayPartialDecoderTraits;
use zarrs_codec::{
ArrayBytes, ArrayCodecTraits, ArrayPartialDecoderTraits, ArrayPartialEncoderTraits,
ArrayToArrayCodecTraits, CodecError, CodecMetadataOptions, CodecOptions, CodecTraits,
PartialDecoderCapability, PartialEncoderCapability, RecommendedConcurrency,
};
use zarrs_metadata::Configuration;
use zarrs_metadata_ext::codec::reshape::{
ReshapeCodecConfiguration, ReshapeCodecConfigurationV1, ReshapeShape,
};
use zarrs_plugin::PluginCreateError;
#[derive(Clone, Debug)]
pub struct ReshapeCodec {
shape: ReshapeShape,
}
impl ReshapeCodec {
pub fn new_with_configuration(
configuration: &ReshapeCodecConfiguration,
) -> Result<Self, PluginCreateError> {
match configuration {
ReshapeCodecConfiguration::V1(configuration) => {
Ok(Self::new(configuration.shape.clone()))
}
_ => Err(PluginCreateError::Other(
"this reshape codec configuration variant is unsupported".to_string(),
)),
}
}
#[must_use]
pub const fn new(shape: ReshapeShape) -> Self {
Self { shape }
}
}
impl CodecTraits for ReshapeCodec {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn configuration(
&self,
_version: ZarrVersion,
_options: &CodecMetadataOptions,
) -> Option<Configuration> {
let configuration = ReshapeCodecConfiguration::V1(ReshapeCodecConfigurationV1 {
shape: self.shape.clone(),
});
Some(configuration.into())
}
fn partial_decoder_capability(&self) -> PartialDecoderCapability {
PartialDecoderCapability {
partial_read: true,
partial_decode: true,
}
}
fn partial_encoder_capability(&self) -> PartialEncoderCapability {
PartialEncoderCapability {
partial_encode: true,
}
}
}
#[cfg_attr(
all(feature = "async", not(target_arch = "wasm32")),
async_trait::async_trait
)]
#[cfg_attr(all(feature = "async", target_arch = "wasm32"), async_trait::async_trait(?Send))]
impl ArrayToArrayCodecTraits for ReshapeCodec {
fn into_dyn(self: Arc<Self>) -> Arc<dyn ArrayToArrayCodecTraits> {
self as Arc<dyn ArrayToArrayCodecTraits>
}
fn encoded_data_type(&self, decoded_data_type: &DataType) -> Result<DataType, CodecError> {
Ok(decoded_data_type.clone())
}
fn encoded_fill_value(
&self,
_decoded_data_type: &DataType,
decoded_fill_value: &FillValue,
) -> Result<FillValue, CodecError> {
Ok(decoded_fill_value.clone())
}
fn encoded_shape(&self, decoded_shape: &[NonZeroU64]) -> Result<ChunkShape, CodecError> {
super::get_encoded_shape(&self.shape, decoded_shape)
}
fn decoded_shape(
&self,
_encoded_shape: &[NonZeroU64],
) -> Result<Option<ChunkShape>, CodecError> {
Ok(None)
}
fn encode<'a>(
&self,
bytes: ArrayBytes<'a>,
_shape: &[NonZeroU64],
_data_type: &DataType,
_fill_value: &FillValue,
_options: &CodecOptions,
) -> Result<ArrayBytes<'a>, CodecError> {
Ok(bytes)
}
fn decode<'a>(
&self,
bytes: ArrayBytes<'a>,
_shape: &[NonZeroU64],
_data_type: &DataType,
_fill_value: &FillValue,
_options: &CodecOptions,
) -> Result<ArrayBytes<'a>, CodecError> {
Ok(bytes)
}
fn partial_decoder(
self: Arc<Self>,
_input_handle: Arc<dyn ArrayPartialDecoderTraits>,
_shape: &[NonZeroU64],
_data_type: &DataType,
_fill_value: &FillValue,
_options: &CodecOptions,
) -> Result<Arc<dyn ArrayPartialDecoderTraits>, CodecError> {
Err(CodecError::Other(
"partial decoding with the reshape codec is not yet supported".to_string(),
))
}
fn partial_encoder(
self: Arc<Self>,
_input_output_handle: Arc<dyn ArrayPartialEncoderTraits>,
_shape: &[NonZeroU64],
_data_type: &DataType,
_fill_value: &FillValue,
_options: &CodecOptions,
) -> Result<Arc<dyn ArrayPartialEncoderTraits>, CodecError> {
Err(CodecError::Other(
"partial encoding with the reshape codec is not yet supported".to_string(),
))
}
#[cfg(feature = "async")]
async fn async_partial_decoder(
self: Arc<Self>,
_input_handle: Arc<dyn AsyncArrayPartialDecoderTraits>,
_shape: &[NonZeroU64],
_data_type: &DataType,
_fill_value: &FillValue,
_options: &CodecOptions,
) -> Result<Arc<dyn AsyncArrayPartialDecoderTraits>, CodecError> {
Err(CodecError::Other(
"partial decoding with the reshape codec is not yet supported".to_string(),
))
}
}
impl ArrayCodecTraits for ReshapeCodec {
fn recommended_concurrency(
&self,
_shape: &[NonZeroU64],
_data_type: &DataType,
) -> Result<RecommendedConcurrency, CodecError> {
Ok(RecommendedConcurrency::new_maximum(1))
}
}