numcodecs_wasm_guest/
lib.rs#![allow(clippy::multiple_crate_versions)]
#[doc(hidden)]
pub use numcodecs;
#[cfg(doc)]
use numcodecs::StaticCodec;
#[cfg(target_arch = "wasm32")]
use ::{
numcodecs::{Codec, StaticCodec},
schemars::schema_for,
serde::Deserialize,
};
#[cfg(target_arch = "wasm32")]
mod convert;
#[cfg(target_arch = "wasm32")]
use crate::{
bindings::exports::numcodecs::abc::codec as wit,
convert::{
from_wit_any_array, into_wit_any_array, into_wit_error, zeros_from_wit_any_array_prototype,
},
};
#[doc(hidden)]
#[allow(clippy::missing_safety_doc)]
pub mod bindings {
wit_bindgen::generate!({
world: "numcodecs:abc/exports@0.1.1",
with: {
"numcodecs:abc/codec@0.1.1": generate,
},
pub_export_macro: true,
});
}
#[macro_export]
macro_rules! export_codec {
($codec:ty) => {
#[cfg(target_arch = "wasm32")]
const _: () = {
type Codec = $codec;
$crate::bindings::export!(
Codec with_types_in $crate::bindings
);
};
const _: () = {
const fn can_only_export_static_codec<T: $crate::numcodecs::StaticCodec>() {}
can_only_export_static_codec::<$codec>()
};
};
}
#[cfg(target_arch = "wasm32")]
#[doc(hidden)]
impl<T: StaticCodec> wit::Guest for T {
type Codec = Self;
fn codec_id() -> String {
String::from(<Self as StaticCodec>::CODEC_ID)
}
fn codec_config_schema() -> wit::JsonSchema {
schema_for!(<Self as StaticCodec>::Config<'static>)
.as_value()
.to_string()
}
}
#[cfg(target_arch = "wasm32")]
impl<T: StaticCodec> wit::GuestCodec for T {
fn from_config(config: String) -> Result<wit::Codec, wit::Error> {
let err = match <Self as StaticCodec>::Config::deserialize(
&mut serde_json::Deserializer::from_str(&config),
) {
Ok(config) => return Ok(wit::Codec::new(<Self as StaticCodec>::from_config(config))),
Err(err) => err,
};
let err = format_serde_error::SerdeError::new(config, err);
Err(into_wit_error(err))
}
fn encode(&self, data: wit::AnyArray) -> Result<wit::AnyArray, wit::Error> {
let data = match from_wit_any_array(data) {
Ok(data) => data,
Err(err) => return Err(into_wit_error(err)),
};
match <Self as Codec>::encode(self, data.into_cow()) {
Ok(encoded) => match into_wit_any_array(encoded) {
Ok(encoded) => Ok(encoded),
Err(err) => Err(into_wit_error(err)),
},
Err(err) => Err(into_wit_error(err)),
}
}
fn decode(&self, encoded: wit::AnyArray) -> Result<wit::AnyArray, wit::Error> {
let encoded = match from_wit_any_array(encoded) {
Ok(encoded) => encoded,
Err(err) => return Err(into_wit_error(err)),
};
match <Self as Codec>::decode(self, encoded.into_cow()) {
Ok(decoded) => match into_wit_any_array(decoded) {
Ok(decoded) => Ok(decoded),
Err(err) => Err(into_wit_error(err)),
},
Err(err) => Err(into_wit_error(err)),
}
}
fn decode_into(
&self,
encoded: wit::AnyArray,
decoded: wit::AnyArrayPrototype,
) -> Result<wit::AnyArray, wit::Error> {
let encoded = match from_wit_any_array(encoded) {
Ok(encoded) => encoded,
Err(err) => return Err(into_wit_error(err)),
};
let mut decoded = zeros_from_wit_any_array_prototype(decoded);
match <Self as Codec>::decode_into(self, encoded.view(), decoded.view_mut()) {
Ok(()) => match into_wit_any_array(decoded) {
Ok(decoded) => Ok(decoded),
Err(err) => Err(into_wit_error(err)),
},
Err(err) => Err(into_wit_error(err)),
}
}
fn get_config(&self) -> Result<wit::Json, wit::Error> {
match serde_json::to_string(&<Self as StaticCodec>::get_config(self)) {
Ok(config) => Ok(config),
Err(err) => Err(into_wit_error(err)),
}
}
}