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

tendermint-rpc: Improve Query API #1229

Merged
merged 5 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions .changelog/unreleased/improvements/1230-query-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [`tendermint-rpc`] Make `tendermint_rpc::Query`'s fields
romac marked this conversation as resolved.
Show resolved Hide resolved
public and add a `Condition::key(&self) -> &str` method
([#1230](https://github.com/informalsystems/tendermint-rs/issues/1230))
33 changes: 24 additions & 9 deletions rpc/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ use crate::{prelude::*, Error};
#[derive(Debug, Clone, PartialEq)]
pub struct Query {
// We can only have at most one event type at present in a query.
event_type: Option<EventType>,
pub event_type: Option<EventType>,
// We can have zero or more additional conditions associated with a query.
// Conditions are currently exclusively joined by logical ANDs.
conditions: Vec<Condition>,
pub conditions: Vec<Condition>,
}

impl Query {
Expand Down Expand Up @@ -442,16 +442,31 @@ pub enum Condition {
Exists(String),
}

impl Condition {
/// Return the key that this condition applies to
pub fn key(&self) -> &str {
thanethomson marked this conversation as resolved.
Show resolved Hide resolved
match self {
Self::Eq(key, _) => key,
Self::Lt(key, _) => key,
Self::Lte(key, _) => key,
Self::Gt(key, _) => key,
Self::Gte(key, _) => key,
Self::Contains(key, _) => key,
Self::Exists(key) => key,
}
}
}

impl fmt::Display for Condition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Condition::Eq(key, op) => write!(f, "{} = {}", key, op),
Condition::Lt(key, op) => write!(f, "{} < {}", key, op),
Condition::Lte(key, op) => write!(f, "{} <= {}", key, op),
Condition::Gt(key, op) => write!(f, "{} > {}", key, op),
Condition::Gte(key, op) => write!(f, "{} >= {}", key, op),
Condition::Contains(key, op) => write!(f, "{} CONTAINS {}", key, escape(op)),
Condition::Exists(key) => write!(f, "{} EXISTS", key),
Self::Eq(key, op) => write!(f, "{} = {}", key, op),
Self::Lt(key, op) => write!(f, "{} < {}", key, op),
Self::Lte(key, op) => write!(f, "{} <= {}", key, op),
Self::Gt(key, op) => write!(f, "{} > {}", key, op),
Self::Gte(key, op) => write!(f, "{} >= {}", key, op),
Self::Contains(key, op) => write!(f, "{} CONTAINS {}", key, escape(op)),
Self::Exists(key) => write!(f, "{} EXISTS", key),
}
}
}
Expand Down