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
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Format {
pub store: StoreFormat,
pub order: OrderFormat,
}
impl Format {
pub fn new(store: StoreFormat, order: OrderFormat) -> Self {
Format { store, order }
}
#[cfg(feature = "scale-info")]
pub fn from_metadata(
ty: &scale_info::TypeDefBitSequence<scale_info::form::PortableForm>,
types: &scale_info::PortableRegistry,
) -> Result<Format, FromMetadataError> {
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(FromMetadataError::StoreFormatNotFound(bit_store_ty))?
.type_def();
let bit_order_def = types
.resolve(bit_order_ty)
.ok_or(FromMetadataError::OrderFormatNotFound(bit_order_ty))?
.path()
.ident()
.ok_or(FromMetadataError::NoBitOrderIdent)?;
use scale_info::{TypeDef, TypeDefPrimitive};
let bit_store_out = match bit_store_def {
TypeDef::Primitive(TypeDefPrimitive::U8) => Some(StoreFormat::U8),
TypeDef::Primitive(TypeDefPrimitive::U16) => Some(StoreFormat::U16),
TypeDef::Primitive(TypeDefPrimitive::U32) => Some(StoreFormat::U32),
TypeDef::Primitive(TypeDefPrimitive::U64) => Some(StoreFormat::U64),
_ => None,
}
.ok_or_else(|| FromMetadataError::StoreFormatNotSupported(format!("{bit_store_def:?}")))?;
let bit_order_out = match &*bit_order_def {
"Lsb0" => Some(OrderFormat::Lsb0),
"Msb0" => Some(OrderFormat::Msb0),
_ => None,
}
.ok_or(FromMetadataError::OrderFormatNotSupported(bit_order_def))?;
Ok(Format { store: bit_store_out, order: bit_order_out })
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum OrderFormat {
Lsb0,
Msb0,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum StoreFormat {
U8,
U16,
U32,
U64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FromMetadataError {
OrderFormatNotFound(u32),
StoreFormatNotFound(u32),
NoBitOrderIdent,
StoreFormatNotSupported(String),
OrderFormatNotSupported(String),
}
impl std::fmt::Display for FromMetadataError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FromMetadataError::OrderFormatNotFound(n) => {
write!(f, "Bit order type {n} not found in registry")
}
FromMetadataError::StoreFormatNotFound(n) => {
write!(f, "Bit store type {n} not found in registry")
}
FromMetadataError::NoBitOrderIdent => {
write!(f, "Bit order cannot be identified")
}
FromMetadataError::StoreFormatNotSupported(s) => {
write!(f, "Bit store type '{s}' is not supported")
}
FromMetadataError::OrderFormatNotSupported(s) => {
write!(f, "Bit order type '{s}' is not supported")
}
}
}
}
impl std::error::Error for FromMetadataError {}
#[cfg(feature = "scale-info")]
#[cfg(test)]
mod test {
use super::*;
fn make_type<T: scale_info::TypeInfo + 'static>() -> (u32, scale_info::PortableRegistry) {
let m = scale_info::MetaType::new::<T>();
let mut types = scale_info::Registry::new();
let id = types.register_type(&m);
let portable_registry: scale_info::PortableRegistry = types.into();
(id.id(), portable_registry)
}
fn assert_format<T: scale_info::TypeInfo + 'static>(store: StoreFormat, order: OrderFormat) {
let (id, types) = make_type::<T>();
let ty = match types.resolve(id).unwrap().type_def() {
scale_info::TypeDef::BitSequence(b) => b,
_ => panic!("expected type to look like a bit sequence"),
};
let actual_format =
crate::Format::from_metadata(ty, &types).expect("can obtain BitSeq Format from type");
assert_eq!(Format::new(store, order), actual_format);
}
#[test]
fn format_extracted_properly() {
use bitvec::{
order::{Lsb0, Msb0},
vec::BitVec,
};
assert_format::<crate::Bits>(StoreFormat::U8, OrderFormat::Lsb0);
assert_format::<BitVec<u8, Lsb0>>(StoreFormat::U8, OrderFormat::Lsb0);
assert_format::<BitVec<u16, Lsb0>>(StoreFormat::U16, OrderFormat::Lsb0);
assert_format::<BitVec<u32, Lsb0>>(StoreFormat::U32, OrderFormat::Lsb0);
assert_format::<BitVec<u64, Lsb0>>(StoreFormat::U64, OrderFormat::Lsb0);
assert_format::<BitVec<u8, Msb0>>(StoreFormat::U8, OrderFormat::Msb0);
assert_format::<BitVec<u16, Msb0>>(StoreFormat::U16, OrderFormat::Msb0);
assert_format::<BitVec<u32, Msb0>>(StoreFormat::U32, OrderFormat::Msb0);
assert_format::<BitVec<u64, Msb0>>(StoreFormat::U64, OrderFormat::Msb0);
}
}