cel-cxx 0.2.5

A high-performance, type-safe Rust interface for Common Expression Language (CEL), build on top of cel-cpp with zero-cost FFI bindings via cxx
Documentation
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use super::*;
use crate::function::FunctionRegistry;
use crate::variable::VariableRegistry;
use crate::{ffi, Program, ProgramInner, ValueType};
use ouroboros::self_referencing;

#[self_referencing]
#[derive(Debug)]
pub struct EnvInner<'f> {
    macros: Arc<HashSet<Macro>>,
    function_registry: Arc<FunctionRegistry<'f>>,
    variable_registry: Arc<VariableRegistry>,

    ffi_ctx: ffi::Ctx,

    #[borrows(ffi_ctx)]
    #[covariant]
    ffi_compiler: cxx::UniquePtr<ffi::Compiler<'this>>,

    #[borrows(ffi_ctx)]
    #[covariant]
    ffi_runtime: cxx::UniquePtr<ffi::Runtime<'this, 'static>>,
}

#[derive(Debug)]
pub struct EnvInnerOptions {
    pub container: String,
    pub file_descriptor_set: Option<Vec<u8>>,
    pub enable_standard: bool,
    pub enable_optional: bool,
    pub enable_ext_bindings: bool,
    pub enable_ext_comprehensions: bool,
    pub enable_ext_encoders: bool,
    pub enable_ext_lists: bool,
    pub enable_ext_math: bool,
    pub enable_ext_proto: bool,
    pub enable_ext_regex: bool,
    pub enable_ext_re: bool,
    pub enable_ext_sets: bool,
    pub enable_ext_strings: bool,
    pub enable_ext_select_optimization: bool,
}

impl Default for EnvInnerOptions {
    fn default() -> Self {
        Self {
            container: "".to_string(),
            file_descriptor_set: None,
            enable_standard: true,
            enable_optional: false,
            enable_ext_bindings: false,
            enable_ext_comprehensions: false,
            enable_ext_encoders: false,
            enable_ext_lists: false,
            enable_ext_math: false,
            enable_ext_proto: false,
            enable_ext_regex: false,
            enable_ext_re: false,
            enable_ext_sets: false,
            enable_ext_strings: false,
            enable_ext_select_optimization: false,
        }
    }
}

impl EnvInnerOptions {
    pub fn check_consistency(&mut self) {
        // If the regex extension is enabled, we need to enable the optional extension.
        if self.enable_ext_regex && !self.enable_optional {
            self.enable_optional = true;
        }
    }
}

impl<'f> EnvInner<'f> {
    pub fn new_with_registries(
        macros: HashSet<Macro>,
        function_registry: FunctionRegistry<'f>,
        variable_registry: VariableRegistry,
        mut env_options: EnvInnerOptions,
    ) -> Result<Self, ffi::Status> {
        ffi::absl::log::init();

        env_options.check_consistency();

        let macros = Arc::new(macros);
        let function_registry = Arc::new(function_registry);
        let variable_registry = Arc::new(variable_registry);

        EnvInnerTryBuilder {
            macros: macros.clone(),
            function_registry: function_registry.clone(),
            variable_registry: variable_registry.clone(),
            ffi_ctx: match &env_options.file_descriptor_set {
                Some(fds) => ffi::Ctx::new_dynamic(fds).map_err(|e| {
                    ffi::Status::invalid_argument(e)
                })?,
                None => ffi::Ctx::new_generated(),
            },
            ffi_compiler_builder: |ffi_ctx: &ffi::Ctx| {
                let options = ffi::CompilerOptions::new();
                let mut builder =
                    ffi::CompilerBuilder::new(ffi_ctx.shared_descriptor_pool().clone(), &options)?;
                
                if !env_options.container.is_empty() {
                    builder
                        .pin_mut()
                        .checker_builder()
                        .set_container(ffi::StringView::new_str(&env_options.container));
                }

                if env_options.enable_standard {
                    builder
                        .pin_mut()
                        .add_library(ffi::compiler::CompilerLibrary::new_standard())?;
                }

                if env_options.enable_optional {
                    builder
                        .pin_mut()
                        .add_library(ffi::compiler::CompilerLibrary::new_optional())?;
                }

                if env_options.enable_ext_bindings {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::bindings::compiler_library())?;
                }

                if env_options.enable_ext_comprehensions {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::comprehensions::compiler_library())?;
                }

                if env_options.enable_ext_encoders {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::encoders::compiler_library())?;
                }

