#[derive(Debug, Clone, PartialEq)]
pub struct ExportOptions {
pub selection: ExportSelection,
pub contents: ExportContents,
pub insert_batch_size: usize,
pub if_not_exists: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ExportSelection {
All,
Namespaces(Vec<String>),
Objects(Vec<QualifiedObject>),
Kinds(Vec<ObjectKind>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct QualifiedObject {
pub namespace: String,
pub name: String,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ExportContents {
SchemaAndData,
SchemaOnly,
DataOnly,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ObjectKind {
Table,
Queue,
RingBuffer,
Series,
Dictionary,
Enum,
}
pub const DEFAULT_INSERT_BATCH_SIZE: usize = 500;
impl ExportOptions {
pub fn all() -> Self {
Self {
selection: ExportSelection::All,
contents: ExportContents::SchemaAndData,
insert_batch_size: DEFAULT_INSERT_BATCH_SIZE,
if_not_exists: false,
}
}
pub fn namespace(mut self, name: impl Into<String>) -> Self {
let name = name.into();
match &mut self.selection {
ExportSelection::Namespaces(names) => names.push(name),
_ => self.selection = ExportSelection::Namespaces(vec![name]),
}
self
}
pub fn object(mut self, namespace: impl Into<String>, name: impl Into<String>) -> Self {
let object = QualifiedObject {
namespace: namespace.into(),
name: name.into(),
};
match &mut self.selection {
ExportSelection::Objects(objects) => objects.push(object),
_ => self.selection = ExportSelection::Objects(vec![object]),
}
self
}
pub fn kind(mut self, kind: ObjectKind) -> Self {
match &mut self.selection {
ExportSelection::Kinds(kinds) => {
if !kinds.contains(&kind) {
kinds.push(kind);
}
}
_ => self.selection = ExportSelection::Kinds(vec![kind]),
}
self
}
pub fn schema_only(mut self) -> Self {
self.contents = ExportContents::SchemaOnly;
self
}
pub fn data_only(mut self) -> Self {
self.contents = ExportContents::DataOnly;
self
}
pub fn batch_size(mut self, size: usize) -> Self {
self.insert_batch_size = size.max(1);
self
}
pub fn if_not_exists(mut self, enabled: bool) -> Self {
self.if_not_exists = enabled;
self
}
pub fn includes_schema(&self) -> bool {
!matches!(self.contents, ExportContents::DataOnly)
}
pub fn includes_data(&self) -> bool {
!matches!(self.contents, ExportContents::SchemaOnly)
}
}
impl Default for ExportOptions {
fn default() -> Self {
Self::all()
}
}