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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::{
    global::{
        EntityId, IVec2, IVec3, IVec4, Mat4, ProceduralMaterialHandle, ProceduralMeshHandle,
        ProceduralSamplerHandle, ProceduralTextureHandle, Quat, UVec2, UVec3, UVec4, Vec2, Vec3,
        Vec4,
    },
    internal::{
        component::Component,
        conversion::{FromBindgen, IntoBindgen},
        wit,
    },
};
use serde::{Deserialize, Serialize};

use ambient_shared_types::primitive_component_definitions;
use std::time::Duration;

#[doc(hidden)]
pub fn get_component<T>(id: &str) -> Component<T> {
    match wit::component::get_index(id) {
        Some(id) => Component::new(id),
        None => {
            panic!("The component `{id}` was not registered with Ambient. Are you sure that this package was built for this version of Ambient, and that there are no stale build files?");
        }
    }
}

/// Implemented by all types that can be used as values in components.
pub trait SupportedValue
where
    Self: Sized,
{
    #[doc(hidden)]
    fn from_result(result: wit::component::Value) -> Option<Self>;

    #[doc(hidden)]
    fn into_result(self) -> wit::component::Value;

    /// Converts a `ComponentValue` into a `Self`.
    fn from_value(value: ComponentValue) -> Option<Self>;
    /// Converts a `Self` into a `ComponentValue`.
    fn into_value(self) -> ComponentValue;
}

/// Implemented by all types that can be used as referenceable values in components.
pub trait SupportedValueRef
where
    Self: SupportedValue,
{
    /// Converts a `&ComponentValue` into a `&Self`.
    fn from_value_ref(value: &ComponentValue) -> Option<&Self>;
}

