cel_cxx_ffi/common/
values.rs

1use crate::absl::{Duration, SpanElement, Status, StringView, Timestamp};
2use crate::common::{MessageType, OpaqueType, Type, ValueKind};
3use crate::protobuf::{Arena, DescriptorPool, MessageFactory};
4use std::pin::Pin;
5
6#[cxx::bridge]
7mod ffi {
8    #[namespace = "rust::cel_cxx"]
9    extern "Rust" {
10        type AnyFfiOpaqueValue<'a>;
11        #[cxx_name = "Clone"]
12        unsafe fn clone<'a>(self: &'a AnyFfiOpaqueValue<'a>) -> Box<AnyFfiOpaqueValue<'a>>;
13        #[cxx_name = "DebugString"]
14        unsafe fn to_string<'a>(self: &AnyFfiOpaqueValue<'a>) -> String;
15        #[cxx_name = "GetTypeName"]
16        unsafe fn type_name<'a>(self: &AnyFfiOpaqueValue<'a>) -> string_view<'a>;
17        #[cxx_name = "GetRuntimeType"]
18        unsafe fn runtime_type<'a>(self: &AnyFfiOpaqueValue<'a>) -> OpaqueType<'a>;
19        #[cxx_name = "Equal"]
20        unsafe fn equal<'a>(self: &AnyFfiOpaqueValue<'a>, other: &AnyFfiOpaqueValue<'a>) -> bool;
21    }
22
23    #[namespace = "absl"]
24    unsafe extern "C++" {
25        include!(<absl/time/time.h>);
26        type Duration = super::Duration;
27        type Time = super::Timestamp;
28
29        include!(<absl/status/status.h>);
30        type Status = super::Status;
31
32        type string_view<'a> = super::StringView<'a>;
33    }
34
35    #[namespace = "google::protobuf"]
36    unsafe extern "C++" {
37        include!(<google/protobuf/arena.h>);
38        type Arena = super::Arena;
39
40        include!(<google/protobuf/descriptor.h>);
41        type DescriptorPool = super::DescriptorPool;
42        type MessageFactory = super::MessageFactory;
43    }
44
45    #[namespace = "cel"]
46    unsafe extern "C++" {
47        include!(<base/kind.h>);
48        type ValueKind = super::ValueKind;
49
50        include!(<common/type.h>);
51        type Type<'a> = super::Type<'a>;
52        type OpaqueType<'a> = super::OpaqueType<'a>;
53
54        include!(<common/types/message_type.h>);
55        type MessageType<'a> = super::MessageType<'a>;
56
57        include!(<common/value.h>);
58        type Value<'a>;
59        fn kind(self: &Value) -> ValueKind;
60        #[rust_name = "runtime_type"]
61        fn GetRuntimeType<'a>(self: &Value<'a>) -> Type<'a>;
62        #[rust_name = "is_bool"]
63        fn IsBool(self: &Value) -> bool;
64        #[rust_name = "is_true"]
65        fn IsTrue(self: &Value) -> bool;
66        #[rust_name = "is_false"]
67        fn IsFalse(self: &Value) -> bool;
68        #[rust_name = "is_bytes"]
69        fn IsBytes(self: &Value) -> bool;
70        #[rust_name = "is_double"]
71        fn IsDouble(self: &Value) -> bool;
72        #[rust_name = "is_duration"]
73        fn IsDuration(self: &Value) -> bool;
74        #[rust_name = "is_error"]
75        fn IsError(self: &Value) -> bool;
76        #[rust_name = "is_int"]
77        fn IsInt(self: &Value) -> bool;
78        #[rust_name = "is_list"]
79        fn IsList(self: &Value) -> bool;
80        #[rust_name = "is_map"]
81        fn IsMap(self: &Value) -> bool;
82        #[rust_name = "is_message"]
83        fn IsMessage(self: &Value) -> bool;
84        #[rust_name = "is_null"]
85        fn IsNull(self: &Value) -> bool;
86        #[rust_name = "is_opaque"]
87        fn IsOpaque(self: &Value) -> bool;
88        #[rust_name = "is_optional"]
89        fn IsOptional(self: &Value) -> bool;
90        #[rust_name = "is_string"]
91        fn IsString(self: &Value) -> bool;
92        #[rust_name = "is_struct"]
93        fn IsStruct(self: &Value) -> bool;
94        #[rust_name = "is_timestamp"]
95        fn IsTimestamp(self: &Value) -> bool;
96        #[rust_name = "is_type"]
97        fn IsType(self: &Value) -> bool;
98        #[rust_name = "is_uint"]
99        fn IsUint(self: &Value) -> bool;
100        #[rust_name = "is_unknown"]
101        fn IsUnknown(self: &Value) -> bool;
102
103        // BoolValue
104        type BoolValue;
105        #[rust_name = "native_value"]
106        fn NativeValue(self: &BoolValue) -> bool;
107
108        type BytesValue<'a>;
109        #[rust_name = "len"]
110        fn Size(self: &BytesValue) -> usize;
111        #[rust_name = "is_empty"]
112        fn IsEmpty(self: &BytesValue) -> bool;
113
114        type DoubleValue;
115        #[rust_name = "native_value"]
116        fn NativeValue(self: &DoubleValue) -> f64;
117
118        type DurationValue;
119        #[rust_name = "native_value"]
120        fn NativeValue(self: &DurationValue) -> Duration;
121
122        type ErrorValue<'a>;
123
124        type IntValue;
125        #[rust_name = "native_value"]
126        fn NativeValue(self: &IntValue) -> i64;
127
128        type ListValue<'a>;
129        type ListValueBuilder<'a>;
130
131        type MapValue<'a>;
132        type MapValueBuilder<'a>;
133
134        type MessageValue<'a>;
135        #[rust_name = "runtime_type"]
136        fn GetRuntimeType<'a>(self: &MessageValue<'a>) -> MessageType<'a>;
137
138        type NullValue;
139        type OpaqueValue<'a>;
140        #[rust_name = "is_optional"]
141        fn IsOptional(self: &OpaqueValue) -> bool;
142
143        type OptionalValue<'a>;
144        #[rust_name = "has_value"]
145        fn HasValue(self: &OptionalValue) -> bool;
146
147        type StringValue<'a>;
148        #[rust_name = "len"]
149        fn Size(self: &StringValue) -> usize;
150        #[rust_name = "is_empty"]
151        fn IsEmpty(self: &StringValue) -> bool;
152
153        //type StructValue;
154        // todo
155
156        type TimestampValue;
157        #[rust_name = "native_value"]
158        fn NativeValue(self: &TimestampValue) -> Time;
159
160        type TypeValue<'a>;
161        #[rust_name = "native_value"]
162        fn NativeValue<'a>(self: &TypeValue<'a>) -> &Type<'a>;
163
164        type UintValue;
165        #[rust_name = "native_value"]
166        fn NativeValue(self: &UintValue) -> u64;
167
168        //type UnknownValue;
169        // todo
170
171        type ValueIterator<'a>;
172        #[rust_name = "has_next"]
173        fn HasNext(self: Pin<&mut ValueIterator>) -> bool;
174    }
175
176    #[namespace = "rust::cel_cxx"]
177    unsafe extern "C++" {
178        include!(<cel-cxx-ffi/include/absl.h>);
179        include!(<cel-cxx-ffi/include/values.h>);
180
181        // Value
182        fn Value_size_of() -> usize;
183        fn Value_swap<'a>(lhs: Pin<&mut Value<'a>>, rhs: Pin<&mut Value<'a>>);
184
185        fn Value_new_bool(bool_value: &BoolValue) -> UniquePtr<Value<'static>>;
186        fn Value_new_bytes<'a>(bytes_value: &BytesValue<'a>) -> UniquePtr<Value<'a>>;
187        fn Value_new_double(double_value: &DoubleValue) -> UniquePtr<Value<'static>>;
188        fn Value_new_duration(duration_value: &DurationValue) -> UniquePtr<Value<'static>>;
189        fn Value_new_error<'a>(error_value: &ErrorValue<'a>) -> UniquePtr<Value<'a>>;
190        fn Value_new_int(int_value: &IntValue) -> UniquePtr<Value<'static>>;
191        fn Value_new_list<'a>(list_value: &ListValue<'a>) -> UniquePtr<Value<'a>>;
192        fn Value_new_map<'a>(map_value: &MapValue<'a>) -> UniquePtr<Value<'a>>;
193        fn Value_new_message<'a>(message_value: &MessageValue<'a>) -> UniquePtr<Value<'a>>;
194        fn Value_new_null() -> UniquePtr<Value<'static>>;
195        fn Value_new_opaque<'a>(opaque_value: &OpaqueValue<'a>) -> UniquePtr<Value<'a>>;
196        fn Value_new_optional<'a>(optional_value: &OptionalValue<'a>) -> UniquePtr<Value<'a>>;
197        fn Value_new_string<'a>(string_value: &StringValue<'a>) -> UniquePtr<Value<'a>>;
198        fn Value_new_timestamp(timestamp_value: &TimestampValue) -> UniquePtr<Value<'static>>;
199        fn Value_new_type<'a>(type_value: &TypeValue<'a>) -> UniquePtr<Value<'a>>;
200        fn Value_new_uint(uint_value: &UintValue) -> UniquePtr<Value<'static>>;
201
202        fn Value_get_bool(value: &Value) -> UniquePtr<BoolValue>;
203        fn Value_get_bytes<'a>(value: &Value<'a>) -> UniquePtr<BytesValue<'a>>;
204        fn Value_get_double(value: &Value) -> UniquePtr<DoubleValue>;
205        fn Value_get_duration(value: &Value) -> UniquePtr<DurationValue>;
206        fn Value_get_error<'a>(value: &Value<'a>) -> UniquePtr<ErrorValue<'a>>;
207        fn Value_get_int(value: &Value) -> UniquePtr<IntValue>;
208        fn Value_get_list<'a>(value: &Value<'a>) -> UniquePtr<ListValue<'a>>;
209        fn Value_get_map<'a>(value: &Value<'a>) -> UniquePtr<MapValue<'a>>;
210        fn Value_get_message<'a>(value: &Value<'a>) -> UniquePtr<MessageValue<'a>>;
211        fn Value_get_null(value: &Value) -> UniquePtr<NullValue>;
212        fn Value_get_opaque<'a>(value: &Value<'a>) -> UniquePtr<OpaqueValue<'a>>;
213        fn Value_get_optional<'a>(value: &Value<'a>) -> UniquePtr<OptionalValue<'a>>;
214        fn Value_get_string<'a>(value: &Value<'a>) -> UniquePtr<StringValue<'a>>;
215        fn Value_get_timestamp(value: &Value) -> UniquePtr<TimestampValue>;
216        fn Value_get_type<'a>(value: &Value<'a>) -> UniquePtr<TypeValue<'a>>;
217        fn Value_get_uint(value: &Value) -> UniquePtr<UintValue>;
218
219        // BoolValue
220        fn BoolValue_new(value: bool) -> UniquePtr<BoolValue>;
221
222        // BytesValue
223        fn BytesValue_new<'a>(arena: &'a Arena, bytes: &[u8]) -> UniquePtr<BytesValue<'a>>;
224        fn BytesValue_native_value(bytes_value: &BytesValue) -> Vec<u8>;
225
226        // DoubleValue
227        fn DoubleValue_new(value: f64) -> UniquePtr<DoubleValue>;
228
229        // DurationValue
230        fn DurationValue_new(value: Duration) -> UniquePtr<DurationValue>;
231
232        // ErrorValue
233        fn ErrorValue_new(status: Status) -> UniquePtr<ErrorValue<'static>>;
234        fn ErrorValue_native_value(error_value: &ErrorValue) -> Status;
235
236        // IntValue
237        fn IntValue_new(value: i64) -> UniquePtr<IntValue>;
238
239        // ListValue
240        fn ListValue_is_empty(list_value: &ListValue) -> bool;
241        fn ListValue_size(list_value: &ListValue) -> usize;
242        fn ListValue_new_iterator<'a, 'this>(
243            list_value: &'this ListValue<'a>,
244            iterator: &mut UniquePtr<ValueIterator<'this>>,
245        ) -> Status;
246
247        // ListValueBuilder
248        fn ListValueBuilder_new<'a>(arena: &'a Arena) -> UniquePtr<ListValueBuilder<'a>>;
249        fn ListValueBuilder_add<'a>(
250            builder: Pin<&mut ListValueBuilder<'a>>,
251            value: &Value<'a>,
252        ) -> Status;
253        fn ListValueBuilder_build<'a>(
254            builder: Pin<&mut ListValueBuilder<'a>>,
255        ) -> UniquePtr<ListValue<'a>>;
256
257        // MapValue
258        fn MapValue_is_empty(map_value: &MapValue) -> bool;
259        fn MapValue_size(map_value: &MapValue) -> usize;
260        fn MapValue_new_iterator<'a, 'this>(
261            map_value: &'this MapValue<'a>,
262            iterator: &mut UniquePtr<ValueIterator<'this>>,
263        ) -> Status;
264
265        // MapValueBuilder
266        fn MapValueBuilder_new<'a>(arena: &'a Arena) -> UniquePtr<MapValueBuilder<'a>>;
267        fn MapValueBuilder_put<'a>(
268            builder: Pin<&mut MapValueBuilder<'a>>,
269            key: &Value<'a>,
270            value: &Value<'a>,
271        ) -> Status;
272        fn MapValueBuilder_build<'a>(
273            builder: Pin<&mut MapValueBuilder<'a>>,
274        ) -> UniquePtr<MapValue<'a>>;
275
276        // MessageValue
277        // todo: implement
278
279        // NullValue
280        fn NullValue_new() -> UniquePtr<NullValue>;
281
282        // OpaqueValue
283        fn OpaqueValue_new<'a>(
284            arena: &'a Arena,
285            descriptor_pool: &'a DescriptorPool,
286            ffi: Box<AnyFfiOpaqueValue<'a>>,
287        ) -> UniquePtr<OpaqueValue<'a>>;
288        fn OpaqueValue_get_optional<'a>(
289            opaque_value: &OpaqueValue<'a>,
290        ) -> UniquePtr<OptionalValue<'a>>;
291        fn OpaqueValue_get_ffi<'a>(opaque_value: &OpaqueValue<'a>) -> Box<AnyFfiOpaqueValue<'a>>;
292
293        // OptionalValue
294        fn OptionalValue_new<'a>(
295            arena: &'a Arena,
296            value: &Value<'a>,
297        ) -> UniquePtr<OptionalValue<'a>>;
298        fn OptionalValue_none() -> UniquePtr<OptionalValue<'static>>;
299        fn OptionalValue_get_value<'a>(optional_value: &OptionalValue<'a>) -> UniquePtr<Value<'a>>;
300
301        // StringValue
302        fn StringValue_new<'a>(arena: &'a Arena, value: &str) -> UniquePtr<StringValue<'a>>;
303        fn StringValue_native_value(string_value: &StringValue) -> String;
304
305        // TimestampValue
306        fn TimestampValue_new(value: Time) -> UniquePtr<TimestampValue>;
307
308        // TypeValue
309        fn TypeValue_new<'a>(value: &Type<'a>) -> UniquePtr<TypeValue<'a>>;
310
311        // UintValue
312        fn UintValue_new(value: u64) -> UniquePtr<UintValue>;
313
314        // ValueIterator
315        fn ValueIterator_next1<'a, 'b>(
316            value_iterator: Pin<&mut ValueIterator<'a>>,
317            descriptor_pool: &DescriptorPool,
318            message_factory: &MessageFactory,
319            arena: &'b Arena,
320            result: &mut UniquePtr<Value<'b>>,
321        ) -> Status;
322        fn ValueIterator_next2<'a, 'b>(
323            value_iterator: Pin<&mut ValueIterator<'a>>,
324            descriptor_pool: &DescriptorPool,
325            message_factory: &MessageFactory,
326            arena: &'b Arena,
327            key: &mut UniquePtr<Value<'b>>,
328            value: &mut UniquePtr<Value<'b>>,
329        ) -> Status;
330    }
331}
332
333// Value
334pub use ffi::Value;
335unsafe impl<'a> Send for Value<'a> {}
336unsafe impl<'a> Sync for Value<'a> {}
337
338impl<'a> crate::SizedExternType for Value<'a> {
339    fn size_of() -> usize {
340        ffi::Value_size_of()
341    }
342}
343
344impl<'a> SpanElement for Value<'a> {
345    type TypeId = cxx::type_id!("rust::cel_cxx::Span_Value");
346}
347
348impl<'a> Value<'a> {
349    pub fn swap(self: Pin<&mut Self>, other: Pin<&mut Self>) {
350        ffi::Value_swap(self, other);
351    }
352
353    pub fn new_bool(value: &BoolValue) -> cxx::UniquePtr<Self> {
354        ffi::Value_new_bool(value)
355    }
356
357    pub fn new_bytes(bytes: &BytesValue<'a>) -> cxx::UniquePtr<Self> {
358        ffi::Value_new_bytes(bytes)
359    }
360
361    pub fn new_double(value: &DoubleValue) -> cxx::UniquePtr<Self> {
362        ffi::Value_new_double(value)
363    }
364
365    pub fn new_duration(value: &DurationValue) -> cxx::UniquePtr<Self> {
366        ffi::Value_new_duration(value)
367    }
368
369    pub fn new_error(value: &ErrorValue<'a>) -> cxx::UniquePtr<Self> {
370        ffi::Value_new_error(value)
371    }
372
373    pub fn new_int(value: &IntValue) -> cxx::UniquePtr<Self> {
374        ffi::Value_new_int(value)
375    }
376
377    pub fn new_list(list: &ListValue<'a>) -> cxx::UniquePtr<Self> {
378        ffi::Value_new_list(list)
379    }
380
381    pub fn new_map(map: &MapValue<'a>) -> cxx::UniquePtr<Self> {
382        ffi::Value_new_map(map)
383    }
384
385    pub fn new_message(message: &MessageValue<'a>) -> cxx::UniquePtr<Self> {
386        ffi::Value_new_message(message)
387    }
388
389    pub fn new_null() -> cxx::UniquePtr<Self> {
390        ffi::Value_new_null()
391    }
392
393    pub fn new_opaque(opaque: &OpaqueValue<'a>) -> cxx::UniquePtr<Self> {
394        ffi::Value_new_opaque(opaque)
395    }
396
397    pub fn new_optional(optional: &OptionalValue<'a>) -> cxx::UniquePtr<Self> {
398        ffi::Value_new_optional(optional)
399    }
400
401    pub fn new_string(string: &StringValue<'a>) -> cxx::UniquePtr<Self> {
402        ffi::Value_new_string(string)
403    }
404
405    pub fn new_timestamp(timestamp: &TimestampValue) -> cxx::UniquePtr<Self> {
406        ffi::Value_new_timestamp(timestamp)
407    }
408
409    pub fn new_type(type_value: &TypeValue<'a>) -> cxx::UniquePtr<Self> {
410        ffi::Value_new_type(type_value)
411    }
412
413    pub fn new_uint(uint: &UintValue) -> cxx::UniquePtr<Self> {
414        ffi::Value_new_uint(uint)
415    }
416
417    pub fn get_bool(&self) -> cxx::UniquePtr<BoolValue> {
418        ffi::Value_get_bool(self)
419    }
420
421    pub fn get_bytes(&self) -> cxx::UniquePtr<BytesValue<'a>> {
422        ffi::Value_get_bytes(self)
423    }
424
425    pub fn get_double(&self) -> cxx::UniquePtr<DoubleValue> {
426        ffi::Value_get_double(self)
427    }
428
429    pub fn get_duration(&self) -> cxx::UniquePtr<DurationValue> {
430        ffi::Value_get_duration(self)
431    }
432
433    pub fn get_error(&self) -> cxx::UniquePtr<ErrorValue<'a>> {
434        ffi::Value_get_error(self)
435    }
436
437    pub fn get_int(&self) -> cxx::UniquePtr<IntValue> {
438        ffi::Value_get_int(self)
439    }
440
441    pub fn get_list(&self) -> cxx::UniquePtr<ListValue<'a>> {
442        ffi::Value_get_list(self)
443    }
444
445    pub fn get_map(&self) -> cxx::UniquePtr<MapValue<'a>> {
446        ffi::Value_get_map(self)
447    }
448
449    pub fn get_message(&self) -> cxx::UniquePtr<MessageValue<'a>> {
450        ffi::Value_get_message(self)
451    }
452
453    pub fn get_null(&self) -> cxx::UniquePtr<NullValue> {
454        ffi::Value_get_null(self)
455    }
456
457    pub fn get_opaque(&self) -> cxx::UniquePtr<OpaqueValue<'a>> {
458        ffi::Value_get_opaque(self)
459    }
460
461    pub fn get_optional(&self) -> cxx::UniquePtr<OptionalValue<'a>> {
462        ffi::Value_get_optional(self)
463    }
464
465    pub fn get_string(&self) -> cxx::UniquePtr<StringValue<'a>> {
466        ffi::Value_get_string(self)
467    }
468
469    pub fn get_timestamp(&self) -> cxx::UniquePtr<TimestampValue> {
470        ffi::Value_get_timestamp(self)
471    }
472
473    pub fn get_type(&self) -> cxx::UniquePtr<TypeValue<'a>> {
474        ffi::Value_get_type(self)
475    }
476
477    pub fn get_uint(&self) -> cxx::UniquePtr<UintValue> {
478        ffi::Value_get_uint(self)
479    }
480}
481
482// BoolValue
483pub use ffi::BoolValue;
484unsafe impl Send for BoolValue {}
485unsafe impl Sync for BoolValue {}
486
487impl BoolValue {
488    pub fn new(value: bool) -> cxx::UniquePtr<Self> {
489        ffi::BoolValue_new(value)
490    }
491}
492
493// BytesValue
494pub use ffi::BytesValue;
495unsafe impl<'a> Send for BytesValue<'a> {}
496unsafe impl<'a> Sync for BytesValue<'a> {}
497
498impl<'a> BytesValue<'a> {
499    pub fn new(arena: &'a Arena, bytes: &[u8]) -> cxx::UniquePtr<Self> {
500        ffi::BytesValue_new(arena, bytes)
501    }
502
503    pub fn native_value(&self) -> Vec<u8> {
504        ffi::BytesValue_native_value(self)
505    }
506}
507
508// DoubleValue
509pub use ffi::DoubleValue;
510unsafe impl Send for DoubleValue {}
511unsafe impl Sync for DoubleValue {}
512
513impl DoubleValue {
514    pub fn new(value: f64) -> cxx::UniquePtr<Self> {
515        ffi::DoubleValue_new(value)
516    }
517}
518
519// DurationValue
520pub use ffi::DurationValue;
521unsafe impl Send for DurationValue {}
522unsafe impl Sync for DurationValue {}
523
524impl DurationValue {
525    pub fn new(value: Duration) -> cxx::UniquePtr<Self> {
526        ffi::DurationValue_new(value)
527    }
528}
529
530// ErrorValue
531pub use ffi::ErrorValue;
532unsafe impl<'a> Send for ErrorValue<'a> {}
533unsafe impl<'a> Sync for ErrorValue<'a> {}
534
535impl<'a> ErrorValue<'a> {
536    pub fn new(status: Status) -> cxx::UniquePtr<Self> {
537        ffi::ErrorValue_new(status)
538    }
539
540    pub fn native_value(&self) -> Status {
541        ffi::ErrorValue_native_value(self)
542    }
543}
544
545// IntValue
546pub use ffi::IntValue;
547unsafe impl Send for IntValue {}
548unsafe impl Sync for IntValue {}
549
550impl IntValue {
551    pub fn new(value: i64) -> cxx::UniquePtr<Self> {
552        ffi::IntValue_new(value)
553    }
554}
555
556// ListValue
557pub use ffi::ListValue;
558unsafe impl<'a> Send for ListValue<'a> {}
559unsafe impl<'a> Sync for ListValue<'a> {}
560
561impl<'a> ListValue<'a> {
562    pub fn is_empty(&self) -> bool {
563        ffi::ListValue_is_empty(self)
564    }
565
566    pub fn len(&self) -> usize {
567        ffi::ListValue_size(self)
568    }
569
570    pub fn new_iterator<'this>(
571        &'this self,
572    ) -> Result<cxx::UniquePtr<ffi::ValueIterator<'this>>, Status> {
573        let mut result = cxx::UniquePtr::null();
574        let status = ffi::ListValue_new_iterator(self, &mut result);
575        if status.is_ok() {
576            Ok(result)
577        } else {
578            Err(status)
579        }
580    }
581}
582
583// ListValueBuilder
584pub use ffi::ListValueBuilder;
585unsafe impl<'a> Send for ListValueBuilder<'a> {}
586unsafe impl<'a> Sync for ListValueBuilder<'a> {}
587
588impl<'a> ListValueBuilder<'a> {
589    pub fn new(arena: &'a Arena) -> cxx::UniquePtr<Self> {
590        ffi::ListValueBuilder_new(arena)
591    }
592
593    pub fn add(self: Pin<&mut Self>, value: &Value<'a>) -> Status {
594        ffi::ListValueBuilder_add(self, value)
595    }
596
597    pub fn build(self: Pin<&mut Self>) -> cxx::UniquePtr<ListValue<'a>> {
598        ffi::ListValueBuilder_build(self)
599    }
600}
601
602// MapValue
603pub use ffi::MapValue;
604unsafe impl<'a> Send for MapValue<'a> {}
605unsafe impl<'a> Sync for MapValue<'a> {}
606
607impl<'a> MapValue<'a> {
608    pub fn is_empty(&self) -> bool {
609        ffi::MapValue_is_empty(self)
610    }
611
612    pub fn len(&self) -> usize {
613        ffi::MapValue_size(self)
614    }
615
616    pub fn new_iterator<'this>(
617        &'this self,
618    ) -> Result<cxx::UniquePtr<ffi::ValueIterator<'this>>, Status> {
619        let mut result = cxx::UniquePtr::null();
620        let status = ffi::MapValue_new_iterator(self, &mut result);
621        if status.is_ok() {
622            Ok(result)
623        } else {
624            Err(status)
625        }
626    }
627}
628
629// MapValueBuilder
630pub use ffi::MapValueBuilder;
631unsafe impl<'a> Send for MapValueBuilder<'a> {}
632unsafe impl<'a> Sync for MapValueBuilder<'a> {}
633
634impl<'a> MapValueBuilder<'a> {
635    pub fn new(arena: &'a Arena) -> cxx::UniquePtr<Self> {
636        ffi::MapValueBuilder_new(arena)
637    }
638
639    pub fn put(self: Pin<&mut Self>, key: &Value<'a>, value: &Value<'a>) -> Status {
640        ffi::MapValueBuilder_put(self, key, value)
641    }
642
643    pub fn build(self: Pin<&mut Self>) -> cxx::UniquePtr<MapValue<'a>> {
644        ffi::MapValueBuilder_build(self)
645    }
646}
647
648pub use ffi::MessageValue;
649unsafe impl<'a> Send for MessageValue<'a> {}
650unsafe impl<'a> Sync for MessageValue<'a> {}
651
652impl<'a> MessageValue<'a> {}
653
654pub use ffi::NullValue;
655unsafe impl Send for NullValue {}
656unsafe impl Sync for NullValue {}
657
658impl NullValue {
659    pub fn new() -> cxx::UniquePtr<Self> {
660        ffi::NullValue_new()
661    }
662}
663
664pub use ffi::OpaqueValue;
665unsafe impl<'a> Send for OpaqueValue<'a> {}
666unsafe impl<'a> Sync for OpaqueValue<'a> {}
667
668impl<'a> OpaqueValue<'a> {
669    pub fn new<T: FfiOpaqueValue + 'a>(
670        arena: &'a Arena,
671        descriptor_pool: &'a DescriptorPool,
672        ffi: T,
673    ) -> cxx::UniquePtr<Self> {
674        ffi::OpaqueValue_new(
675            arena,
676            descriptor_pool,
677            Box::new(AnyFfiOpaqueValue::new(ffi, arena, descriptor_pool)),
678        )
679    }
680
681    pub fn get_optional(&self) -> cxx::UniquePtr<OptionalValue<'a>> {
682        ffi::OpaqueValue_get_optional(self)
683    }
684
685    pub fn downcast<T: FfiOpaqueValue>(&self) -> Option<Box<T>> {
686        let ffi = ffi::OpaqueValue_get_ffi(self);
687        ffi.downcast::<T>()
688    }
689}
690
691pub trait FfiOpaqueValue:
692    'static + std::fmt::Debug + std::fmt::Display + dyn_clone::DynClone + private::Sealed + Send + Sync
693{
694    fn opaque_type<'a>(
695        &self,
696        arena: &'a Arena,
697        descriptor_pool: &'a DescriptorPool,
698    ) -> OpaqueType<'a>;
699}
700dyn_clone::clone_trait_object!(FfiOpaqueValue);
701
702#[derive(Clone)]
703struct AnyFfiOpaqueValue<'a> {
704    ffi: Box<dyn FfiOpaqueValue>,
705    opaque_type: OpaqueType<'a>,
706}
707
708impl<'a> AnyFfiOpaqueValue<'a> {
709    pub fn new<T: FfiOpaqueValue + 'static>(
710        value: T,
711        arena: &'a Arena,
712        descriptor_pool: &'a DescriptorPool,
713    ) -> Self {
714        let opaque_type = value.opaque_type(arena, descriptor_pool);
715        Self {
716            ffi: Box::new(value),
717            opaque_type,
718        }
719    }
720
721    pub fn clone(&self) -> Box<AnyFfiOpaqueValue<'a>> {
722        Box::new(Clone::clone(self))
723    }
724
725    pub fn type_name(&self) -> StringView<'a> {
726        self.opaque_type.name()
727    }
728
729    pub fn runtime_type(&self) -> OpaqueType<'a> {
730        self.opaque_type
731    }
732
733    pub fn equal<'b>(&self, other: &AnyFfiOpaqueValue<'b>) -> bool {
734        self == other
735    }
736
737    pub fn downcast<T: FfiOpaqueValue>(&self) -> Option<Box<T>> {
738        self.ffi.clone().into_any().downcast::<T>().ok()
739    }
740}
741
742impl<'a> std::fmt::Display for AnyFfiOpaqueValue<'a> {
743    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
744        write!(f, "AnyFfiOpaqueValue")
745    }
746}
747
748impl<'a> PartialEq for AnyFfiOpaqueValue<'a> {
749    fn eq(&self, other: &Self) -> bool {
750        *self.ffi == *other.ffi
751    }
752}
753
754impl<'a> Eq for AnyFfiOpaqueValue<'a> {}
755
756impl<'a> std::fmt::Debug for AnyFfiOpaqueValue<'a> {
757    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
758        write!(f, "AnyFfiOpaqueValue({:?})", self.ffi)
759    }
760}
761
762impl PartialEq for dyn FfiOpaqueValue {
763    fn eq(&self, other: &Self) -> bool {
764        self.dyn_eq(private::Sealed::as_any(other))
765    }
766}
767
768impl Eq for dyn FfiOpaqueValue {}
769
770mod private {
771    use super::FfiOpaqueValue;
772    use std::any::Any;
773
774    pub trait Sealed: Any {
775        fn into_any(self: Box<Self>) -> Box<dyn Any>;
776        fn as_any(&self) -> &dyn Any;
777        fn as_any_mut(&mut self) -> &mut dyn Any;
778
779        fn dyn_eq(&self, other: &dyn Any) -> bool;
780    }
781
782    impl<T: FfiOpaqueValue + PartialEq + Eq> Sealed for T {
783        fn into_any(self: Box<Self>) -> Box<dyn Any> {
784            self
785        }
786        fn as_any(&self) -> &dyn Any {
787            self
788        }
789        fn as_any_mut(&mut self) -> &mut dyn Any {
790            self
791        }
792
793        fn dyn_eq(&self, other: &dyn Any) -> bool {
794            other.downcast_ref::<T>() == Some(self)
795        }
796    }
797}
798
799pub use ffi::OptionalValue;
800unsafe impl<'a> Send for OptionalValue<'a> {}
801unsafe impl<'a> Sync for OptionalValue<'a> {}
802
803impl<'a> OptionalValue<'a> {
804    pub fn new(arena: &'a Arena, value: &Value<'a>) -> cxx::UniquePtr<Self> {
805        ffi::OptionalValue_new(arena, value)
806    }
807
808    pub fn none() -> cxx::UniquePtr<Self> {
809        ffi::OptionalValue_none()
810    }
811
812    pub fn get_value(&self) -> cxx::UniquePtr<Value<'a>> {
813        ffi::OptionalValue_get_value(self)
814    }
815}
816
817pub use ffi::StringValue;
818unsafe impl<'a> Send for StringValue<'a> {}
819unsafe impl<'a> Sync for StringValue<'a> {}
820
821impl<'a> StringValue<'a> {
822    pub fn new(arena: &'a Arena, value: &str) -> cxx::UniquePtr<Self> {
823        ffi::StringValue_new(arena, value)
824    }
825
826    pub fn native_value(&self) -> String {
827        ffi::StringValue_native_value(self)
828    }
829}
830
831pub use ffi::TimestampValue;
832unsafe impl Send for TimestampValue {}
833unsafe impl Sync for TimestampValue {}
834
835impl TimestampValue {
836    pub fn new(value: Timestamp) -> cxx::UniquePtr<Self> {
837        ffi::TimestampValue_new(value)
838    }
839}
840
841pub use ffi::TypeValue;
842unsafe impl<'a> Send for TypeValue<'a> {}
843unsafe impl<'a> Sync for TypeValue<'a> {}
844
845impl<'a> TypeValue<'a> {
846    pub fn new(value: &Type<'a>) -> cxx::UniquePtr<Self> {
847        ffi::TypeValue_new(value)
848    }
849}
850
851pub use ffi::UintValue;
852unsafe impl Send for UintValue {}
853unsafe impl Sync for UintValue {}
854
855impl UintValue {
856    pub fn new(value: u64) -> cxx::UniquePtr<Self> {
857        ffi::UintValue_new(value)
858    }
859}
860
861pub use ffi::ValueIterator;
862unsafe impl<'a> Send for ValueIterator<'a> {}
863unsafe impl<'a> Sync for ValueIterator<'a> {}
864
865impl<'a> ValueIterator<'a> {
866    pub fn next1<'b>(
867        self: Pin<&mut Self>,
868        descriptor_pool: &DescriptorPool,
869        message_factory: &MessageFactory,
870        arena: &'b Arena,
871    ) -> Result<cxx::UniquePtr<Value<'b>>, Status> {
872        let mut result = cxx::UniquePtr::null();
873        let status =
874            ffi::ValueIterator_next1(self, descriptor_pool, message_factory, arena, &mut result);
875        if status.is_ok() {
876            Ok(result)
877        } else {
878            Err(status)
879        }
880    }
881
882    pub fn next2<'b>(
883        self: Pin<&mut Self>,
884        descriptor_pool: &DescriptorPool,
885        message_factory: &MessageFactory,
886        arena: &'b Arena,
887    ) -> Result<(cxx::UniquePtr<Value<'b>>, cxx::UniquePtr<Value<'b>>), Status> {
888        let mut key = cxx::UniquePtr::null();
889        let mut value = cxx::UniquePtr::null();
890        let status = ffi::ValueIterator_next2(
891            self,
892            descriptor_pool,
893            message_factory,
894            arena,
895            &mut key,
896            &mut value,
897        );
898        if status.is_ok() {
899            Ok((key, value))
900        } else {
901            Err(status)
902        }
903    }
904}