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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Function overload management and resolution.
//!
//! This module provides types and utilities for managing function overloads,
//! allowing multiple function implementations with different signatures to
//! be registered under the same name.
//!
//! # Key Types
//!
//! - [`FunctionOverloads`]: Container for multiple overloads of a function
//! - [`FunctionKindOverload`]: Overloads grouped by argument kinds  
//! - [`FunctionDeclOrImpl`]: Union type for declarations and implementations
//!
//! # Overload Resolution
//!
//! The system supports sophisticated overload resolution based on:
//! - **Argument count**: Number of parameters
//! - **Argument types**: CEL types of each parameter
//! - **Member vs global**: Whether the function is called as a method
//!
//! This enables natural function calls like:
//! ```text
//! // Different overloads of "format"
//! format("Hello")              // format(string) -> string
//! format("Hello %s", "World")  // format(string, string) -> string  
//! format(42)                   // format(int) -> string
//! ```

use super::*;
use crate::Kind;

/// Collection of function overloads grouped by argument kinds.
///
/// `FunctionOverloads<T>` manages multiple function overloads that share the same name
/// but have different argument signatures. This allows CEL to support function overloading
/// based on argument types, similar to many programming languages.
///
/// # Type Parameters
///
/// - `T`: The type of function stored (implementation, declaration, or either)
///
/// # Examples
///
/// ```rust,no_run
/// use cel_cxx::function::{FunctionOverloads, FunctionDeclOrImpl};
///
/// let mut overloads = FunctionOverloads::<FunctionDeclOrImpl>::new();
/// // Add different overloads for the same function name
/// ```
#[derive(Debug, Default, Clone)]
pub struct FunctionOverloads<T>(Vec<FunctionKindOverload<T>>);

impl<T: FunctionTypeOverload> FunctionOverloads<T> {
    /// Creates a new empty function overload collection.
    pub fn new() -> Self {
        Self(Vec::new())
    }

    /// Finds a function overload by member type and argument kinds.
    ///
    /// # Parameters
    ///
    /// - `member`: Whether to find member functions (true) or global functions (false)
    /// - `kinds`: Argument kinds to match
    ///
    /// # Returns
    ///
    /// Returns `Some(&FunctionKindOverload)` if found, `None` otherwise
    pub fn find(&self, member: bool, kinds: &[Kind]) -> Option<&FunctionKindOverload<T>> {
        self.0
            .iter()
            .find(|overload| overload.member() == member && overload.argument_kinds() == kinds)
    }

    /// Finds a mutable reference to a function overload by member flag and argument kinds.
    ///
    /// # Parameters
    ///
    /// - `member`: Whether to search for member functions
    /// - `kinds`: Argument kinds to match
    ///
    /// # Returns
    ///
    /// Mutable reference to the matching overload, or `None` if not found
    pub fn find_mut(&mut self, member: bool, kinds: &[Kind]) -> Option<&mut FunctionKindOverload<T>>
    where
        T: Send + Sync,
    {
        self.0
            .iter_mut()
            .find(|overload| overload.member() == member && overload.argument_kinds() == kinds)
    }

    /// Returns an iterator over all function overloads.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use cel_cxx::function::{FunctionOverloads, FunctionDeclOrImpl};
    ///
    /// let overloads = FunctionOverloads::<FunctionDeclOrImpl<'_>>::new();
    /// for overload in overloads.entries() {
    ///     // Process each overload
    /// }
    /// ```
    pub fn entries(&self) -> impl Iterator<Item = &FunctionKindOverload<T>> {
        self.0.iter()
    }

    /// Returns a mutable iterator over all function overloads.
    pub fn entries_mut(&mut self) -> impl Iterator<Item = &mut FunctionKindOverload<T>> {
        self.0.iter_mut()
    }

    /// Clears all function overloads.
    pub fn clear(&mut self) {
        self.0.clear()
    }

    /// Removes a function overload by member flag and argument kinds.
    ///
    /// # Parameters
    ///
    /// - `member`: Whether to remove member functions
    /// - `args`: Argument kinds to match for removal
    ///
    /// # Returns
    ///
    /// `Ok(())` if removal was successful, `Err(Error)` if not found
    pub fn remove<I, K>(&mut self, member: bool, args: I) -> Result<(), Error>
    where
        I: IntoIterator<Item = K>,
        K: Into<Kind>,
    {
        let kinds = args.into_iter().map(|k| k.into()).collect::<Vec<_>>();
        if let Some(index) = self
            .0
            .iter()
            .position(|overload| overload.member() == member && overload.argument_kinds() == &kinds)
        {
            self.0.remove(index);
        } else {
            return Err(Error::not_found(format!(
                "Overload [{}] ({}) not found",
                if member { "member" } else { "global" },
                kinds
                    .iter()
                    .map(|k| k.to_string())
                    .collect::<Vec<_>>()
                    .join(", "),
            )));
        }
        Ok(())
    }
}

