use crate::name::{EventType, Tags};
use crate::position::Position;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct QueryItem {
pub types: Vec<EventType>,
pub tags: Tags,
}
impl QueryItem {
pub fn new(types: Vec<EventType>, tags: Tags) -> Self {
QueryItem { types, tags }
}
pub fn of_types(types: Vec<EventType>) -> Self {
QueryItem {
types,
tags: Tags::empty(),
}
}
pub fn with_tags(tags: Tags) -> Self {
QueryItem {
types: Vec::new(),
tags,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Query {
All,
Items(Vec<QueryItem>),
}
impl Query {
pub fn all() -> Self {
Query::All
}
pub fn items(items: impl Into<Vec<QueryItem>>) -> Self {
Query::Items(items.into())
}
pub fn item(item: QueryItem) -> Self {
Query::Items(vec![item])
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AppendCondition {
pub fail_if_events_match: Query,
pub after: Position,
}
impl AppendCondition {
pub fn new(fail_if_events_match: Query) -> Self {
AppendCondition {
fail_if_events_match,
after: Position::ZERO,
}
}
pub fn after(mut self, after: Position) -> Self {
self.after = after;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn item_constructors() {
assert!(QueryItem::of_types(Vec::new()).tags.is_empty());
assert!(QueryItem::with_tags(Tags::empty()).types.is_empty());
assert_eq!(
QueryItem::default(),
QueryItem::new(Vec::new(), Tags::empty())
);
}
#[test]
fn query_constructors() {
assert_eq!(Query::all(), Query::All);
assert_eq!(Query::items(Vec::new()), Query::Items(Vec::new()));
assert_eq!(
Query::item(QueryItem::default()),
Query::Items(vec![QueryItem::default()])
);
}
#[test]
fn condition_defaults_to_position_zero() {
let cond = AppendCondition::new(Query::all());
assert_eq!(cond.after, Position::ZERO);
assert_eq!(cond.fail_if_events_match, Query::All);
}
#[test]
fn condition_after_sets_bound() {
let cond = AppendCondition::new(Query::all()).after(Position::new(42));
assert_eq!(cond.after, Position::new(42));
}
}