cel_cxx_ffi/common/
constant.rs

1use crate::absl::{Duration, Timestamp};
2
3#[cxx::bridge]
4mod ffi {
5    #[namespace = "absl"]
6    unsafe extern "C++" {
7        include!(<absl/status/status.h>);
8        include!(<absl/time/time.h>);
9        type Time = super::Timestamp;
10        type Duration = super::Duration;
11    }
12
13    #[namespace = "cel"]
14    unsafe extern "C++" {
15        include!(<common/constant.h>);
16        type ConstantKindCase = super::ConstantKindCase;
17
18        type Constant;
19        fn kind_case(self: &Constant) -> ConstantKindCase;
20        fn has_value(self: &Constant) -> bool;
21        fn has_null_value(self: &Constant) -> bool;
22        fn has_bool_value(self: &Constant) -> bool;
23        fn has_int_value(self: &Constant) -> bool;
24        fn has_uint_value(self: &Constant) -> bool;
25        fn has_double_value(self: &Constant) -> bool;
26        fn has_bytes_value(self: &Constant) -> bool;
27        fn has_string_value(self: &Constant) -> bool;
28        fn has_duration_value(self: &Constant) -> bool;
29        fn has_timestamp_value(self: &Constant) -> bool;
30        fn bool_value(self: &Constant) -> bool;
31        fn int_value(self: &Constant) -> i64;
32        fn uint_value(self: &Constant) -> u64;
33        fn double_value(self: &Constant) -> f64;
34        fn bytes_value(self: &Constant) -> &CxxString;
35        fn string_value(self: &Constant) -> &CxxString;
36        fn duration_value(self: &Constant) -> Duration;
37        fn timestamp_value(self: &Constant) -> Time;
38    }
39
40    #[namespace = "rust::cel_cxx"]
41    unsafe extern "C++" {
42        include!(<cel-cxx-ffi/include/constant.h>);
43
44        // Constnat
45        fn Constant_new_null() -> UniquePtr<Constant>;
46        fn Constant_new_bool(value: bool) -> UniquePtr<Constant>;
47        fn Constant_new_int(value: i64) -> UniquePtr<Constant>;
48        fn Constant_new_uint(value: u64) -> UniquePtr<Constant>;
49        fn Constant_new_double(value: f64) -> UniquePtr<Constant>;
50        fn Constant_new_bytes(value: &[u8]) -> UniquePtr<Constant>;
51        fn Constant_new_string(value: &str) -> UniquePtr<Constant>;
52        fn Constant_new_duration(value: Duration) -> UniquePtr<Constant>;
53        fn Constant_new_timestamp(value: Time) -> UniquePtr<Constant>;
54    }
55}
56
57// Constant
58pub use ffi::Constant;
59unsafe impl Send for Constant {}
60unsafe impl Sync for Constant {}
61
62impl Constant {
63    pub fn new_null() -> cxx::UniquePtr<Self> {
64        ffi::Constant_new_null()
65    }
66
67    pub fn new_bool(value: bool) -> cxx::UniquePtr<Self> {
68        ffi::Constant_new_bool(value)
69    }
70
71    pub fn new_int(value: i64) -> cxx::UniquePtr<Self> {
72        ffi::Constant_new_int(value)
73    }
74
75    pub fn new_uint(value: u64) -> cxx::UniquePtr<Self> {
76        ffi::Constant_new_uint(value)
77    }
78
79    pub fn new_double(value: f64) -> cxx::UniquePtr<Self> {
80        ffi::Constant_new_double(value)
81    }
82
83    pub fn new_bytes(value: &[u8]) -> cxx::UniquePtr<Self> {
84        ffi::Constant_new_bytes(value)
85    }
86
87    pub fn new_string(value: &str) -> cxx::UniquePtr<Self> {
88        ffi::Constant_new_string(value)
89    }
90
91    pub fn new_duration(value: Duration) -> cxx::UniquePtr<Self> {
92        ffi::Constant_new_duration(value)
93    }
94
95    pub fn new_timestamp(value: Timestamp) -> cxx::UniquePtr<Self> {
96        ffi::Constant_new_timestamp(value)
97    }
98}
99
100#[repr(i32)]
101#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
102pub enum ConstantKindCase {
103    Unspecified = 0,
104    Null,
105    Bool,
106    Int,
107    Uint,
108    Double,
109    Bytes,
110    String,
111    Duration,
112    Timestamp,
113}
114
115unsafe impl cxx::ExternType for ConstantKindCase {
116    type Id = cxx::type_id!("cel::ConstantKindCase");
117    type Kind = cxx::kind::Trivial;
118}
119
120impl ConstantKindCase {
121    pub fn is_unspecified(&self) -> bool {
122        *self == ConstantKindCase::Unspecified
123    }
124
125    pub fn is_null(&self) -> bool {
126        *self == ConstantKindCase::Null
127    }
128
129    pub fn is_bool(&self) -> bool {
130        *self == ConstantKindCase::Bool
131    }
132
133    pub fn is_int(&self) -> bool {
134        *self == ConstantKindCase::Int
135    }
136
137    pub fn is_uint(&self) -> bool {
138        *self == ConstantKindCase::Uint
139    }
140
141    pub fn is_double(&self) -> bool {
142        *self == ConstantKindCase::Double
143    }
144    
145    pub fn is_bytes(&self) -> bool {
146        *self == ConstantKindCase::Bytes
147    }
148
149    pub fn is_string(&self) -> bool {
150        *self == ConstantKindCase::String
151    }
152    
153    pub fn is_duration(&self) -> bool {
154        *self == ConstantKindCase::Duration
155    }
156
157    pub fn is_timestamp(&self) -> bool {
158        *self == ConstantKindCase::Timestamp
159    }
160}