-
Notifications
You must be signed in to change notification settings - Fork 63
/
map_type.rs
160 lines (159 loc) · 7.91 KB
/
map_type.rs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::{
ScSpecTypeBytesN, ScSpecTypeDef, ScSpecTypeMap, ScSpecTypeOption, ScSpecTypeResult,
ScSpecTypeTuple, ScSpecTypeUdt, ScSpecTypeVec,
};
use syn::{
spanned::Spanned, Error, Expr, ExprLit, GenericArgument, Lit, Path, PathArguments, PathSegment,
Type, TypePath, TypeTuple,
};
#[allow(clippy::too_many_lines)]
pub fn map_type(t: &Type, allow_hash: bool) -> Result<ScSpecTypeDef, Error> {
match t {
Type::Path(TypePath {
qself: None,
path: Path { segments, .. },
}) => {
match segments.last() {
Some(PathSegment {
ident,
arguments: PathArguments::None,
}) => match &ident.to_string()[..] {
"Val" => Ok(ScSpecTypeDef::Val),
"u64" => Ok(ScSpecTypeDef::U64),
"i64" => Ok(ScSpecTypeDef::I64),
"u32" => Ok(ScSpecTypeDef::U32),
"i32" => Ok(ScSpecTypeDef::I32),
"u128" => Ok(ScSpecTypeDef::U128),
"i128" => Ok(ScSpecTypeDef::I128),
"U256" => Ok(ScSpecTypeDef::U256),
"I256" => Ok(ScSpecTypeDef::I256),
"bool" => Ok(ScSpecTypeDef::Bool),
"Symbol" => Ok(ScSpecTypeDef::Symbol),
"String" => Ok(ScSpecTypeDef::String),
"Error" => Ok(ScSpecTypeDef::Error),
"Bytes" => Ok(ScSpecTypeDef::Bytes),
"Address" => Ok(ScSpecTypeDef::Address),
"Timepoint" => Ok(ScSpecTypeDef::Timepoint),
"Duration" => Ok(ScSpecTypeDef::Duration),
s => Ok(ScSpecTypeDef::Udt(ScSpecTypeUdt {
name: s.try_into().map_err(|e| {
Error::new(
t.span(),
format!("Udt name {:?} cannot be used in XDR spec: {}", s, e),
)
})?,
})),
},
Some(PathSegment {
ident,
arguments: PathArguments::AngleBracketed(angle_bracketed),
}) => {
let args = angle_bracketed.args.iter().collect::<Vec<_>>();
match &ident.to_string()[..] {
"Result" => {
let (ok, err) = match args.as_slice() {
[GenericArgument::Type(ok), GenericArgument::Type(err)] => (ok, err),
[..] => Err(Error::new(
t.span(),
"incorrect number of generic arguments, expect two for Result<T, E>",
))?,
};
Ok(ScSpecTypeDef::Result(Box::new(ScSpecTypeResult {
ok_type: Box::new(map_type(ok, false)?),
error_type: Box::new(map_type(err, false)?),
})))
}
"Option" => {
let t = match args.as_slice() {
[GenericArgument::Type(t)] => t,
[..] => Err(Error::new(
t.span(),
"incorrect number of generic arguments, expect one for Option<T>",
))?,
};
Ok(ScSpecTypeDef::Option(Box::new(ScSpecTypeOption {
value_type: Box::new(map_type(t, false)?),
})))
}
"Vec" => {
let t = match args.as_slice() {
[GenericArgument::Type(t)] => t,
[..] => Err(Error::new(
t.span(),
"incorrect number of generic arguments, expect one for Vec<T>",
))?,
};
Ok(ScSpecTypeDef::Vec(Box::new(ScSpecTypeVec {
element_type: Box::new(map_type(t, false)?),
})))
}
"Map" => {
let (k, v) = match args.as_slice() {
[GenericArgument::Type(k), GenericArgument::Type(v)] => (k, v),
[..] => Err(Error::new(
t.span(),
"incorrect number of generic arguments, expect two for Map<K, V>",
))?,
};
Ok(ScSpecTypeDef::Map(Box::new(ScSpecTypeMap {
key_type: Box::new(map_type(k, false)?),
value_type: Box::new(map_type(v, false)?),
})))
}
"BytesN" => {
let n = match args.as_slice() {
[GenericArgument::Const(Expr::Lit(ExprLit { lit: Lit::Int(int), .. }))] => int.base10_parse()?,
[..] => Err(Error::new(
t.span(),
"incorrect number of generic arguments, expect one for BytesN<N>",
))?,
};
Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN { n }))
}
"Hash" => {
if allow_hash {
let n = match args.as_slice() {
[GenericArgument::Const(Expr::Lit(ExprLit { lit: Lit::Int(int), .. }))] => int.base10_parse()?,
[..] => Err(Error::new(
t.span(),
"incorrect number of generic arguments, expect one for Hash<N>",
))?,
};
Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN { n }))
} else {
Err(Error::new(
t.span(),
"Hash<N> can only be used in contexts where there is a guarantee that the hash has been sourced from a secure cryptographic hash function",
))
}
}
_ => Err(Error::new(
angle_bracketed.span(),
"generics unsupported on user-defined types in contract functions",
))?,
}
}
_ => Err(Error::new(t.span(), "unsupported type"))?,
}
}
Type::Tuple(TypeTuple { elems, .. }) => {
let map_type_reject_hash =
|t: &Type| -> Result<ScSpecTypeDef, Error> { map_type(t, false) };
Ok(ScSpecTypeDef::Tuple(Box::new(ScSpecTypeTuple {
value_types: elems
.iter()
.map(map_type_reject_hash)
.collect::<Result<Vec<ScSpecTypeDef>, Error>>()? // TODO: Implement conversion to VecM from iters to omit this collect.
.try_into()
.map_err(|e| {
Error::new(
t.span(),
format!("tuple values cannot be used in XDR spec: {}", e),
)
})?,
})))
}
_ => Err(Error::new(t.span(), "unsupported type"))?,
}
}