use crate::{ModuleTypes, Result, TypeRange};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize, Default, Eq, PartialEq, Clone)]
pub struct Overrides {
#[serde(rename = "TYPES_MODULES")]
types_modules: HashMap<String, ModuleTypes>,
#[serde(rename = "TYPES_SPEC")]
types_spec: HashMap<String, Vec<TypeRange>>,
}
impl Overrides {
pub fn new(raw_json: &str) -> Result<Self> {
serde_json::from_str(raw_json).map_err(Into::into)
}
pub fn get_chain_types(&self, chain: &str, spec: u32) -> Option<&ModuleTypes> {
self.types_spec.get(chain)?.iter().find(|f| crate::is_in_range(spec, f)).map(|o| &o.types)
}
pub fn get_module_types(&self, module: &str) -> Option<&ModuleTypes> {
self.types_modules.get(module)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_deserialize_into_single_override() {
let json = r#"
{
"minmax": [
1020,
1031
],
"types": {
"BalanceLock": "BalanceLockTo212",
"DispatchError": "DispatchErrorTo198",
"Keys": "SessionKeys5",
"SlashingSpans": "SlashingSpansTo204"
}
}
"#;
let single_override: TypeRange = serde_json::from_str(json).unwrap();
dbg!(single_override);
}
#[test]
fn should_deserialize_into_spec() {
let json = r#"
{
"kusama": [
{
"minmax": [
1019,
1031
],
"types": {
"BalanceLock": "BalanceLockTo212",
"DispatchError": "DispatchErrorTo198",
"Keys": "SessionKeys5",
"SlashingSpans": "SlashingSpansTo204"
}
},
{
"minmax": [
1032,
1042
],
"types": {
"BalanceLock": "BalanceLockTo212",
"Keys": "SessionKeys5",
"SlashingSpans": "SlashingSpansTo204"
}
},
{
"minmax": [
1043,
null
],
"types": {
"BalanceLock": "BalanceLockTo212",
"Keys": "SessionKeys5"
}
}
],
"polkadot": [
{
"minmax": [
1000,
null
],
"types": {
"Keys": "SessionKeys5"
}
}
]
}
"#;
let types_spec: HashMap<String, Vec<TypeRange>> = serde_json::from_str(json).unwrap();
dbg!(types_spec);
}
}