Skip to content

Commit

Permalink
Merge pull request #128 from paritytech/topic_index
Browse files Browse the repository at this point in the history
add `fn is_empty()` and implement `std::ops::Index` for Topic
  • Loading branch information
debris authored Oct 16, 2018
2 parents c0674c3 + 3108846 commit e806bd8
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions ethabi/src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ops;
use serde::{Serialize, Serializer};
use serde_json::Value;
use hex::ToHex;
Expand Down Expand Up @@ -54,6 +55,14 @@ impl<T> Topic<T> {
Topic::This(topic) => Topic::This(f(topic)),
}
}

/// Returns true if topic is empty (Topic::Any)
pub fn is_any(&self) -> bool {
match *self {
Topic::Any => true,
Topic::This(_) | Topic::OneOf(_) => false,
}
}
}

impl<T> Default for Topic<T> {
Expand Down Expand Up @@ -111,6 +120,23 @@ impl Serialize for Topic<Hash> {
}
}

impl<T> ops::Index<usize> for Topic<T> {
type Output = T;

fn index(&self, index: usize) -> &Self::Output {
match *self {
Topic::Any => panic!("Topic unavailable"),
Topic::This(ref topic) => {
if index != 0 {
panic!("Topic unavailable");
}
topic
},
Topic::OneOf(ref topics) => topics.index(index),
}
}
}

#[cfg(test)]
mod tests {
use serde_json;
Expand Down Expand Up @@ -156,4 +182,30 @@ r#"["0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",null,["
let is: Vec<u64> = Topic::OneOf(vec![10u64, 20]).into();
assert_eq!(expected, is);
}

#[test]
fn test_topic_is_any() {
assert!((Topic::Any as Topic<u8>).is_any());
assert!(!Topic::OneOf(vec![10u64, 20]).is_any());
assert!(!Topic::This(10u64).is_any());
}

#[test]
fn test_topic_index() {
assert_eq!(Topic::OneOf(vec![10u64, 20])[0], 10);
assert_eq!(Topic::OneOf(vec![10u64, 20])[1], 20);
assert_eq!(Topic::This(10u64)[0], 10);
}

#[test]
#[should_panic(expected = "Topic unavailable")]
fn test_topic_index_panic() {
let _ = (Topic::Any as Topic<u8>)[0];
}

#[test]
#[should_panic(expected = "Topic unavailable")]
fn test_topic_index_panic2() {
assert_eq!(Topic::This(10u64)[1], 10);
}
}

0 comments on commit e806bd8

Please sign in to comment.