cel_cxx/types/
convert.rs

1use super::*;
2use crate::Error;
3
4impl From<StructType> for ValueType {
5    fn from(value: StructType) -> Self {
6        ValueType::Struct(value)
7    }
8}
9
10impl From<ListType> for ValueType {
11    fn from(value: ListType) -> Self {
12        ValueType::List(value)
13    }
14}
15
16impl From<MapType> for ValueType {
17    fn from(value: MapType) -> Self {
18        ValueType::Map(value)
19    }
20}
21
22impl From<TypeType> for ValueType {
23    fn from(value: TypeType) -> Self {
24        ValueType::Type(value)
25    }
26}
27
28impl From<OpaqueType> for ValueType {
29    fn from(value: OpaqueType) -> Self {
30        ValueType::Opaque(value)
31    }
32}
33
34impl From<OptionalType> for ValueType {
35    fn from(value: OptionalType) -> Self {
36        ValueType::Optional(value)
37    }
38}
39
40impl From<TypeParamType> for ValueType {
41    fn from(value: TypeParamType) -> Self {
42        ValueType::TypeParam(value)
43    }
44}
45
46impl From<FunctionType> for ValueType {
47    fn from(value: FunctionType) -> Self {
48        ValueType::Function(value)
49    }
50}
51
52impl From<EnumType> for ValueType {
53    fn from(value: EnumType) -> Self {
54        ValueType::Enum(value)
55    }
56}
57
58/// Error type for invalid map key type conversions.
59///
60/// `InvalidMapKeyType` is returned when attempting to convert a [`ValueType`] to a [`MapKeyType`]
61/// fails because the type is not a valid map key type. According to the CEL specification,
62/// only basic comparable types (bool, int, uint, string) can be used as map keys.
63///
64/// # Examples
65///
66/// ```rust,no_run
67/// use cel_cxx::{ValueType, ListType, MapKeyType, InvalidMapKeyType};
68///
69/// // Valid map key type
70/// let string_type = ValueType::String;
71/// let map_key_type = MapKeyType::try_from(string_type).unwrap();
72/// assert_eq!(map_key_type, MapKeyType::String);
73///
74/// // Invalid map key type
75/// let list_type = ValueType::List(ListType::new(ValueType::Int));
76/// let result = MapKeyType::try_from(list_type);
77/// assert!(result.is_err());
78/// ```
79#[derive(thiserror::Error, Debug, Clone)]
80pub struct InvalidMapKeyType(pub ValueType);
81
82impl InvalidMapKeyType {
83    /// Creates a new `InvalidMapKeyType` error.
84    ///
85    /// # Parameters
86    ///
87    /// - `ty`: The type that failed to convert to a map key type
88    ///
89    /// # Returns
90    ///
91    /// New `InvalidMapKeyType` instance
92    pub fn new(ty: ValueType) -> Self {
93        Self(ty)
94    }
95}
96
97impl std::fmt::Display for InvalidMapKeyType {
98    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        write!(f, "invalid key type: {}", self.0)
100    }
101}
102
103impl From<InvalidMapKeyType> for Error {
104    fn from(error: InvalidMapKeyType) -> Self {
105        Error::invalid_argument(format!("invalid key type: {}", error.0))
106    }
107}
108
109impl From<MapKeyType> for ValueType {
110    fn from(key: MapKeyType) -> Self {
111        match key {
112            MapKeyType::Bool => ValueType::Bool,
113            MapKeyType::Int => ValueType::Int,
114            MapKeyType::Uint => ValueType::Uint,
115            MapKeyType::String => ValueType::String,
116            MapKeyType::Dyn => ValueType::Dyn,
117            MapKeyType::TypeParam(tp) => ValueType::TypeParam(tp),
118        }
119    }
120}
121
122impl TryFrom<ValueType> for MapKeyType {
123    type Error = InvalidMapKeyType;
124
125    fn try_from(value: ValueType) -> Result<Self, Self::Error> {
126        match value {
127            ValueType::Bool => Ok(MapKeyType::Bool),
128            ValueType::Int => Ok(MapKeyType::Int),
129            ValueType::Uint => Ok(MapKeyType::Uint),
130            ValueType::String => Ok(MapKeyType::String),
131            ValueType::Dyn => Ok(MapKeyType::Dyn),
132            ValueType::TypeParam(tp) => Ok(MapKeyType::TypeParam(tp)),
133            _ => Err(InvalidMapKeyType::new(value)),
134        }
135    }
136}