datafusion_common/
parsers.rs1use std::fmt::Display;
8use std::str::FromStr;
9
10use crate::DataFusionError;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub enum CompressionTypeVariant {
15 GZIP,
17 BZIP2,
19 XZ,
21 ZSTD,
23 UNCOMPRESSED,
25}
26
27impl FromStr for CompressionTypeVariant {
28 type Err = DataFusionError;
29
30 fn from_str(s: &str) -> Result<Self, Self::Err> {
31 let s = s.to_uppercase();
32 match s.as_str() {
33 "GZIP" | "GZ" => Ok(Self::GZIP),
34 "BZIP2" | "BZ2" => Ok(Self::BZIP2),
35 "XZ" => Ok(Self::XZ),
36 "ZST" | "ZSTD" => Ok(Self::ZSTD),
37 "" | "UNCOMPRESSED" => Ok(Self::UNCOMPRESSED),
38 _ => Err(DataFusionError::NotImplemented(format!(
39 "Unsupported file compression type {s}"
40 ))),
41 }
42 }
43}
44
45impl Display for CompressionTypeVariant {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 let str = match self {
48 Self::GZIP => "GZIP",
49 Self::BZIP2 => "BZIP2",
50 Self::XZ => "XZ",
51 Self::ZSTD => "ZSTD",
52 Self::UNCOMPRESSED => "",
53 };
54 write!(f, "{str}")
55 }
56}
57
58impl CompressionTypeVariant {
59 pub const fn is_compressed(&self) -> bool {
60 !matches!(self, &Self::UNCOMPRESSED)
61 }
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
69pub enum CsvQuoteStyle {
70 Always,
72 #[default]
74 Necessary,
75 NonNumeric,
77 Never,
79}
80
81impl FromStr for CsvQuoteStyle {
82 type Err = DataFusionError;
83
84 fn from_str(s: &str) -> Result<Self, Self::Err> {
85 match s.to_lowercase().as_str() {
86 "always" => Ok(Self::Always),
87 "necessary" => Ok(Self::Necessary),
88 "non_numeric" | "nonnumeric" => Ok(Self::NonNumeric),
89 "never" => Ok(Self::Never),
90 _ => Err(DataFusionError::NotImplemented(format!(
91 "Unsupported CSV quote style {s}"
92 ))),
93 }
94 }
95}
96
97impl From<CsvQuoteStyle> for arrow::csv::QuoteStyle {
98 fn from(style: CsvQuoteStyle) -> Self {
99 match style {
100 CsvQuoteStyle::Always => Self::Always,
101 CsvQuoteStyle::NonNumeric => Self::NonNumeric,
102 CsvQuoteStyle::Never => Self::Never,
103 CsvQuoteStyle::Necessary => Self::Necessary,
104 }
105 }
106}
107
108impl Display for CsvQuoteStyle {
109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110 let str = match self {
111 Self::Always => "Always",
112 Self::Necessary => "Necessary",
113 Self::NonNumeric => "NonNumeric",
114 Self::Never => "Never",
115 };
116 write!(f, "{str}")
117 }
118}