use std::fmt::{self, Display};
use std::str::FromStr;
use crate::config::{ConfigField, Visit};
use crate::error::{DataFusionError, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DFParquetWriterVersion {
#[default]
V1_0,
V2_0,
}
impl FromStr for DFParquetWriterVersion {
type Err = DataFusionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"1.0" => Ok(DFParquetWriterVersion::V1_0),
"2.0" => Ok(DFParquetWriterVersion::V2_0),
other => Err(DataFusionError::Configuration(format!(
"Invalid parquet writer version: {other}. Expected one of: 1.0, 2.0"
))),
}
}
}
impl Display for DFParquetWriterVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
DFParquetWriterVersion::V1_0 => "1.0",
DFParquetWriterVersion::V2_0 => "2.0",
};
write!(f, "{s}")
}
}
impl ConfigField for DFParquetWriterVersion {
fn visit<V: Visit>(&self, v: &mut V, key: &str, description: &'static str) {
v.some(key, self, description)
}
fn set(&mut self, _: &str, value: &str) -> Result<()> {
*self = DFParquetWriterVersion::from_str(value)?;
Ok(())
}
}
#[cfg(feature = "parquet")]
impl From<DFParquetWriterVersion> for parquet::file::properties::WriterVersion {
fn from(value: DFParquetWriterVersion) -> Self {
match value {
DFParquetWriterVersion::V1_0 => {
parquet::file::properties::WriterVersion::PARQUET_1_0
}
DFParquetWriterVersion::V2_0 => {
parquet::file::properties::WriterVersion::PARQUET_2_0
}
}
}
}
#[cfg(feature = "parquet")]
impl From<parquet::file::properties::WriterVersion> for DFParquetWriterVersion {
fn from(version: parquet::file::properties::WriterVersion) -> Self {
match version {
parquet::file::properties::WriterVersion::PARQUET_1_0 => {
DFParquetWriterVersion::V1_0
}
parquet::file::properties::WriterVersion::PARQUET_2_0 => {
DFParquetWriterVersion::V2_0
}
}
}
}