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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
use *;
use crateIntoError;
use Hash;
/// Trait for types that have a known CEL type.
///
/// This trait provides type information for values that can be used in CEL expressions.
/// It is used in conjunction with [`IntoValue`] and [`FromValue`] to provide complete type
/// information for value conversions.
///
/// # Implementation
///
/// **Important**: This trait should generally not be implemented manually. For most use cases:
/// - **Built-in types**: Already have implementations (primitives, collections, etc.)
/// - **Custom opaque types**: Use the `#[derive(Opaque)]` macro which automatically implements this trait
/// - **Special cases**: Only implement manually if you have very specific requirements
///
/// # Examples
///
/// ## Using Built-in Types
///
/// ```rust
/// use cel_cxx::{TypedValue, ValueType};
///
/// // Built-in types already implement TypedValue
/// assert_eq!(i64::value_type(), ValueType::Int);
/// assert_eq!(String::value_type(), ValueType::String);
/// assert_eq!(bool::value_type(), ValueType::Bool);
/// ```
///
/// ## Using Derive Macro for Custom Types
///
/// ```rust,no_run
/// use cel_cxx::Opaque;
///
/// #[derive(Opaque, Debug, Clone, PartialEq)]
/// #[cel_cxx(display)]
/// struct CustomId(u64);
///
/// // TypedValue is automatically implemented by the derive macro
/// ```
/// Trait for converting values into CEL values.
///
/// This trait enables automatic conversion from Rust types to CEL [`Value`] instances.
/// It is typically used alongside [`TypedValue`] to provide complete value conversion
/// functionality.
///
/// # Implementation
///
/// **Important**: This trait should generally not be implemented manually. For most use cases:
/// - **Built-in types**: Already have implementations
/// - **Custom opaque types**: Use the `#[derive(Opaque)]` macro
/// - **Collection types**: Automatically implemented for compatible element types
///
/// # Examples
///
/// ## Using Built-in Conversions
///
/// ```rust
/// use cel_cxx::{IntoValue, Value};
///
/// // Built-in types can be converted directly
/// let string_val = "hello".into_value();
/// let int_val = 42i64.into_value();
/// let bool_val = true.into_value();
///
/// assert_eq!(string_val, Value::String("hello".to_string().into()));
/// assert_eq!(int_val, Value::Int(42));
/// assert_eq!(bool_val, Value::Bool(true));
/// ```
///
/// ## Collection Conversions
///
/// ```rust
/// use cel_cxx::{IntoValue, Value};
///
/// let list = vec![1i64, 2i64, 3i64].into_value();
/// let map = std::collections::HashMap::from([
/// ("key".to_string(), "value".to_string())
/// ]).into_value();
/// ```
/// Trait for converting CEL values into Rust types.
///
/// This trait enables conversion from CEL [`Value`] instances back to Rust types,
/// with support for both owned and borrowed data through Generic Associated Types (GATs).
/// It is the inverse operation of [`IntoValue`].
///
/// # Generic Associated Types (GATs)
///
/// The `Output<'a>` associated type allows the trait to return either owned or borrowed
/// data depending on the source value:
/// - For `String`: `Output<'a> = String` (always owned)
/// - For `&str`: `Output<'a> = &'a str` (borrowed from the source Value)
/// - For primitives: `Output<'a> = Self` (copied)
///
/// # Implementation
///
/// **Important**: This trait should generally not be implemented manually. For most use cases:
/// - **Built-in types**: Already have implementations with proper lifetime handling
/// - **Custom opaque types**: Use the `#[derive(Opaque)]` macro
/// - **Manual implementation**: Only for very specific requirements and advanced use cases
///
/// # Examples
///
/// ## Using Built-in Conversions
///
/// ```rust
/// use cel_cxx::{FromValue, Value};
///
/// let cel_string = Value::String("hello".to_string().into());
/// let rust_string = String::from_value(&cel_string)?;
/// assert_eq!(rust_string, "hello");
///
/// let cel_int = Value::Int(42);
/// let rust_int = i64::from_value(&cel_int)?;
/// assert_eq!(rust_int, 42);
/// # Ok::<(), cel_cxx::Error>(())
/// ```
///
/// ## Zero-Copy String Conversion
///
/// ```rust
/// use cel_cxx::{FromValue, Value};
///
/// let cel_string = Value::String("hello world".to_string().into());
///
/// // Convert to borrowed string slice (zero-copy)
/// let borrowed_str = <&str>::from_value(&cel_string)?;
/// assert_eq!(borrowed_str, "hello world");
/// # Ok::<(), cel_cxx::Error>(())
/// ```
/// Error type for failed value conversions.
///
/// This error is returned when attempting to convert a CEL [`Value`] to a Rust type
/// using the [`FromValue`] trait, but the conversion fails due to type mismatch.
///
/// # Error Information
///
/// The error contains:
/// - **Source value**: The original CEL value that could not be converted
/// - **Target type**: String description of the intended conversion target
/// - **Context**: Additional information about why the conversion failed
///
/// # Common Causes
///
/// - **Type mismatch**: Trying to convert `Value::String` to `i64`
/// - **Invalid format**: Trying to convert malformed data
/// - **Constraint violation**: Value doesn't meet type constraints
///
/// # Examples
///
/// ```rust
/// use cel_cxx::{Value, FromValue, FromValueError};
///
/// let string_val = Value::String("not_a_number".to_string().into());
/// let result = i64::from_value(&string_val);
///
/// match result {
/// Ok(num) => println!("Converted: {}", num),
/// Err(error) => {
/// println!("Cannot convert value: {}", error);
/// }
/// }
/// ```
/// Trait for types that can be used as map keys and have a known CEL map key type.
///
/// `TypedMapKey` provides static type information for map key types.
/// This trait is used alongside [`IntoMapKey`] for complete map key functionality.
///
/// # Examples
///
/// ```rust,no_run
/// use cel_cxx::{TypedMapKey, MapKeyType};
///
/// // Built-in types implement this automatically
/// assert_eq!(String::mapkey_type(), MapKeyType::String);
/// assert_eq!(i64::mapkey_type(), MapKeyType::Int);
/// ```
/// Trait for converting values into CEL map keys.
///
/// `IntoMapKey` provides conversion from Rust types to CEL [`MapKey`] instances.
/// Only types that are valid CEL map key types can implement this trait.
///
/// # Examples
///
/// ```rust,no_run
/// use cel_cxx::{IntoMapKey, MapKey};
///
/// // Convert various types to map keys
/// let string_key = "key".into_mapkey();
/// let int_key = 42i64.into_mapkey();
/// let bool_key = true.into_mapkey();
///
/// assert_eq!(string_key, MapKey::String("key".to_string().into()));
/// assert_eq!(int_key, MapKey::Int(42));
/// assert_eq!(bool_key, MapKey::Bool(true));
/// ```
/// Trait for converting CEL map keys into Rust types.
///
/// `FromMapKey` provides conversion from CEL [`MapKey`] instances to Rust types.
/// This is the inverse operation of [`IntoMapKey`].
///
/// # Examples
///
/// ```rust,no_run
/// use cel_cxx::{FromMapKey, MapKey};
///
/// // Convert map keys back to Rust types
/// let cel_key = MapKey::String("hello".to_string().into());
/// let rust_string = String::from_mapkey(&cel_key).unwrap();
/// assert_eq!(rust_string, "hello");
///
/// let cel_key = MapKey::Int(42);
/// let rust_int = i64::from_mapkey(&cel_key).unwrap();
/// assert_eq!(rust_int, 42);
/// ```
/// Error type for failed map key conversions.
///
/// This error is returned when attempting to convert a CEL [`MapKey`] to a Rust type
/// using the [`FromMapKey`] trait, but the conversion fails due to type mismatch.
///
/// # Error Information
///
/// The error contains:
/// - **Source key**: The original CEL map key that could not be converted
/// - **Target type**: The intended map key type for the conversion
///
/// # Map Key Constraints
///
/// Only certain types can be used as CEL map keys:
/// - `bool` - Boolean keys
/// - `i64` - Signed integer keys
/// - `u64` - Unsigned integer keys
/// - `String` - String keys
///
/// # Common Causes
///
/// - **Type mismatch**: Trying to convert `MapKey::String` to `i64`
/// - **Invalid key type**: Attempting conversion to unsupported key type
/// - **Format issues**: Key value doesn't meet target type constraints
///
/// # Examples
///
/// ```rust
/// use cel_cxx::{MapKey, FromMapKey, FromMapKeyError};
///
/// let string_key = MapKey::String("not_a_number".to_string().into());
/// let result = i64::from_mapkey(&string_key);
///
/// match result {
/// Ok(num) => println!("Converted key: {}", num),
/// Err(error) => {
/// println!("Cannot convert key: {}", error);
/// }
/// }
/// ```
/// Trait for types that can be converted into compile-time CEL constants.
///
/// This trait extends [`IntoValue`] and [`TypedValue`] to provide conversion
/// to [`Constant`] values, which can be used for compile-time optimization
/// and type inference in CEL expressions.
///
/// # Compile-Time vs Runtime Values
///
/// - **Constants**: Known at compile-time, can be folded and optimized
/// - **Values**: Computed at runtime, require full evaluation
///
/// Constants enable the CEL compiler to:
/// - Perform constant folding optimizations
/// - Detect type errors earlier
/// - Generate more efficient evaluation code
/// - Support constant expression evaluation
///
/// # Automatic Implementation
///
/// This trait is automatically implemented for all types that implement
/// both [`IntoValue`] and [`TypedValue`], providing a default conversion
/// from value to constant.
///
/// # Examples
///
/// ## Basic Constant Creation
///
/// ```rust
/// use cel_cxx::{IntoConstant, Constant};
///
/// // Primitive constants
/// let null_const = ().into_constant(); // Constant::Null
/// let bool_const = true.into_constant(); // Constant::Bool(true)
/// let int_const = 42i64.into_constant(); // Constant::Int(42)
/// let string_const = "hello".into_constant(); // Constant::String("hello".to_string())
/// ```
///
/// ## Usage in Environment Building
///
/// ```rust,no_run
/// use cel_cxx::{Env, IntoConstant};
///
/// let env = Env::builder()
/// .define_constant("PI", 3.14159)?
/// .define_constant("APP_NAME", "MyApp")?
/// .define_constant("MAX_RETRIES", 5i64)?
/// .build()?;
///
/// // These constants can be used in expressions:
/// // "PI * radius * radius"
/// // "APP_NAME + ' v1.0'"
/// // "retries < MAX_RETRIES"
/// # Ok::<(), cel_cxx::Error>(())
/// ```
/// Trait for converting return values to CEL results.
///
/// This trait provides automatic conversion for function return types,
/// supporting both direct values and `Result` types for error handling.
///
/// # Note
///
/// This trait is sealed and cannot be implemented outside this crate.
///
/// # Implementations
///
/// - `T` where `T: IntoValue + TypedValue` - Direct value conversion
/// - `Result<T, E>` where `T: IntoValue + TypedValue, E: IntoError` - Error handling
// Sealed implementations for IntoResult