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
use super::ScaleTypeDef as TypeDef;
use scale_info::{form::PortableForm, PortableRegistry, TypeDefBitSequence, TypeDefPrimitive};
#[derive(Debug, Clone, thiserror::Error, PartialEq)]
pub enum BitSequenceError {
#[error("Bit order type {0} not found in registry")]
BitOrderTypeNotFound(u32),
#[error("Bit store type {0} not found in registry")]
BitStoreTypeNotFound(u32),
#[error("Bit order cannot be identified")]
NoBitOrderIdent,
#[error("Bit store type {0} is not supported")]
StoreTypeNotSupported(String),
#[error("Bit order type {0} is not supported")]
OrderTypeNotSupported(String),
}
pub fn get_bitsequence_details(
ty: &TypeDefBitSequence<PortableForm>,
types: &PortableRegistry,
) -> Result<(BitStoreTy, BitOrderTy), BitSequenceError> {
let bit_store_ty = ty.bit_store_type().id();
let bit_order_ty = ty.bit_order_type().id();
let bit_store_def = types
.resolve(bit_store_ty)
.ok_or(BitSequenceError::BitStoreTypeNotFound(bit_store_ty))?
.type_def();
let bit_order_def = types
.resolve(bit_order_ty)
.ok_or(BitSequenceError::BitOrderTypeNotFound(bit_order_ty))?
.path()
.ident()
.ok_or(BitSequenceError::NoBitOrderIdent)?;
let bit_store_out = match bit_store_def {
TypeDef::Primitive(TypeDefPrimitive::U8) => Some(BitStoreTy::U8),
TypeDef::Primitive(TypeDefPrimitive::U16) => Some(BitStoreTy::U16),
TypeDef::Primitive(TypeDefPrimitive::U32) => Some(BitStoreTy::U32),
TypeDef::Primitive(TypeDefPrimitive::U64) => Some(BitStoreTy::U64),
_ => None,
}
.ok_or_else(|| BitSequenceError::StoreTypeNotSupported(format!("{bit_store_def:?}")))?;
let bit_order_out = match &*bit_order_def {
"Lsb0" => Some(BitOrderTy::Lsb0),
"Msb0" => Some(BitOrderTy::Msb0),
_ => None,
}
.ok_or(BitSequenceError::OrderTypeNotSupported(bit_order_def))?;
Ok((bit_store_out, bit_order_out))
}
#[derive(Copy, Clone, PartialEq)]
pub enum BitOrderTy {
Lsb0,
Msb0,
}
#[derive(Copy, Clone, PartialEq)]
pub enum BitStoreTy {
U8,
U16,
U32,
U64,
}