datafusion_common/
parsers.rs1use std::fmt::Display;
21use std::str::FromStr;
22
23use crate::DataFusionError;
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27pub enum CompressionTypeVariant {
28 GZIP,
30 BZIP2,
32 XZ,
34 ZSTD,
36 UNCOMPRESSED,
38}
39
40impl FromStr for CompressionTypeVariant {
41 type Err = DataFusionError;
42
43 fn from_str(s: &str) -> Result<Self, Self::Err> {
44 let s = s.to_uppercase();
45 match s.as_str() {
46 "GZIP" | "GZ" => Ok(Self::GZIP),
47 "BZIP2" | "BZ2" => Ok(Self::BZIP2),
48 "XZ" => Ok(Self::XZ),
49 "ZST" | "ZSTD" => Ok(Self::ZSTD),
50 "" | "UNCOMPRESSED" => Ok(Self::UNCOMPRESSED),
51 _ => Err(DataFusionError::NotImplemented(format!(
52 "Unsupported file compression type {s}"
53 ))),
54 }
55 }
56}
57
58impl Display for CompressionTypeVariant {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 let str = match self {
61 Self::GZIP => "GZIP",
62 Self::BZIP2 => "BZIP2",
63 Self::XZ => "XZ",
64 Self::ZSTD => "ZSTD",
65 Self::UNCOMPRESSED => "",
66 };
67 write!(f, "{str}")
68 }
69}
70
71impl CompressionTypeVariant {
72 pub const fn is_compressed(&self) -> bool {
73 !matches!(self, &Self::UNCOMPRESSED)
74 }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
82pub enum CsvQuoteStyle {
83 Always,
85 #[default]
87 Necessary,
88 NonNumeric,
90 Never,
92}
93
94impl FromStr for CsvQuoteStyle {
95 type Err = DataFusionError;
96
97 fn from_str(s: &str) -> Result<Self, Self::Err> {
98 match s.to_lowercase().as_str() {
99 "always" => Ok(Self::Always),
100 "necessary" => Ok(Self::Necessary),
101 "non_numeric" | "nonnumeric" => Ok(Self::NonNumeric),
102 "never" => Ok(Self::Never),
103 _ => Err(DataFusionError::NotImplemented(format!(
104 "Unsupported CSV quote style {s}"
105 ))),
106 }
107 }
108}
109
110impl From<CsvQuoteStyle> for arrow::csv::QuoteStyle {
111 fn from(style: CsvQuoteStyle) -> Self {
112 match style {
113 CsvQuoteStyle::Always => Self::Always,
114 CsvQuoteStyle::NonNumeric => Self::NonNumeric,
115 CsvQuoteStyle::Never => Self::Never,
116 CsvQuoteStyle::Necessary => Self::Necessary,
117 }
118 }
119}
120
121impl Display for CsvQuoteStyle {
122 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123 let str = match self {
124 Self::Always => "Always",
125 Self::Necessary => "Necessary",
126 Self::NonNumeric => "NonNumeric",
127 Self::Never => "Never",
128 };
129 write!(f, "{str}")
130 }
131}