use crate::error::Result;
use crate::spec::{PartitionSpec, Schema};
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct TableCreation {
name: String,
schema: Schema,
location: Option<String>,
properties: HashMap<String, String>,
partition_spec: Option<PartitionSpec>,
}
impl TableCreation {
pub fn builder() -> TableCreationBuilder {
TableCreationBuilder::default()
}
pub fn name(&self) -> &str {
&self.name
}
pub fn schema(&self) -> &Schema {
&self.schema
}
pub fn location(&self) -> Option<&str> {
self.location.as_deref()
}
pub fn properties(&self) -> &HashMap<String, String> {
&self.properties
}
pub fn partition_spec(&self) -> Option<&PartitionSpec> {
self.partition_spec.as_ref()
}
}
#[derive(Default)]
pub struct TableCreationBuilder {
name: Option<String>,
schema: Option<Schema>,
location: Option<String>,
properties: HashMap<String, String>,
partition_spec: Option<PartitionSpec>,
}
impl TableCreationBuilder {
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn with_schema(mut self, schema: Schema) -> Self {
self.schema = Some(schema);
self
}
pub fn with_location(mut self, location: impl Into<String>) -> Self {
self.location = Some(location.into());
self
}
pub fn with_property(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.properties.insert(key.into(), value.into());
self
}
pub fn with_partition_spec(mut self, spec: PartitionSpec) -> Self {
self.partition_spec = Some(spec);
self
}
pub fn build(self) -> Result<TableCreation> {
let name = self
.name
.ok_or_else(|| crate::error::Error::invalid_input("Table name is required"))?;
let schema = self
.schema
.ok_or_else(|| crate::error::Error::invalid_input("Schema is required"))?;
Ok(TableCreation {
name,
schema,
location: self.location,
properties: self.properties,
partition_spec: self.partition_spec,
})
}
}