use std::any::Any;
use std::fmt;
use ciborium::Value as CborValue;
use serde::Serialize;
use vantage_types::Record;
mod listing;
#[derive(Clone, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct Page {
pub rows: Vec<(String, Record<CborValue>)>,
pub total: Option<i64>,
}
impl Page {
pub fn new(rows: Vec<(String, Record<CborValue>)>) -> Self {
Self { rows, total: None }
}
pub fn with_total(mut self, total: i64) -> Self {
self.total = Some(total);
self
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[non_exhaustive]
pub struct Preview {
pub query: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
}
impl Preview {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
query: None,
note: None,
}
}
pub fn with_query(mut self, query: impl Into<String>) -> Self {
self.query = Some(query.into());
self
}
pub fn with_note(mut self, note: impl Into<String>) -> Self {
self.note = Some(note.into());
self
}
}
pub struct NativeCondition {
condition: Box<dyn Any + Send + Sync>,
key: String,
}
impl NativeCondition {
pub fn new<C: Any + Send + Sync>(condition: C, key: impl Into<String>) -> Self {
Self {
condition: Box::new(condition),
key: key.into(),
}
}
pub fn key(&self) -> &str {
&self.key
}
pub fn downcast<C: Any>(self) -> Result<C, Self> {
let Self { condition, key } = self;
condition
.downcast::<C>()
.map(|c| *c)
.map_err(|condition| Self { condition, key })
}
}
impl fmt::Debug for NativeCondition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("NativeCondition").field(&self.key).finish()
}
}