Skip to main content

datafusion_common/
parsers.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Interval parsing logic
6
7use std::fmt::Display;
8use std::str::FromStr;
9
10use crate::DataFusionError;
11
12/// Readable file compression type
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub enum CompressionTypeVariant {
15    /// Gzip-ed file
16    GZIP,
17    /// Bzip2-ed file
18    BZIP2,
19    /// Xz-ed file (liblzma)
20    XZ,
21    /// Zstd-ed file,
22    ZSTD,
23    /// Uncompressed file
24    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/// CSV quote style
65///
66/// Controls when fields are quoted when writing CSV files.
67/// Corresponds to [`arrow::csv::QuoteStyle`].
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
69pub enum CsvQuoteStyle {
70    /// Quote all fields
71    Always,
72    /// Only quote fields when necessary (default)
73    #[default]
74    Necessary,
75    /// Quote all non-numeric fields
76    NonNumeric,
77    /// Never quote fields
78    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}