1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! [![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs]
//!
//! [CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/numcodecs-rs/ci.yml?branch=main
//! [workflow]: https://github.com/juntyr/numcodecs-rs/actions/workflows/ci.yml?query=branch%3Amain
//!
//! [MSRV]: https://img.shields.io/badge/MSRV-1.76.0-blue
//! [repo]: https://github.com/juntyr/numcodecs-rs
//!
//! [Latest Version]: https://img.shields.io/crates/v/numcodecs-wasm-guest
//! [crates.io]: https://crates.io/crates/numcodecs-wasm-guest
//!
//! [Rust Doc Crate]: https://img.shields.io/docsrs/numcodecs-wasm-guest
//! [docs.rs]: https://docs.rs/numcodecs-wasm-guest/
//!
//! [Rust Doc Main]: https://img.shields.io/badge/docs-main-blue
//! [docs]: https://juntyr.github.io/numcodecs-rs/numcodecs_wasm_guest
//!
//! wasm32-wasi guest-side bindings for the [`numcodecs`] API, which allows you
//! to export one [`StaticCodec`] from a WASM component.

// Required in docs and the [`export_codec`] macro
#[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]
/// Export a [`StaticCodec`] type using the WASM component model.
///
/// ```rust,ignore
/// # use numcodecs_wasm_guest::export_codec;
///
/// struct MyCodec {
///     // ...
/// }
///
/// impl numcodecs::Codec for MyCodec {
///     // ...
/// }
///
/// impl numcodecs::StaticCodec for MyCodec {
///     // ...
/// }
///
/// export_codec!(MyCodec);
/// ```
macro_rules! export_codec {
    ($codec:ty) => {
        type Codec = $codec;

        const _: () = {
            const fn can_only_export_static_codec<T: $crate::numcodecs::StaticCodec>() {}

            can_only_export_static_codec::<Codec>()
        };

        #[cfg(target_arch = "wasm32")]
        $crate::bindings::export!(Codec with_types_in $crate::bindings);
    };
}

#[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)),
        }
    }
}