celestia_types/
any.rs

1//! Helpers for dealing with `Any` types conversion
2
3use prost::Name;
4use tendermint_proto::google::protobuf::Any;
5
6/// Value convertion into protobuf's Any
7pub trait IntoProtobufAny {
8    /// Converts itself into protobuf's Any type
9    fn into_any(self) -> Any;
10}
11
12impl<T> IntoProtobufAny for T
13where
14    T: Name,
15{
16    fn into_any(self) -> Any {
17        Any {
18            type_url: T::type_url(),
19            value: self.encode_to_vec(),
20        }
21    }
22}
23
24#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
25pub use wbg::*;
26
27#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
28mod wbg {
29    use super::*;
30    use js_sys::Uint8Array;
31    use lumina_utils::make_object;
32    use wasm_bindgen::prelude::*;
33
34    #[wasm_bindgen(typescript_custom_section)]
35    const _: &str = "
36    /**
37     * Protobuf Any type
38     */
39    export interface ProtoAny {
40      typeUrl: string;
41      value: Uint8Array;
42    }
43    ";
44
45    #[wasm_bindgen]
46    extern "C" {
47        /// Protobuf Any exposed to javascript
48        #[wasm_bindgen(typescript_type = "ProtoAny")]
49        pub type JsAny;
50
51        /// Any type URL
52        #[wasm_bindgen(method, getter, js_name = typeUrl)]
53        pub fn type_url(this: &JsAny) -> String;
54
55        /// Any type value
56        #[wasm_bindgen(method, getter, js_name = value)]
57        pub fn value(this: &JsAny) -> Vec<u8>;
58    }
59
60    impl From<Any> for JsAny {
61        fn from(value: Any) -> Self {
62            let obj = make_object!(
63                "typeUrl" => value.type_url.into(),
64                "value" => Uint8Array::from(&value.value[..])
65            );
66
67            obj.unchecked_into()
68        }
69    }
70
71    impl IntoProtobufAny for JsAny {
72        fn into_any(self) -> Any {
73            Any {
74                type_url: self.type_url(),
75                value: self.value(),
76            }
77        }
78    }
79}
80
81/// uniffi conversion types
82#[cfg(feature = "uniffi")]
83mod uniffi_types {
84    use tendermint_proto::google::protobuf::Any as ProtobufAny;
85
86    /// Any contains an arbitrary serialized protocol buffer message along with a URL that
87    /// describes the type of the serialized message.
88    #[uniffi::remote(Record)]
89    pub struct ProtobufAny {
90        /// A URL/resource name that uniquely identifies the type of the serialized protocol
91        /// buffer message. This string must contain at least one “/” character. The last
92        /// segment of the URL’s path must represent the fully qualified name of the type
93        /// (as in path/google.protobuf.Duration). The name should be in a canonical form
94        /// (e.g., leading “.” is not accepted).
95        pub type_url: String,
96        /// Must be a valid serialized protocol buffer of the above specified type.
97        pub value: Vec<u8>,
98    }
99}