Skip to content
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

Add range getters, combine start and end types #126

Merged
merged 2 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,7 @@ where
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)]
pub struct TypeDefRange<T: Form = MetaForm> {
start: T::Type,
end: T::Type,
index_type: T::Type,
inclusive: bool,
}

Expand All @@ -441,20 +440,33 @@ impl TypeDefRange {
Idx: PartialOrd + fmt::Debug + TypeInfo + 'static,
{
Self {
start: MetaType::new::<Idx>(),
end: MetaType::new::<Idx>(),
index_type: MetaType::new::<Idx>(),
inclusive,
}
}
}

impl<T> TypeDefRange<T>
where
T: Form,
{
/// Returns the type of the index of the range.
pub fn index_type(&self) -> &T::Type {
&self.index_type
}

/// Returns true if the range is inclusive as with [`core::ops::RangeInclusive`].
pub fn inclusive(&self) -> bool {
self.inclusive
}
}

impl IntoPortable for TypeDefRange {
type Output = TypeDefRange<PortableForm>;

fn into_portable(self, registry: &mut Registry) -> Self::Output {
TypeDefRange {
start: registry.register_type(&self.start),
end: registry.register_type(&self.end),
index_type: registry.register_type(&self.index_type),
inclusive: self.inclusive,
}
}
Expand Down
4 changes: 2 additions & 2 deletions test_suite/tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ fn test_ranges() {
{
"id": 1,
"type": {
"def": { "range": { "start": 2, "end": 2, "inclusive": false} },
"def": { "range": { "index_type": 2, "inclusive": false} },
}
},
{
Expand All @@ -406,7 +406,7 @@ fn test_ranges() {
{
"id": 3,
"type": {
"def": { "range": { "start": 4, "end": 4, "inclusive": true} },
"def": { "range": { "index_type": 4, "inclusive": true} },
}
},
{
Expand Down