Skip to main content

rib/
value_and_type.rs

1use crate::value::Value;
2use crate::wit_type::WitType;
3
4#[derive(Clone, Debug, PartialEq)]
5pub struct ValueAndType {
6    pub value: Value,
7    pub typ: WitType,
8}
9
10impl ValueAndType {
11    pub fn new(value: Value, typ: WitType) -> Self {
12        Self { value, typ }
13    }
14}
15
16impl std::fmt::Display for ValueAndType {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match crate::print_value_and_type(self) {
19            Ok(s) => write!(f, "{s}"),
20            Err(_) => write!(f, "{:?}", self.value),
21        }
22    }
23}
24
25impl From<ValueAndType> for Value {
26    fn from(value_and_type: ValueAndType) -> Self {
27        value_and_type.value
28    }
29}
30
31impl From<ValueAndType> for WitType {
32    fn from(value_and_type: ValueAndType) -> Self {
33        value_and_type.typ
34    }
35}
36
37pub trait IntoValue {
38    fn into_value(self) -> Value;
39    fn get_type() -> WitType;
40}
41
42pub trait IntoValueAndType {
43    fn into_value_and_type(self) -> ValueAndType;
44}
45
46impl<T: IntoValue + Sized> IntoValueAndType for T {
47    fn into_value_and_type(self) -> ValueAndType {
48        ValueAndType::new(self.into_value(), Self::get_type())
49    }
50}
51
52use crate::wit_type::builders as wit_type;
53
54impl IntoValue for u8 {
55    fn into_value(self) -> Value {
56        Value::U8(self)
57    }
58    fn get_type() -> WitType {
59        wit_type::u8()
60    }
61}
62
63impl IntoValue for u16 {
64    fn into_value(self) -> Value {
65        Value::U16(self)
66    }
67    fn get_type() -> WitType {
68        wit_type::u16()
69    }
70}
71
72impl IntoValue for u32 {
73    fn into_value(self) -> Value {
74        Value::U32(self)
75    }
76    fn get_type() -> WitType {
77        wit_type::u32()
78    }
79}
80
81impl IntoValue for u64 {
82    fn into_value(self) -> Value {
83        Value::U64(self)
84    }
85    fn get_type() -> WitType {
86        wit_type::u64()
87    }
88}
89
90impl IntoValue for i8 {
91    fn into_value(self) -> Value {
92        Value::S8(self)
93    }
94    fn get_type() -> WitType {
95        wit_type::s8()
96    }
97}
98
99impl IntoValue for i16 {
100    fn into_value(self) -> Value {
101        Value::S16(self)
102    }
103    fn get_type() -> WitType {
104        wit_type::s16()
105    }
106}
107
108impl IntoValue for i32 {
109    fn into_value(self) -> Value {
110        Value::S32(self)
111    }
112    fn get_type() -> WitType {
113        wit_type::s32()
114    }
115}
116
117impl IntoValue for i64 {
118    fn into_value(self) -> Value {
119        Value::S64(self)
120    }
121    fn get_type() -> WitType {
122        wit_type::s64()
123    }
124}
125
126impl IntoValue for f32 {
127    fn into_value(self) -> Value {
128        Value::F32(self)
129    }
130    fn get_type() -> WitType {
131        wit_type::f32()
132    }
133}
134
135impl IntoValue for f64 {
136    fn into_value(self) -> Value {
137        Value::F64(self)
138    }
139    fn get_type() -> WitType {
140        wit_type::f64()
141    }
142}
143
144impl IntoValue for bool {
145    fn into_value(self) -> Value {
146        Value::Bool(self)
147    }
148    fn get_type() -> WitType {
149        wit_type::bool()
150    }
151}
152
153impl IntoValue for char {
154    fn into_value(self) -> Value {
155        Value::Char(self)
156    }
157    fn get_type() -> WitType {
158        wit_type::chr()
159    }
160}
161
162impl IntoValue for String {
163    fn into_value(self) -> Value {
164        Value::String(self)
165    }
166    fn get_type() -> WitType {
167        wit_type::str()
168    }
169}
170
171impl IntoValue for &str {
172    fn into_value(self) -> Value {
173        Value::String(self.to_string())
174    }
175    fn get_type() -> WitType {
176        wit_type::str()
177    }
178}
179
180impl<T: IntoValue> IntoValue for Vec<T> {
181    fn into_value(self) -> Value {
182        Value::List(self.into_iter().map(|v| v.into_value()).collect())
183    }
184    fn get_type() -> WitType {
185        wit_type::list(T::get_type())
186    }
187}
188
189impl<T: IntoValue> IntoValue for Option<T> {
190    fn into_value(self) -> Value {
191        Value::Option(self.map(|v| Box::new(v.into_value())))
192    }
193    fn get_type() -> WitType {
194        wit_type::option(T::get_type())
195    }
196}
197
198impl<A: IntoValue, B: IntoValue> IntoValue for (A, B) {
199    fn into_value(self) -> Value {
200        Value::Tuple(vec![self.0.into_value(), self.1.into_value()])
201    }
202    fn get_type() -> WitType {
203        wit_type::tuple(vec![A::get_type(), B::get_type()])
204    }
205}