spacetimedb_sats/
convert.rs

1use crate::sum_value::SumTag;
2use crate::{i256, u256};
3use crate::{AlgebraicType, AlgebraicValue, ProductType, ProductValue};
4use spacetimedb_primitives::{ArgId, ColId, ConstraintId, IndexId, ScheduleId, SequenceId, TableId, ViewId};
5
6impl crate::Value for AlgebraicValue {
7    type Type = AlgebraicType;
8}
9
10impl<X: Into<Box<[AlgebraicValue]>>> From<X> for ProductValue {
11    fn from(elements: X) -> Self {
12        let elements = elements.into();
13        ProductValue { elements }
14    }
15}
16
17impl From<AlgebraicValue> for ProductValue {
18    fn from(x: AlgebraicValue) -> Self {
19        [x].into()
20    }
21}
22
23impl From<AlgebraicType> for ProductType {
24    fn from(x: AlgebraicType) -> Self {
25        Self::new([x.into()].into())
26    }
27}
28
29impl From<()> for AlgebraicValue {
30    fn from((): ()) -> Self {
31        AlgebraicValue::unit()
32    }
33}
34
35macro_rules! built_in_into {
36    ($native:ty, $kind:ident) => {
37        impl From<$native> for AlgebraicValue {
38            fn from(x: $native) -> Self {
39                Self::$kind(x.into())
40            }
41        }
42    };
43}
44
45built_in_into!(u128, U128);
46built_in_into!(i128, I128);
47built_in_into!(u256, U256);
48built_in_into!(i256, I256);
49built_in_into!(f32, F32);
50built_in_into!(f64, F64);
51built_in_into!(&str, String);
52built_in_into!(String, String);
53built_in_into!(&[u8], Bytes);
54built_in_into!(Box<[u8]>, Bytes);
55built_in_into!(SumTag, Sum);
56
57macro_rules! system_id {
58    ($name:ident) => {
59        impl From<$name> for AlgebraicValue {
60            fn from(value: $name) -> Self {
61                value.0.into()
62            }
63        }
64    };
65}
66system_id!(ArgId);
67system_id!(TableId);
68system_id!(ViewId);
69system_id!(ColId);
70system_id!(SequenceId);
71system_id!(IndexId);
72system_id!(ConstraintId);
73system_id!(ScheduleId);