impl<'f> FunctionOverloads<FunctionDeclOrImpl<'f>> {
    /// Adds a function implementation to the overloads.
    ///
    /// This method adds a concrete function implementation to the appropriate overload
    /// group based on its signature. If no matching overload exists, a new one is created.
    ///
    /// # Arguments
    ///
    /// * `member` - Whether this is a member function
    /// * `f` - The function implementation to add
    ///
    /// # Returns
    ///
    /// `Ok(&mut Self)` for method chaining, or `Err(Error)` if addition fails
    ///
    /// # Errors
    ///
    /// Returns error if the function signature conflicts with existing registrations.
    pub fn add_impl(&mut self, member: bool, f: Function<'f>) -> Result<&mut Self, Error> {
        if member && f.arguments_len() == 0 {
            return Err(Error::invalid_argument("Member functions cannot have zero arguments"));
        }
        let kinds = f
            .arguments()
            .into_iter()
            .map(|t| t.kind())
            .collect::<Vec<_>>();
        if let Some(overload) = self.find_mut(member, &kinds) {
            overload.add_impl(f)?;
        } else {
            let mut overload = FunctionKindOverload::new(member, kinds);
            overload.add_impl(f)?;
            self.0.push(overload);
        }
        Ok(self)
    }

    /// Adds a function declaration to the overloads.
    ///
    /// This method adds a function type signature (declaration) to the appropriate
    /// overload group. Declarations provide type information for compile-time checking
    /// without requiring an implementation.
    ///
    /// # Arguments
    ///
    /// * `member` - Whether this is a member function declaration
    /// * `f` - The function type signature to add
    ///
    /// # Returns
    ///
    /// `Ok(&mut Self)` for method chaining, or `Err(Error)` if addition fails
    ///
    /// # Errors
    ///
    /// Returns error if the function signature conflicts with existing registrations.
    pub fn add_decl(&mut self, member: bool, f: FunctionType) -> Result<&mut Self, Error> {
        if member && f.arguments().is_empty() {
            return Err(Error::invalid_argument("Member functions cannot have zero arguments"));
        }
        let kinds = f.arguments().iter().map(|t| t.kind()).collect::<Vec<_>>();
        if let Some(overload) = self.find_mut(member, &kinds) {
            overload.add_decl(f)?;
        } else {
            let mut overload = FunctionKindOverload::new(member, kinds);
            overload.add_decl(f)?;
            self.0.push(overload);
        }
        Ok(self)
    }
}

impl<'f> FunctionOverloads<Function<'f>> {
    /// Adds a function to the overloads.
    ///
    /// This method adds a function to the appropriate overload group based on its
    /// signature. This is used when working with pure function implementations
    /// without separate declarations.
    ///
    /// # Arguments
    ///
    /// * `member` - Whether this is a member function
    /// * `f` - The function to add
    ///
    /// # Returns
    ///
    /// `Ok(&mut Self)` for method chaining, or `Err(Error)` if addition fails
    ///
    /// # Errors
    ///
    /// Returns error if the function signature conflicts with existing registrations.
    pub fn add(&mut self, member: bool, f: Function<'f>) -> Result<&mut Self, Error> {
        let kinds = f
            .arguments()
            .into_iter()
            .map(|t| t.kind())
            .collect::<Vec<_>>();
        if let Some(overload) = self.find_mut(member, &kinds) {
            overload.add(f)?;
        } else {
            let mut overload = FunctionKindOverload::new(member, kinds);
            overload.add(f)?;
            self.0.push(overload);
        }
        Ok(self)
    }
}

/// Trait for extracting argument types from function-like objects.
///
/// This trait provides a unified interface for getting argument type information
/// from different kinds of function objects (implementations, declarations, etc.).
/// It is used internally by the overload resolution system.
///
/// # Implementation Note
///
/// This trait is automatically implemented for function types that can provide
/// argument type information. It should not be implemented manually.
pub trait FunctionTypeOverload {
    /// Returns the argument types for this function.
    ///
    /// # Returns
    ///
    /// A vector of [`ValueType`] representing the function's argument types.
    fn arguments(&self) -> Vec<ValueType>;
}

impl FunctionTypeOverload for FunctionDeclOrImpl<'_> {
    fn arguments(&self) -> Vec<ValueType> {
        self.decl().arguments().to_vec()
    }
}

