-
Notifications
You must be signed in to change notification settings - Fork 13
Update Type Definitions to latest PolkadotJS #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
d3f2fc6
26fb7c6
6cbc5af
02f19e1
7e6e3f1
841a8a3
1f5d5b6
5d58622
c3cc791
022a859
ae81fb7
d4d67c9
6d45bf1
ff867b1
9441229
3de3101
c6bdd11
6dc9052
c288fd0
0b42103
d67d602
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ use onig::{Regex, Region, SearchOptions}; | |
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| enum RegexSet { | ||
| ArrayPrimitive, | ||
| BitSize, | ||
| ArrayStruct, | ||
| Vec, | ||
| Option, | ||
|
|
@@ -37,6 +38,8 @@ impl RegexSet { | |
| fn get_type(s: &str) -> Option<RegexSet> { | ||
| if rust_array_decl_prim().is_match(s) { | ||
| Some(RegexSet::ArrayPrimitive) | ||
| } else if rust_bit_size().is_match(s) { | ||
| Some(RegexSet::BitSize) | ||
| } else if rust_array_decl_struct().is_match(s) { | ||
| Some(RegexSet::ArrayStruct) | ||
| } else if rust_vec_decl().is_match(s) { | ||
|
|
@@ -61,6 +64,7 @@ impl RegexSet { | |
| fn parse_type(&self, s: &str) -> Option<RustTypeMarker> { | ||
| match self { | ||
| RegexSet::ArrayPrimitive => parse_primitive_array(s), | ||
| RegexSet::BitSize => parse_bit_size(s), | ||
| RegexSet::ArrayStruct => parse_struct_array(s), | ||
| RegexSet::Vec => parse_vec(s), | ||
| RegexSet::Option => parse_option(s), | ||
|
|
@@ -90,6 +94,10 @@ fn rust_array_decl_struct() -> Regex { | |
| Regex::new(r"^\[ *?([\w><]+) *?; *?(\d+) *?\]").expect("Primitive Regex expression invalid") | ||
| } | ||
|
|
||
| pub fn rust_bit_size() -> Regex { | ||
| Regex::new(r"^(Int|UInt)<([\w\d]+), ?([\w\d]+)>").expect("Regex expression should be infallible; qed") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this regex is for I'm surprised that Why I suggest
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the capture groups are necessary for the type params, i.e. this should be fine:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's a sample of some of the input from polkadot.js: "Fixed64": "Int<64, Fixed64>",
"FixedI64": "Int<64, FixedI64>",
"FixedU64": "UInt<64, FixedU64>",
"Fixed128": "Int<128, Fixed128>",
"FixedI128": "Int<128, FixedI128>",
"FixedU128": "UInt<128, FixedU128>",
"I32F32": "Int<64, I32F32>",
"U32F32": "UInt<64, U32F32>",
"PerU16": "UInt<16, PerU16>",
"Perbill": "UInt<32, Perbill>",
"Percent": "UInt<8, Percent>",
"Permill": "UInt<32, Permill>",
"Perquintill": "UInt<64, Perquintill>",
"Balance": "UInt<128, Balance>",So the second type parameter is unnecessary, since it's already a key in our de-serialized JSON, but the first type parameter is needed to delegate the size of the type we are dealing with. This also leads me to believe that the first type parameter will always be a digit and the second just a repeat of the JSON key (I haven't found anything in the definitions that contradicts this). I'm not 100% on what the rationale of having the type repeat is in Polkadot.JS, but in the PR's I found like here they use the I agree with @niklasad1 , though, arbitrary number of spaces is definitely preferable, fixes for these/and above incoming
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All right, I see We could do |
||
| } | ||
|
|
||
| /// Match a rust vector | ||
| /// allowed to be nested within, or have other (ie Option<>) nested within | ||
| pub fn rust_vec_decl() -> Regex { | ||
|
|
@@ -354,7 +362,40 @@ fn parse_generic(s: &str) -> Option<RustTypeMarker> { | |
| // account that a HeartBeat type in Polkadot is HeartBeat<T::BlockNumber> | ||
| let ty_inner = parse(ty_inner).expect("Must be a type; qed"); | ||
|
|
||
| Some(RustTypeMarker::Generic((Box::new(ty_outer), Box::new(ty_inner)))) | ||
| Some(RustTypeMarker::Generic(Box::new(ty_outer), Box::new(ty_inner))) | ||
| } | ||
|
|
||
| fn parse_bit_size(s: &str) -> Option<RustTypeMarker> { | ||
| let re = rust_bit_size(); | ||
| if !re.is_match(s) { | ||
| return None; | ||
| } | ||
|
|
||
| let ty = re.captures(s)?.at(1)?; | ||
| let size = re.captures(s)?.at(2)?; | ||
|
|
||
| match ty { | ||
| "UInt" => match size.parse::<usize>().expect("Should always be a number") { | ||
| 8 => Some(RustTypeMarker::U8), | ||
| 16 => Some(RustTypeMarker::U16), | ||
| 32 => Some(RustTypeMarker::U32), | ||
| 64 => Some(RustTypeMarker::U64), | ||
| 128 => Some(RustTypeMarker::U128), | ||
| s => Some(RustTypeMarker::Array { size: s, ty: Box::new(RustTypeMarker::U8) }), | ||
| }, | ||
| "Int" => match size.parse::<usize>().expect("Should always be number") { | ||
| 8 => Some(RustTypeMarker::I8), | ||
| 16 => Some(RustTypeMarker::I16), | ||
| 32 => Some(RustTypeMarker::I32), | ||
| 64 => Some(RustTypeMarker::I64), | ||
| 128 => Some(RustTypeMarker::I128), | ||
| s => Some(RustTypeMarker::Array { size: s, ty: Box::new(RustTypeMarker::U8) }), | ||
| }, | ||
| _ => { | ||
| log::warn!("Could not ascertain type of bit-size declaration"); | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// recursively parses a regex set | ||
|
|
@@ -674,7 +715,7 @@ mod tests { | |
| let re = rust_generic_decl(); | ||
| let caps = re.captures("GenericOuterType<GenericInnerType>").unwrap(); | ||
| assert_eq!( | ||
| vec![Some("GenericOuterType<GenericInnerType>"), Some("GenericOuterType"), Some("GenericInnerType")], | ||
| vec![Some("GenericOuterType<GenericInnerType>"), Some("GenericOuterType"), Some("GenericInnerType"),], | ||
| caps.iter().collect::<Vec<Option<&str>>>() | ||
| ); | ||
| } | ||
|
|
@@ -829,4 +870,13 @@ mod tests { | |
| let res = parse_vec(ty).unwrap(); | ||
| log::debug!("{:?}", res); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_parse_bit_size() { | ||
| pretty_env_logger::try_init(); | ||
| let ty = "UInt<128, Balance>"; | ||
| assert_eq!(parse_bit_size(ty).unwrap(), RustTypeMarker::U128); | ||
| let ty = "Int<64, Balance>"; | ||
| assert_eq!(parse_bit_size(ty).unwrap(), RustTypeMarker::I64); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.