-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathlib.rs
502 lines (453 loc) · 14.3 KB
/
lib.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use std::str::FromStr;
use anyhow::anyhow;
use serde::{Deserialize, Serialize};
/// IDL specification Semantic Version
pub const IDL_SPEC: &str = env!("CARGO_PKG_VERSION");
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Idl {
pub address: String,
pub metadata: IdlMetadata,
#[serde(default, skip_serializing_if = "is_default")]
pub docs: Vec<String>,
pub instructions: Vec<IdlInstruction>,
#[serde(default, skip_serializing_if = "is_default")]
pub accounts: Vec<IdlAccount>,
#[serde(default, skip_serializing_if = "is_default")]
pub events: Vec<IdlEvent>,
#[serde(default, skip_serializing_if = "is_default")]
pub errors: Vec<IdlErrorCode>,
#[serde(default, skip_serializing_if = "is_default")]
pub types: Vec<IdlTypeDef>,
#[serde(default, skip_serializing_if = "is_default")]
pub constants: Vec<IdlConst>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlMetadata {
pub name: String,
pub version: String,
pub spec: String,
#[serde(skip_serializing_if = "is_default")]
pub description: Option<String>,
#[serde(skip_serializing_if = "is_default")]
pub repository: Option<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub dependencies: Vec<IdlDependency>,
#[serde(skip_serializing_if = "is_default")]
pub contact: Option<String>,
#[serde(skip_serializing_if = "is_default")]
pub deployments: Option<IdlDeployments>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlDependency {
pub name: String,
pub version: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlDeployments {
pub mainnet: Option<String>,
pub testnet: Option<String>,
pub devnet: Option<String>,
pub localnet: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlInstruction {
pub name: String,
#[serde(default, skip_serializing_if = "is_default")]
pub docs: Vec<String>,
pub discriminator: IdlDiscriminator,
pub accounts: Vec<IdlInstructionAccountItem>,
pub args: Vec<IdlField>,
#[serde(skip_serializing_if = "is_default")]
pub returns: Option<IdlType>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum IdlInstructionAccountItem {
Composite(IdlInstructionAccounts),
Single(IdlInstructionAccount),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlInstructionAccount {
pub name: String,
#[serde(default, skip_serializing_if = "is_default")]
pub docs: Vec<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub writable: bool,
#[serde(default, skip_serializing_if = "is_default")]
pub signer: bool,
#[serde(default, skip_serializing_if = "is_default")]
pub optional: bool,
#[serde(skip_serializing_if = "is_default")]
pub address: Option<String>,
#[serde(skip_serializing_if = "is_default")]
pub pda: Option<IdlPda>,
#[serde(default, skip_serializing_if = "is_default")]
pub relations: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlInstructionAccounts {
pub name: String,
pub accounts: Vec<IdlInstructionAccountItem>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlPda {
pub seeds: Vec<IdlSeed>,
#[serde(skip_serializing_if = "is_default")]
pub program: Option<IdlSeed>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum IdlSeed {
Const(IdlSeedConst),
Arg(IdlSeedArg),
Account(IdlSeedAccount),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlSeedConst {
pub value: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlSeedArg {
pub path: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlSeedAccount {
pub path: String,
#[serde(skip_serializing_if = "is_default")]
pub account: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlAccount {
pub name: String,
pub discriminator: IdlDiscriminator,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlEvent {
pub name: String,
pub discriminator: IdlDiscriminator,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlConst {
pub name: String,
#[serde(default, skip_serializing_if = "is_default")]
pub docs: Vec<String>,
#[serde(rename = "type")]
pub ty: IdlType,
pub value: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct IdlErrorCode {
pub code: u32,
pub name: String,
#[serde(skip_serializing_if = "is_default")]
pub msg: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlField {
pub name: String,
#[serde(default, skip_serializing_if = "is_default")]
pub docs: Vec<String>,
#[serde(rename = "type")]
pub ty: IdlType,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlTypeDef {
pub name: String,
#[serde(default, skip_serializing_if = "is_default")]
pub docs: Vec<String>,
#[serde(default, skip_serializing_if = "is_default")]
pub serialization: IdlSerialization,
#[serde(skip_serializing_if = "is_default")]
pub repr: Option<IdlRepr>,
#[serde(default, skip_serializing_if = "is_default")]
pub generics: Vec<IdlTypeDefGeneric>,
#[serde(rename = "type")]
pub ty: IdlTypeDefTy,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum IdlSerialization {
#[default]
Borsh,
Bytemuck,
BytemuckUnsafe,
Custom(String),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind", rename_all = "lowercase")]
#[non_exhaustive]
pub enum IdlRepr {
Rust(IdlReprModifier),
C(IdlReprModifier),
Transparent,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlReprModifier {
#[serde(default, skip_serializing_if = "is_default")]
pub packed: bool,
#[serde(skip_serializing_if = "is_default")]
pub align: Option<usize>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum IdlTypeDefGeneric {
Type {
name: String,
},
Const {
name: String,
#[serde(rename = "type")]
ty: String,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum IdlTypeDefTy {
Struct {
#[serde(skip_serializing_if = "is_default")]
fields: Option<IdlDefinedFields>,
},
Enum {
variants: Vec<IdlEnumVariant>,
},
Type {
alias: IdlType,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlEnumVariant {
pub name: String,
#[serde(skip_serializing_if = "is_default")]
pub fields: Option<IdlDefinedFields>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum IdlDefinedFields {
Named(Vec<IdlField>),
Tuple(Vec<IdlType>),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum IdlArrayLen {
Generic(String),
#[serde(untagged)]
Value(usize),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum IdlGenericArg {
Type {
#[serde(rename = "type")]
ty: IdlType,
},
Const {
value: String,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum IdlType {
Bool,
U8,
I8,
U16,
I16,
U32,
I32,
F32,
U64,
I64,
F64,
U128,
I128,
U256,
I256,
Bytes,
String,
Pubkey,
Option(Box<IdlType>),
Vec(Box<IdlType>),
Array(Box<IdlType>, IdlArrayLen),
Defined {
name: String,
#[serde(default, skip_serializing_if = "is_default")]
generics: Vec<IdlGenericArg>,
},
Generic(String),
}
// TODO: Move to utils crate
impl FromStr for IdlType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut s = s.to_owned();
s.retain(|c| !c.is_whitespace());
let r = match s.as_str() {
"bool" => IdlType::Bool,
"u8" => IdlType::U8,
"i8" => IdlType::I8,
"u16" => IdlType::U16,
"i16" => IdlType::I16,
"u32" => IdlType::U32,
"i32" => IdlType::I32,
"f32" => IdlType::F32,
"u64" => IdlType::U64,
"i64" => IdlType::I64,
"f64" => IdlType::F64,
"u128" => IdlType::U128,
"i128" => IdlType::I128,
"u256" => IdlType::U256,
"i256" => IdlType::I256,
"Vec<u8>" => IdlType::Bytes,
"String" | "&str" | "&'staticstr" => IdlType::String,
"Pubkey" => IdlType::Pubkey,
_ => {
if let Some(inner) = s.strip_prefix("Option<") {
let inner_ty = Self::from_str(
inner
.strip_suffix('>')
.ok_or_else(|| anyhow!("Invalid Option"))?,
)?;
return Ok(IdlType::Option(Box::new(inner_ty)));
}
if let Some(inner) = s.strip_prefix("Vec<") {
let inner_ty = Self::from_str(
inner
.strip_suffix('>')
.ok_or_else(|| anyhow!("Invalid Vec"))?,
)?;
return Ok(IdlType::Vec(Box::new(inner_ty)));
}
if s.starts_with('[') {
fn array_from_str(inner: &str) -> IdlType {
match inner.strip_suffix(']') {
Some(nested_inner) => array_from_str(&nested_inner[1..]),
None => {
let (raw_type, raw_length) = inner.rsplit_once(';').unwrap();
let ty = IdlType::from_str(raw_type).unwrap();
let len = match raw_length.replace('_', "").parse::<usize>() {
Ok(len) => IdlArrayLen::Value(len),
Err(_) => IdlArrayLen::Generic(raw_length.to_owned()),
};
IdlType::Array(Box::new(ty), len)
}
}
}
return Ok(array_from_str(&s));
}
// Defined
let (name, generics) = if let Some(i) = s.find('<') {
(
s.get(..i).unwrap().to_owned(),
s.get(i + 1..)
.unwrap()
.strip_suffix('>')
.unwrap()
.split(',')
.map(|g| g.trim().to_owned())
.map(|g| {
if g.parse::<bool>().is_ok()
|| g.parse::<u128>().is_ok()
|| g.parse::<i128>().is_ok()
|| g.parse::<char>().is_ok()
{
Ok(IdlGenericArg::Const { value: g })
} else {
Self::from_str(&g).map(|ty| IdlGenericArg::Type { ty })
}
})
.collect::<Result<Vec<_>, _>>()?,
)
} else {
(s.to_owned(), vec![])
};
IdlType::Defined { name, generics }
}
};
Ok(r)
}
}
pub type IdlDiscriminator = Vec<u8>;
/// Get whether the given data is the default of its type.
fn is_default<T: Default + PartialEq>(it: &T) -> bool {
*it == T::default()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn option() {
assert_eq!(
IdlType::from_str("Option<bool>").unwrap(),
IdlType::Option(Box::new(IdlType::Bool))
)
}
#[test]
fn vector() {
assert_eq!(
IdlType::from_str("Vec<bool>").unwrap(),
IdlType::Vec(Box::new(IdlType::Bool))
)
}
#[test]
fn array() {
assert_eq!(
IdlType::from_str("[Pubkey; 16]").unwrap(),
IdlType::Array(Box::new(IdlType::Pubkey), IdlArrayLen::Value(16))
);
}
#[test]
fn array_with_underscored_length() {
assert_eq!(
IdlType::from_str("[u8; 50_000]").unwrap(),
IdlType::Array(Box::new(IdlType::U8), IdlArrayLen::Value(50000))
);
}
#[test]
fn multidimensional_array() {
assert_eq!(
IdlType::from_str("[[u8; 16]; 32]").unwrap(),
IdlType::Array(
Box::new(IdlType::Array(
Box::new(IdlType::U8),
IdlArrayLen::Value(16)
)),
IdlArrayLen::Value(32)
)
);
}
#[test]
fn generic_array() {
assert_eq!(
IdlType::from_str("[u64; T]").unwrap(),
IdlType::Array(Box::new(IdlType::U64), IdlArrayLen::Generic("T".into()))
);
}
#[test]
fn defined() {
assert_eq!(
IdlType::from_str("MyStruct").unwrap(),
IdlType::Defined {
name: "MyStruct".into(),
generics: vec![]
}
)
}
#[test]
fn defined_with_generics() {
assert_eq!(
IdlType::from_str("MyStruct<Pubkey, u64, 8>").unwrap(),
IdlType::Defined {
name: "MyStruct".into(),
generics: vec![
IdlGenericArg::Type {
ty: IdlType::Pubkey
},
IdlGenericArg::Type { ty: IdlType::U64 },
IdlGenericArg::Const { value: "8".into() },
],
}
)
}
}