Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rust!): Rename to CsvParserOptions to CsvReaderOptions, use in CsvReader #15919

Merged
merged 7 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/polars-io/src/csv/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod reader;
mod splitfields;
mod utils;

pub use options::{CommentPrefix, CsvEncoding, CsvParserOptions, NullValues};
pub use options::{CommentPrefix, CsvEncoding, CsvReaderOptions, NullValues};
pub use parser::count_rows;
pub use read_impl::batched_mmap::{BatchedCsvReaderMmap, OwnedBatchedCsvReaderMmap};
pub use read_impl::batched_read::{BatchedCsvReaderRead, OwnedBatchedCsvReader};
Expand Down
29 changes: 17 additions & 12 deletions crates/polars-io/src/csv/read/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CsvParserOptions {
pub struct CsvReaderOptions {
pub has_header: bool,
pub separator: u8,
pub comment_prefix: Option<CommentPrefix>,
pub quote_char: Option<u8>,
pub comment_prefix: Option<CommentPrefix>,
pub eol_char: u8,
pub encoding: CsvEncoding,
pub skip_rows: usize,
Expand All @@ -27,13 +27,13 @@ pub struct CsvParserOptions {
pub low_memory: bool,
}

impl Default for CsvParserOptions {
impl Default for CsvReaderOptions {
fn default() -> Self {
Self {
has_header: true,
separator: b',',
comment_prefix: None,
quote_char: Some(b'"'),
comment_prefix: None,
eol_char: b'\n',
encoding: CsvEncoding::default(),
skip_rows: 0,
Expand Down Expand Up @@ -75,17 +75,22 @@ pub enum CommentPrefix {

impl CommentPrefix {
/// Creates a new `CommentPrefix` for the `Single` variant.
pub fn new_single(c: u8) -> Self {
CommentPrefix::Single(c)
pub fn new_single(prefix: u8) -> Self {
CommentPrefix::Single(prefix)
}

/// Creates a new `CommentPrefix` for the `Multi` variant.
pub fn new_multi(prefix: String) -> Self {
CommentPrefix::Multi(prefix)
}

/// Creates a new `CommentPrefix`. If `Multi` variant is used and the string is longer
/// than 5 characters, it will return `None`.
pub fn new_multi(s: String) -> Option<Self> {
if s.len() <= 5 {
Some(CommentPrefix::Multi(s))
/// Creates a new `CommentPrefix` from a `&str`.
pub fn new_from_str(prefix: &str) -> Self {
if prefix.len() == 1 && prefix.chars().next().unwrap().is_ascii() {
let c = prefix.as_bytes()[0];
CommentPrefix::Single(c)
} else {
None
CommentPrefix::Multi(prefix.to_string())
}
}
}
Copy link
Member Author

@stinodego stinodego Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the arbitrary 5 character limit here. Not sure why it was there in the first place 🤔 the original PR/issue do not mention anything about the limit: #12519

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API mentions the 5 character limit here, so that should probably updated too.

Copy link
Member Author

@stinodego stinodego Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, updated. Thanks!

Expand Down
Loading
Loading