polars_io/csv/write/
options.rs

1use std::num::NonZeroUsize;
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// Options for writing CSV files.
7#[derive(Clone, Debug, Eq, Hash, PartialEq)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub struct CsvWriterOptions {
10    pub include_bom: bool,
11    pub include_header: bool,
12    pub batch_size: NonZeroUsize,
13    pub serialize_options: SerializeOptions,
14}
15
16impl Default for CsvWriterOptions {
17    fn default() -> Self {
18        Self {
19            include_bom: false,
20            include_header: true,
21            batch_size: NonZeroUsize::new(1024).unwrap(),
22            serialize_options: SerializeOptions::default(),
23        }
24    }
25}
26
27/// Options to serialize logical types to CSV.
28///
29/// The default is to format times and dates as `chrono` crate formats them.
30#[derive(Clone, Debug, Eq, Hash, PartialEq)]
31#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
32pub struct SerializeOptions {
33    /// Used for [`DataType::Date`](polars_core::datatypes::DataType::Date).
34    pub date_format: Option<String>,
35    /// Used for [`DataType::Time`](polars_core::datatypes::DataType::Time).
36    pub time_format: Option<String>,
37    /// Used for [`DataType::Datetime`](polars_core::datatypes::DataType::Datetime).
38    pub datetime_format: Option<String>,
39    /// Used for [`DataType::Float64`](polars_core::datatypes::DataType::Float64)
40    /// and [`DataType::Float32`](polars_core::datatypes::DataType::Float32).
41    pub float_scientific: Option<bool>,
42    pub float_precision: Option<usize>,
43    /// Used as separator.
44    pub separator: u8,
45    /// Quoting character.
46    pub quote_char: u8,
47    /// Null value representation.
48    pub null: String,
49    /// String appended after every row.
50    pub line_terminator: String,
51    /// When to insert quotes.
52    pub quote_style: QuoteStyle,
53}
54
55impl Default for SerializeOptions {
56    fn default() -> Self {
57        Self {
58            date_format: None,
59            time_format: None,
60            datetime_format: None,
61            float_scientific: None,
62            float_precision: None,
63            separator: b',',
64            quote_char: b'"',
65            null: String::new(),
66            line_terminator: "\n".into(),
67            quote_style: Default::default(),
68        }
69    }
70}
71
72/// Quote style indicating when to insert quotes around a field.
73#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq)]
74#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
75pub enum QuoteStyle {
76    /// Quote fields only when necessary.
77    ///
78    /// Quotes are necessary when fields contain a quote, separator or record terminator.
79    /// Quotes are also necessary when writing an empty record (which is indistinguishable
80    /// from arecord with one empty field).
81    /// This is the default.
82    #[default]
83    Necessary,
84    /// Quote every field. Always.
85    Always,
86    /// Quote non-numeric fields.
87    ///
88    /// When writing a field that does not parse as a valid float or integer,
89    /// quotes will be used even if they aren't strictly necessary.
90    NonNumeric,
91    /// Never quote any fields, even if it would produce invalid CSV data.
92    Never,
93}