-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasting.rs
More file actions
141 lines (133 loc) · 3.27 KB
/
casting.rs
File metadata and controls
141 lines (133 loc) · 3.27 KB
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
use super::*;
impl<B: AsRef<[u8]> + ByteStorage> Value<B> {
pub const fn as_tag(&self) -> Tag {
macro_rules! as_tag_impl {
(
$($name:ident)*
) => {
match self {
$(Value::$name(..) => Tag::$name,)*
}
};
}
as_tag_impl! {
Uint
Int
Bool
Uints
String
Bytes
Tuple
List
Option
Alias
Enum
Choice
Struct
Type
TypeId
}
}
fn as_type(&self) -> Type {
macro_rules! as_type_impl {
(
direct {$($direct_name:ident)*}
type {$($type_name:ident)*}
type_id {$($type_id_name:ident)*}
$($tt:tt)*
) => {
match self {
$(Value::$direct_name(..) => Type::$direct_name,)*
$($tt)*
$(Value::$type_name(r#type, ..) => Type::$type_name(Box::new(r#type.clone())),)*
$(Value::$type_id_name(type_id, ..) => Type::$type_id_name(type_id.clone()),)*
}
};
}
as_type_impl! {
direct {
Uint
Int
Bool
Uints
Bytes
String
Type
TypeId
}
type {
List
Option
}
type_id {
Alias
Enum
Choice
Struct
}
Value::Tuple(values) => {
Type::Tuple(values.iter().map(|value| value.as_type()).collect())
}
}
}
}
impl Type {
pub const fn as_type_tag(&self) -> TypeTag {
macro_rules! as_tag_impl {
(
empty {$($empty_name:ident)*}
non_empty {$($name:ident)*}
) => {
match self {
$(Type::$empty_name => TypeTag::$empty_name,)*
$(Type::$name(..) => TypeTag::$name,)*
}
};
}
as_tag_impl! {
empty {
Unknown
Uint
Int
Bool
Uints
String
Bytes
Type
TypeId
}
non_empty {
Tuple
List
Option
Alias
Enum
Choice
Struct
}
}
}
}
impl TypeId {
pub const fn as_type_id_tag(&self) -> TypeIdTag {
macro_rules! as_tag_impl {
(
empty {$($empty_name:ident)*}
non_empty {$($name:ident)*}
) => {
match self {
$(TypeId::$empty_name => TypeIdTag::$empty_name,)*
$(TypeId::$name { .. } => TypeIdTag::$name,)*
}
};
}
as_tag_impl! {
empty {
Anonymous
}
non_empty {
Std
}
}
}
}