use crate::core::errors::ConduitError;
use crate::tape::entries::TapeEntry;
use crate::tape::store::{AsyncTapeStore, TapeStore};
#[derive(Debug, Clone)]
pub struct TapeQuery {
pub tape: String,
pub query_text: Option<String>,
pub after_anchor: Option<String>,
pub after_last: bool,
pub between_anchors: Option<(String, String)>,
pub between_dates: Option<(String, String)>,
pub kinds: Vec<String>,
pub limit: Option<usize>,
}
impl TapeQuery {
pub fn new(tape: impl Into<String>) -> Self {
Self {
tape: tape.into(),
query_text: None,
after_anchor: None,
after_last: false,
between_anchors: None,
between_dates: None,
kinds: Vec::new(),
limit: None,
}
}
pub fn query(mut self, value: impl Into<String>) -> Self {
self.query_text = Some(value.into());
self
}
pub fn after_anchor(mut self, name: impl Into<String>) -> Self {
let name = name.into();
if name.is_empty() {
self.after_anchor = None;
self.after_last = false;
} else {
self.after_anchor = Some(name);
self.after_last = false;
}
self
}
pub fn last_anchor(mut self) -> Self {
self.after_anchor = None;
self.after_last = true;
self
}
pub fn between_anchors(mut self, start: impl Into<String>, end: impl Into<String>) -> Self {
self.between_anchors = Some((start.into(), end.into()));
self
}
pub fn between_dates(mut self, start: impl Into<String>, end: impl Into<String>) -> Self {
self.between_dates = Some((start.into(), end.into()));
self
}
pub fn kinds(mut self, kinds: Vec<String>) -> Self {
self.kinds = kinds;
self
}
pub fn limit(mut self, value: usize) -> Self {
self.limit = Some(value);
self
}
pub fn all_sync(&self, store: &dyn TapeStore) -> Result<Vec<TapeEntry>, ConduitError> {
store.fetch_all(self)
}
pub async fn all_async(
&self,
store: &dyn AsyncTapeStore,
) -> Result<Vec<TapeEntry>, ConduitError> {
store.fetch_all(self).await
}
}