                if env_options.enable_ext_lists {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::lists::compiler_library())?;
                }

                if env_options.enable_ext_math {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::math::compiler_library())?;
                }

                if env_options.enable_ext_proto {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::proto::compiler_library())?;
                }

                if env_options.enable_ext_regex {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::regex::compiler_library())?;
                }

                if env_options.enable_ext_re {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::re::compiler_library())?;
                }

                if env_options.enable_ext_sets {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::sets::compiler_library())?;
                }

                if env_options.enable_ext_strings {
                    builder
                        .pin_mut()
                        .add_library(ffi::extensions::strings::compiler_library())?;
                }

                for m in macros.iter() {
                    let ffi_macro = &*m.0;
                    let status = builder.pin_mut()
                        .parser_builder()
                        .add_macro(ffi_macro);
                    if !status.is_ok() {
                        return Err(status.into());
                    }
                }

                for (name, decl_or_constant) in variable_registry.entries() {
                    let ffi_variable_decl = match decl_or_constant.constant() {
                        Some(constant) => {
                            let ffi_constant: cxx::UniquePtr<ffi::Constant> = constant.into();
                            ffi::VariableDecl::new_constant(name, &ffi_constant)
                        }
                        None => {
                            let value_type = decl_or_constant.decl();
                            ffi::VariableDecl::new(
                                name,
                                &ffi::type_from_rust(
                                    value_type,
                                    ffi_ctx.arena(),
                                    ffi_ctx.descriptor_pool(),
                                ),
                            )
                        }
                    };
                    builder
                        .pin_mut()
                        .checker_builder()
                        .add_variable(&ffi_variable_decl);
                }

                for (name, overloads) in function_registry.entries() {
                    let mut ffi_function_decl = ffi::FunctionDecl::new(name);
                    for kind_overload in overloads.entries() {
                        let member = kind_overload.member();

                        for type_overload in kind_overload.entries() {
                            let id = type_overload.decl().id(name, member);
                            let ffi_result = ffi::type_from_rust(
                                type_overload.decl().result(),
                                ffi_ctx.arena(),
                                ffi_ctx.descriptor_pool(),
                            );
                            let ffi_arguments = type_overload
                                .decl()
                                .arguments()
                                .iter()
                                .map(|ty| {
                                    ffi::type_from_rust(
                                        ty,
                                        ffi_ctx.arena(),
                                        ffi_ctx.descriptor_pool(),
                                    )
                                })
                                .collect::<Vec<_>>();
                            let ffi_overload_decl =
                                ffi::OverloadDecl::new(&id, member, &ffi_result, &ffi_arguments);
                            ffi_function_decl.pin_mut().add_overload(&ffi_overload_decl);
                        }
                    }
                    builder
                        .pin_mut()
                        .checker_builder()
                        .merge_function(&ffi_function_decl);
                }

                let compiler = builder.pin_mut().build()?;
                Ok(compiler)
            },
            ffi_runtime_builder: |ffi_ctx: &ffi::Ctx| {
                let mut options = ffi::RuntimeOptions::new();

                if !env_options.container.is_empty() {
                    options
                        .pin_mut()
                        .container_mut()
                        .push_str(&env_options.container);
                }

                // Qualified type identifiers are required for:
                // - Optional values: enables optional.of(), optional.none(), etc.
                // - Custom descriptor pools: enables fully-qualified enum constant
                //   resolution (e.g. package.EnumType.ENUM_VALUE) via the runtime Resolver.
                if env_options.enable_optional || env_options.file_descriptor_set.is_some() {
                    *options.pin_mut().enable_qualified_type_identifiers_mut() = true;
                }

                let mut builder =
                    ffi::RuntimeBuilder::new(ffi_ctx.shared_descriptor_pool().clone(), &options)?;

                if env_options.enable_standard {
                    builder.pin_mut().enable_standard(&options)?;
                }
                if env_options.enable_optional {
                    builder.pin_mut().enable_optional(&options)?;
                }

                // Register enum constants from the custom descriptor pool so that
                // named enum references (e.g. test.Status.STATUS_ACTIVE) resolve
                // at plan time.
                if let Some(fds) = &env_options.file_descriptor_set {
                    builder
                        .pin_mut()
                        .type_registry()
                        .register_enums_from_file_descriptor_set(
                            ffi_ctx.descriptor_pool(),
                            fds,
                        )?;
                }

                if env_options.enable_ext_comprehensions {
                    ffi::extensions::comprehensions::register_functions(
                        builder.pin_mut().function_registry(),
                        &options,
                    )?;
                }

                if env_options.enable_ext_encoders {
                    ffi::extensions::encoders::register_functions(
                        builder.pin_mut().function_registry(),
                        &options,
                    )?;
                }

                if env_options.enable_ext_lists {
                    ffi::extensions::lists::register_functions(
                        builder.pin_mut().function_registry(),
                        &options,
                    )?;
                }

                if env_options.enable_ext_math {
                    ffi::extensions::math::register_functions(
                        builder.pin_mut().function_registry(),
                        &options,
                    )?;
                }

                if env_options.enable_ext_regex {
                    ffi::extensions::regex::register_functions(
                        builder.pin_mut()
                    )?;
                }

                if env_options.enable_ext_re {
                    ffi::extensions::re::register_functions(
                        builder.pin_mut().function_registry(),
                        &options,
                    )?;
                }

                if env_options.enable_ext_sets {
                    ffi::extensions::sets::register_functions(
                        builder.pin_mut().function_registry(),
                        &options,
                    )?;
                }

                if env_options.enable_ext_strings {
                    ffi::extensions::strings::register_functions(
                        builder.pin_mut().function_registry(),
                        &options,
                    )?;
                }

                if env_options.enable_ext_select_optimization {
                    let options = ffi::extensions::select_optimization::Options::new();
                    ffi::extensions::select_optimization::enable(
                        builder.pin_mut(),
                        &options,
                    )?;
                }

                for (name, overloads) in function_registry.entries() {
                    for kind_overload in overloads.entries() {
                        let receiver_style = kind_overload.member();
                        let ffi_types = kind_overload.argument_kinds();
                        let ffi_function_descriptor =
                            ffi::FunctionDescriptor::new(name, receiver_style, ffi_types, true);
                        builder
                            .pin_mut()
                            .function_registry()
                            .register_lazy(&ffi_function_descriptor);
                    }
                }

                for (_, decl) in variable_registry.entries() {
                    if let ValueType::Opaque(opaque) = decl.decl() {
                        let ffi_opaque_type = ffi::opaque_type_from_rust(
                            opaque,
                            ffi_ctx.arena(),
                            ffi_ctx.descriptor_pool(),
                        );
                        builder
                            .pin_mut()
                            .type_registry()
                            .register_type(&ffi_opaque_type);
                    }
                }

                let runtime = builder.pin_mut().build()?;
                Ok(runtime)
            },
        }
        .try_build()
    }

    pub fn function_registry<'this>(&'this self) -> &'this Arc<FunctionRegistry<'f>> {
        self.with_function_registry(|function_registry| function_registry)
    }

    #[allow(unused)]
    pub fn variable_registry(&self) -> &Arc<VariableRegistry> {
        self.with_variable_registry(|variable_registry| variable_registry)
    }

    pub fn ctx(&self) -> &ffi::Ctx {
        self.with_ffi_ctx(|ffi_ctx| ffi_ctx)
    }

    pub fn compiler<'this>(&'this self) -> &'this ffi::Compiler<'this> {
        self.with_ffi_compiler(|ffi_compiler| ffi_compiler)
    }

    pub fn runtime<'this>(&'this self) -> &'this ffi::Runtime<'this, 'static> {
        self.with_ffi_runtime(|ffi_runtime| ffi_runtime)
    }

    pub fn compile<Fm: FnMarker, Rm: RuntimeMarker, S: AsRef<[u8]>>(
        self: Arc<Self>,
        source: S,
    ) -> Result<Program<'f, Fm, Rm>, Error> {
        let inner = ProgramInner::new_from_env_source(self, source)?;
        Ok(Program {
            inner: Arc::new(inner),
            _fn_marker: std::marker::PhantomData,
            _rt_marker: std::marker::PhantomData,
        })
    }
}