impl FunctionTypeOverload for Function<'_> {
    fn arguments(&self) -> Vec<ValueType> {
        self.arguments()
    }
}

/// Function overload for a specific argument kind signature.
///
/// `FunctionKindOverload` groups function implementations and declarations that have
/// the same member flag and argument kinds. This allows for efficient lookup and
/// type checking during function resolution.
///
/// # Type Parameters
///
/// - `T`: The type of function stored (implementation or declaration)
///
/// # Examples
///
/// ```rust,no_run
/// use cel_cxx::{Kind, function::{FunctionKindOverload, Function}};
///
/// // Create overload for member function taking (string, int)
/// let overload = FunctionKindOverload::<Function<'_>>::new(
///     true,
///     vec![Kind::String, Kind::Int]
/// );
/// ```
#[derive(Debug, Clone)]
pub struct FunctionKindOverload<T> {
    member: bool,
    argument_kinds: Vec<Kind>,
    entries: Vec<T>,
}

impl<T: FunctionTypeOverload> FunctionKindOverload<T> {
    /// Creates a new function kind overload.
    ///
    /// # Parameters
    ///
    /// - `member`: Whether this overload is for member functions
    /// - `argument_kinds`: The argument kinds this overload handles
    ///
    /// # Returns
    ///
    /// New `FunctionKindOverload` instance
    pub fn new(member: bool, argument_kinds: Vec<Kind>) -> Self {
        Self {
            member,
            argument_kinds,
            entries: Vec::new(),
        }
    }

    /// Returns whether this overload is for member functions.
    ///
    /// # Returns
    ///
    /// `true` if this overload handles member functions, `false` for global functions
    pub fn member(&self) -> bool {
        self.member
    }

    /// Returns the argument kinds for this overload.
    ///
    /// # Returns
    ///
    /// Reference to the vector of argument kinds this overload handles
    pub fn argument_kinds(&self) -> &Vec<Kind> {
        &self.argument_kinds
    }

    /// Finds a function by exact argument types.
    ///
    /// # Parameters
    ///
    /// - `args`: Argument types to match
    ///
    /// # Returns
    ///
    /// Reference to the matching function, or `None` if not found
    pub fn find(&self, args: &[ValueType]) -> Option<&T> {
        self.entries.iter().find(|entry| entry.arguments() == args)
    }

    /// Returns an iterator over all functions in this overload.
    ///
    /// # Returns
    ///
    /// Iterator over references to all functions in this overload
    pub fn entries(&self) -> impl Iterator<Item = &T> {
        self.entries.iter()
    }

    /// Returns a mutable iterator over all functions in this overload.
    ///
    /// # Returns
    ///
    /// Iterator over mutable references to all functions in this overload
    pub fn entries_mut(&mut self) -> impl Iterator<Item = &mut T> {
        self.entries.iter_mut()
    }

    /// Clears all functions from this overload.
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// Removes a function by exact argument types.
    ///
    /// # Parameters
    ///
    /// - `args`: Argument types to match for removal
    ///
    /// # Returns
    ///
    /// `Ok(())` if removal was successful, `Err(Error)` if not found
    pub fn remove(&mut self, args: &[ValueType]) -> Result<(), Error> {
        if let Some(index) = self
            .entries
            .iter()
            .position(|entry| entry.arguments() == args)
        {
            self.entries.remove(index);
        } else {
            return Err(Error::not_found(format!(
                "Overload [{}] ({}) not found",
                if self.member { "member" } else { "global" },
                args.iter()
                    .map(|t| t.to_string())
                    .collect::<Vec<_>>()
                    .join(", "),
            )));
        }
        Ok(())
    }
}

impl<'f> FunctionKindOverload<FunctionDeclOrImpl<'f>> {
    /// Adds a function implementation to this overload group.
    ///
    /// This method adds a concrete function implementation to the overload group.
    /// The function signature must match the argument kinds of this overload group.
    ///
    /// # Arguments
    ///
    /// * `f` - The function implementation to add
    ///
    /// # Returns
    ///
    /// `Ok(&mut Self)` for method chaining, or `Err(Error)` if the function already exists
    ///
    /// # Errors
    ///
    /// Returns an error if a function with the same exact signature already exists.
    pub fn add_impl(&mut self, f: Function<'f>) -> Result<&mut Self, Error> {
        if let Some(_entry) = self.find(&f.arguments()) {
            return Err(Error::invalid_argument("Function already exists"));
        }
        self.entries.push(FunctionDeclOrImpl::new_impl(f));
        Ok(self)
    }

