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
use crate::metadata::{
registry::local_type::LocalType,
result::{Error, Result},
};
use parity_scale_codec::Decode;
use scale_info::{
form::{Form, MetaForm, PortableForm},
interner::UntrackedSymbol,
PortableRegistry, Type, TypeDef,
};
use std::{any::TypeId, collections::HashMap, convert::TryFrom, fmt, ops::Deref};
pub trait LocalRegistry: Sized + Clone {
fn from_hex(hex: &str) -> Result<Self>;
fn derive_id(&self, id: u32) -> Result<LocalType<'_, PortableForm>>;
fn derive_name(&self, ident: &str) -> Result<LocalType<'_, PortableForm>>;
}
impl LocalRegistry for PortableRegistry {
fn from_hex(encoded: &str) -> Result<Self> {
Ok(PortableRegistry::decode(
&mut hex::decode(encoded)?.as_ref(),
)?)
}
fn derive_id(&self, id: u32) -> Result<LocalType<'_, PortableForm>> {
Ok(LocalType {
ty: self
.resolve(id)
.ok_or_else(|| Error::TypeNotFound(format!("{id:?}")))?,
registry: self,
})
}
fn derive_name(&self, ident: &str) -> Result<LocalType<'_, PortableForm>> {
for ty in self.types() {
let ty = ty.ty();
if ty.path().ident() == Some(ident.into()) {
return Ok(LocalType { ty, registry: self });
}
}
Err(Error::TypeNotFound(ident.into()))
}
}