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#[derive(thiserror::Error, Debug, Clone)]
80pub struct InvalidMapKeyType(pub ValueType);
81
82impl InvalidMapKeyType {
83 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}