macro_rules! define_component_types {
    ($(($value:ident, $type:ty)),*) => { paste::paste! {
        /// A value that can be stored in a component.
        #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
        #[allow(missing_docs)]
        pub enum ComponentValue {
            $(
                [<$value>]($type),
            )*
            Vec(ComponentVecValue),
            Option(ComponentOptionValue),
        }

        /// A vector value that can be stored in a component.
        #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
        #[allow(missing_docs)]
        pub enum ComponentVecValue {
            $(
                [<$value>](Vec<$type>),
            )*
        }

        /// An option value that can be stored in a component.
        #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
        #[allow(missing_docs)]
        pub enum ComponentOptionValue {
            $(
                [<$value>](Option<$type>),
            )*
        }

        impl IntoBindgen for ComponentValue {
            type Item = wit::component::Value;
            fn into_bindgen(self) -> Self::Item {
                match self {
                    $(
                        Self::[<$value>](v) => wit::component::Value::[< Type $value >](v.into_bindgen()),
                    )*
                    Self::Vec(v) => wit::component::Value::TypeVec(v.into_bindgen()),
                    Self::Option(v) => wit::component::Value::TypeOption(v.into_bindgen()),
                }
            }
        }
        impl FromBindgen for wit::component::Value {
            type Item = ComponentValue;
            fn from_bindgen(self) -> Self::Item {
                match self {
                    $(
                        wit::component::Value::[< Type $value >](v) => ComponentValue::[<$value>](v.from_bindgen()),
                    )*
                    wit::component::Value::TypeVec(v) => ComponentValue::Vec(v.from_bindgen()),
                    wit::component::Value::TypeOption(v) => ComponentValue::Option(v.from_bindgen()),
                }
            }
        }

        impl IntoBindgen for ComponentVecValue {
            type Item = wit::component::VecValue;
            fn into_bindgen(self) -> Self::Item {
                match self {
                    $(
                        Self::[<$value>](v) => wit::component::VecValue::[< Type $value >](v.into_bindgen()),
                    )*
                }
            }
        }
        impl FromBindgen for wit::component::VecValue {
            type Item = ComponentVecValue;
            fn from_bindgen(self) -> Self::Item {
                match self {
                    $(
                        wit::component::VecValue::[< Type $value >](v) => ComponentVecValue::[<$value>](v.from_bindgen()),
                    )*
                }
            }
        }

        impl IntoBindgen for ComponentOptionValue {
            type Item = wit::component::OptionValue;
            fn into_bindgen(self) -> Self::Item {
                match self {
                    $(
                        Self::[<$value>](v) => wit::component::OptionValue::[< Type $value >](v.into_bindgen()),
                    )*
                }
            }
        }
        impl FromBindgen for wit::component::OptionValue {
            type Item = ComponentOptionValue;
            fn from_bindgen(self) -> Self::Item {
                match self {
                    $(
                        wit::component::OptionValue::[< Type $value >](v) => ComponentOptionValue::[<$value>](v.from_bindgen()),
                    )*
                }
            }
        }

        $(
        impl SupportedValue for $type {
            fn from_result(result: wit::component::Value) -> Option<Self> {
                match result {
                    wit::component::Value::[< Type $value >](v) => Some(v.from_bindgen()),
                    _ => None,
                }
            }

            fn into_result(self) -> wit::component::Value {
                wit::component::Value::[< Type $value >](self.into_bindgen())
            }

            fn from_value(value: ComponentValue) -> Option<Self> {
                match value {
                    ComponentValue::[<$value>](v) => Some(v),
                    _ => None,
                }
            }
            fn into_value(self) -> ComponentValue {
                ComponentValue::[<$value>](self)
            }
        }
        impl SupportedValueRef for $type {
            fn from_value_ref(value: &ComponentValue) -> Option<&Self> {
                match value {
                    ComponentValue::[<$value>](v) => Some(v),
                    _ => None,
                }
            }

        }
        impl SupportedValue for Vec<$type> {
            fn from_result(result: wit::component::Value) -> Option<Self> {
                match result {
                    wit::component::Value::TypeVec(wit::component::VecValue::[< Type $value >](v)) => Some(v.into_iter().map(|v| v.from_bindgen()).collect()),
                    _ => None,
                }
            }

            fn into_result(self) -> wit::component::Value {
                wit::component::Value::TypeVec(wit::component::VecValue::[< Type $value >](self.into_bindgen()))
            }

            fn from_value(value: ComponentValue) -> Option<Self> {
                match value {
                    ComponentValue::Vec(ComponentVecValue::[<$value>](v)) => Some(v),
                    _ => None,
                }
            }

            fn into_value(self) -> ComponentValue {
                ComponentValue::Vec(ComponentVecValue::[<$value>](self))
            }
        }
        impl SupportedValueRef for Vec<$type> {
            fn from_value_ref(value: &ComponentValue) -> Option<&Self> {
                match value {
                    ComponentValue::Vec(ComponentVecValue::[<$value>](v)) => Some(v),
                    _ => None,
                }
            }
        }
        impl SupportedValue for Option<$type> {
            fn from_result(result: wit::component::Value) -> Option<Self> {
                match result {
                    wit::component::Value::TypeOption(wit::component::OptionValue::[< Type $value >](v)) => Some(v.from_bindgen()),
                    _ => None,
                }
            }

            fn into_result(self) -> wit::component::Value {
                wit::component::Value::TypeOption(wit::component::OptionValue::[< Type $value >](self.into_bindgen()))
            }

            fn from_value(value: ComponentValue) -> Option<Self> {
                match value {
                    ComponentValue::Option(ComponentOptionValue::[<$value>](v)) => Some(v),
                    _ => None,
                }
            }

            fn into_value(self) -> ComponentValue {
                ComponentValue::Option(ComponentOptionValue::[<$value>](self))
            }
        }
        impl SupportedValueRef for Option<$type> {
            fn from_value_ref(value: &ComponentValue) -> Option<&Self> {
                match value {
                    ComponentValue::Option(ComponentOptionValue::[<$value>](v)) => Some(v),
                    _ => None,
                }
            }
        }
        ) *
    } }
}

primitive_component_definitions!(define_component_types);