    /// Adds a function declaration to this overload group.
    ///
    /// This method adds a function type signature (declaration) to the overload group.
    /// The function signature must match the argument kinds of this overload group.
    ///
    /// # Arguments
    ///
    /// * `r#type` - The function type signature to add
    ///
    /// # Returns
    ///
    /// `Ok(&mut Self)` for method chaining, or `Err(Error)` if the function already exists
    ///
    /// # Errors
    ///
    /// Returns an error if a function with the same exact signature already exists.
    pub fn add_decl(&mut self, r#type: FunctionType) -> Result<&mut Self, Error> {
        if let Some(_entry) = self.find(r#type.arguments()) {
            return Err(Error::invalid_argument("Function already exists"));
        }
        self.entries.push(FunctionDeclOrImpl::new(r#type));
        Ok(self)
    }
}

impl<'f> FunctionKindOverload<Function<'f>> {
    /// Adds a function (implementation or declaration) to this overload.
    ///
    /// This is a convenience method that automatically determines whether to add
    /// an implementation or declaration based on the function type.
    ///
    /// # Type Parameters
    ///
    /// - `F`: Function type
    /// - `M`: Function marker (sync/async)
    /// - `E`: Error type
    /// - `R`: Return type
    /// - `A`: Argument tuple type
    ///
    /// # Parameters
    ///
    /// - `f`: Function to add
    ///
    /// # Returns
    ///
    /// Mutable reference to self for chaining, or error if addition failed
    pub fn add(&mut self, f: Function<'f>) -> Result<&mut Self, Error> {
        if let Some(_entry) = self.find(&f.arguments()) {
            return Err(Error::invalid_argument("Function already exists"));
        }
        self.entries.push(f);
        Ok(self)
    }
}

/// Union type representing either a function declaration or implementation.
///
/// `FunctionDeclOrImpl` can hold either:
/// - A function type signature (declaration) for compile-time type checking
/// - A concrete function implementation for runtime execution
/// - Both a declaration and implementation (preferred)
///
/// This allows the system to provide type information even when implementations
/// are not available, enabling better compile-time checking and error reporting.
///
/// # Examples
///
/// ```rust,no_run
/// use cel_cxx::function::{FunctionDeclOrImpl, IntoFunction};
/// use cel_cxx::types::{ValueType, FunctionType};
///
/// // Create from declaration only
/// let func_type = FunctionType::new(ValueType::Int, vec![ValueType::Int]);
/// let decl_only = FunctionDeclOrImpl::new(func_type);
///
/// // Create from implementation (includes both decl and impl)
/// let func = (|a: i32, b: i32| -> i32 { a + b }).into_function();
/// let with_impl = FunctionDeclOrImpl::new_impl(func);
/// ```
#[derive(Debug, Clone)]
pub struct FunctionDeclOrImpl<'f> {
    r#type: FunctionType,
    r#impl: Option<Function<'f>>,
}

impl<'f> FunctionDeclOrImpl<'f> {
    /// Creates a new `FunctionDeclOrImpl` from a function implementation.
    ///
    /// This constructor creates both the declaration (extracted from the function's
    /// type signature) and stores the implementation for runtime execution.
    ///
    /// # Arguments
    ///
    /// * `r#impl` - The function implementation
    ///
    /// # Returns
    ///
    /// New `FunctionDeclOrImpl` containing both declaration and implementation
    pub fn new_impl(r#impl: Function<'f>) -> Self {
        Self {
            r#type: r#impl.function_type(),
            r#impl: Some(r#impl),
        }
    }

    /// Creates a new `FunctionDeclOrImpl` from a function type declaration.
    ///
    /// This constructor creates a declaration-only entry, useful for providing
    /// type information without requiring an implementation.
    ///
    /// # Arguments
    ///
    /// * `r#type` - The function type signature
    ///
    /// # Returns
    ///
    /// New `FunctionDeclOrImpl` containing only the declaration
    pub fn new(r#type: FunctionType) -> Self {
        Self {
            r#type,
            r#impl: None,
        }
    }

    /// Returns whether this entry has a concrete implementation.
    ///
    /// # Returns
    ///
    /// `true` if this entry contains a function implementation, `false` if declaration-only
    pub fn is_impl(&self) -> bool {
        self.r#impl.is_some()
    }

    /// Gets the function type declaration.
    ///
    /// # Returns
    ///
    /// Reference to the function type signature
    pub fn decl(&self) -> &FunctionType {
        &self.r#type
    }

    /// Gets the function implementation, if available.
    ///
    /// # Returns
    ///
    /// `Some(&Function)` if implementation is available, `None` for declaration-only entries
    pub fn r#impl(&self) -> Option<&Function<'f>> {
        self.r#impl.as_ref()
    }
}