use crate::error::{DataFusionError, Result};
use core::fmt;
use std::fmt::Display;
use std::str::FromStr;
pub const DEFAULT_ARROW_EXTENSION: &str = ".arrow";
pub const DEFAULT_AVRO_EXTENSION: &str = ".avro";
pub const DEFAULT_CSV_EXTENSION: &str = ".csv";
pub const DEFAULT_JSON_EXTENSION: &str = ".json";
pub const DEFAULT_PARQUET_EXTENSION: &str = ".parquet";
pub trait GetExt {
fn get_ext(&self) -> String;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FileType {
ARROW,
AVRO,
#[cfg(feature = "parquet")]
PARQUET,
CSV,
JSON,
}
impl GetExt for FileType {
fn get_ext(&self) -> String {
match self {
FileType::ARROW => DEFAULT_ARROW_EXTENSION.to_owned(),
FileType::AVRO => DEFAULT_AVRO_EXTENSION.to_owned(),
#[cfg(feature = "parquet")]
FileType::PARQUET => DEFAULT_PARQUET_EXTENSION.to_owned(),
FileType::CSV => DEFAULT_CSV_EXTENSION.to_owned(),
FileType::JSON => DEFAULT_JSON_EXTENSION.to_owned(),
}
}
}
impl Display for FileType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let out = match self {
FileType::CSV => "csv",
FileType::JSON => "json",
#[cfg(feature = "parquet")]
FileType::PARQUET => "parquet",
FileType::AVRO => "avro",
FileType::ARROW => "arrow",
};
write!(f, "{}", out)
}
}
impl FromStr for FileType {
type Err = DataFusionError;
fn from_str(s: &str) -> Result<Self> {
let s = s.to_uppercase();
match s.as_str() {
"ARROW" => Ok(FileType::ARROW),
"AVRO" => Ok(FileType::AVRO),
#[cfg(feature = "parquet")]
"PARQUET" => Ok(FileType::PARQUET),
"CSV" => Ok(FileType::CSV),
"JSON" | "NDJSON" => Ok(FileType::JSON),
_ => Err(DataFusionError::NotImplemented(format!(
"Unknown FileType: {s}"
))),
}
}
}
#[cfg(test)]
mod tests {
use crate::error::DataFusionError;
use crate::file_options::FileType;
use std::str::FromStr;
#[test]
fn from_str() {
for (ext, file_type) in [
("csv", FileType::CSV),
("CSV", FileType::CSV),
("json", FileType::JSON),
("JSON", FileType::JSON),
("avro", FileType::AVRO),
("AVRO", FileType::AVRO),
("parquet", FileType::PARQUET),
("PARQUET", FileType::PARQUET),
] {
assert_eq!(FileType::from_str(ext).unwrap(), file_type);
}
assert!(matches!(
FileType::from_str("Unknown"),
Err(DataFusionError::NotImplemented(_))
));
}
}