Skip to content

Commit

Permalink
Add range getters, combine start and end types (#126)
Browse files Browse the repository at this point in the history
* Add getters and combine start and end types

* Fix tests
  • Loading branch information
ascjones authored Aug 25, 2021
1 parent 15f4e60 commit 56d24a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
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

0 comments on commit 56d24a2

Please sign in to comment.