use std::collections::BTreeMap;
use relate_core::{Error, Result, Value};
#[derive(Default)]
struct QuoteScan {
in_quotes: bool,
field_started: bool,
}
enum QuoteEvent {
Data(char),
EnterQuotes,
ExitQuotes,
EscapedQuote,
FieldEnd,
}
impl QuoteScan {
fn advance(&mut self, c: char, next_is_quote: bool, delim: char) -> QuoteEvent {
if self.in_quotes {
if c == '"' {
if next_is_quote {
return QuoteEvent::EscapedQuote;
}
self.in_quotes = false;
return QuoteEvent::ExitQuotes;
}
return QuoteEvent::Data(c);
}
if c == '"' && !self.field_started {
self.in_quotes = true;
self.field_started = true;
return QuoteEvent::EnterQuotes;
}
if c == delim {
self.field_started = false;
return QuoteEvent::FieldEnd;
}
self.field_started = true;
QuoteEvent::Data(c)
}
fn in_quotes(&self) -> bool {
self.in_quotes
}
}
pub(crate) struct RecordCursor<S> {
text: S,
pos: usize,
}
fn cr_stripped_end(text: &str, break_at: usize, floor: usize) -> usize {
if break_at > floor && text.as_bytes()[break_at - 1] == b'\r' {
break_at - 1
} else {
break_at
}
}
impl<S: AsRef<str>> RecordCursor<S> {
pub(crate) fn new(text: S) -> Self {
Self { text, pos: 0 }
}
pub(crate) fn next_record(&mut self, delim: char) -> Option<&str> {
let text = self.text.as_ref();
let (start, end, next) = Self::next_range(text, self.pos, delim)?;
self.pos = next;
Some(&text[start..end])
}
pub(crate) fn remaining_records(&self, delim: char, keep: impl Fn(&str) -> bool) -> usize {
let text = self.text.as_ref();
let mut count = 0;
let mut pos = self.pos;
while let Some((start, end, next)) = Self::next_range(text, pos, delim) {
if keep(&text[start..end]) {
count += 1;
}
pos = next;
}
count
}
fn next_range(text: &str, pos: usize, delim: char) -> Option<(usize, usize, usize)> {
if pos >= text.len() {
return None;
}
if delim == '\n' {
return match text[pos..].find('\n') {
Some(i) => {
let break_at = pos + i;
Some((pos, cr_stripped_end(text, break_at, pos), break_at + 1))
}
None => Some((pos, text.len(), text.len())),
};
}
let mut scan = QuoteScan::default();
let mut iter = text[pos..].char_indices().peekable();
while let Some((offset, c)) = iter.next() {
let i = pos + offset;
match scan.advance(c, iter.peek().is_some_and(|&(_, next)| next == '"'), delim) {
QuoteEvent::EscapedQuote => {
iter.next();
}
QuoteEvent::Data('\n') if !scan.in_quotes() => {
return Some((pos, cr_stripped_end(text, i, pos), i + 1));
}
QuoteEvent::Data(_)
| QuoteEvent::EnterQuotes
| QuoteEvent::ExitQuotes
| QuoteEvent::FieldEnd => {}
}
}
Some((pos, cr_stripped_end(text, text.len(), pos), text.len()))
}
}
pub(crate) fn split_csv_line(line: &str, delim: char) -> Vec<String> {
let mut cells = Vec::new();
let mut cur = String::new();
let mut scan = QuoteScan::default();
let mut chars = line.chars().peekable();
while let Some(c) = chars.next() {
match scan.advance(c, chars.peek() == Some(&'"'), delim) {
QuoteEvent::Data(c) => cur.push(c),
QuoteEvent::EscapedQuote => {
cur.push('"');
chars.next();
}
QuoteEvent::FieldEnd => cells.push(std::mem::take(&mut cur)),
QuoteEvent::EnterQuotes | QuoteEvent::ExitQuotes => {}
}
}
cells.push(cur);
cells
}
pub(crate) fn field_terminator_char(field_terminator: Option<&str>) -> Result<char> {
match field_terminator {
None => Ok(','),
Some(s) => {
let mut chars = s.chars();
match (chars.next(), chars.next()) {
(Some(c), None) => Ok(c),
_ => Err(Error::ty(
"LOAD CSV FIELDTERMINATOR must be exactly one character",
)),
}
}
}
}
fn zip_with_header_nulls<'a>(
headers: &'a [String],
cells: &'a [String],
) -> impl Iterator<Item = (&'a String, Value)> + 'a {
headers.iter().enumerate().map(|(i, key)| {
let value = match cells.get(i) {
Some(cell) => Value::String(cell.as_str().into()),
None => Value::Null,
};
(key, value)
})
}
pub(crate) fn record_value(headers: Option<&[String]>, cells: Vec<String>) -> Value {
match headers {
Some(headers) => Value::Map(
zip_with_header_nulls(headers, &cells)
.map(|(key, value)| (key.clone(), value))
.collect::<BTreeMap<_, _>>()
.into(),
),
None => Value::List(
cells
.into_iter()
.map(|c| Value::String(c.into()))
.collect::<Vec<_>>()
.into(),
),
}
}
#[cfg(test)]
mod tests {
use super::{field_terminator_char, split_csv_line, RecordCursor};
fn split(text: &str, delim: char) -> Vec<String> {
let mut cursor = RecordCursor::new(text);
let mut out = Vec::new();
while let Some(record) = cursor.next_record(delim) {
out.push(record.to_owned());
}
out
}
#[test]
fn final_record_without_line_ending_strips_trailing_cr() {
let rs = split("a,b\r\nc,d\r", ',');
assert_eq!(rs, ["a,b", "c,d"]);
}
#[test]
fn newline_field_terminator_keeps_one_row_per_line() {
let rs = split("1\n2\n3", '\n');
assert_eq!(rs, ["1", "2", "3"]);
assert_eq!(split_csv_line("1", '\n'), vec!["1".to_string()]);
}
#[test]
fn quoted_field_with_delimiter_stays_one_cell() {
let cells = split_csv_line("1,\"Neo4j, Cypher and graphs\",x", ',');
assert_eq!(cells, vec!["1", "Neo4j, Cypher and graphs", "x"]);
}
#[test]
fn doubled_quote_inside_quoted_field_is_one_quote() {
let cells = split_csv_line("\"a \"\"quoted\"\" b\",2", ',');
assert_eq!(cells, vec!["a \"quoted\" b", "2"]);
}
#[test]
fn header_row_is_quote_aware_too() {
let cells = split_csv_line("id,\"the, header\",n", ',');
assert_eq!(cells, vec!["id", "the, header", "n"]);
}
#[test]
fn unquoted_lines_are_unchanged() {
assert_eq!(split_csv_line("a,b,c", ','), vec!["a", "b", "c"]);
assert_eq!(split_csv_line("a", ','), vec!["a"]);
}
#[test]
fn unterminated_quote_is_preserved_not_dropped() {
let cells = split_csv_line("\"open,ended", ',');
assert_eq!(cells, vec!["open,ended"]);
}
#[test]
fn quoted_field_with_newline_stays_one_record() {
let records = split("1,\"line one\nline two\",3\n4,four,5\n", ',');
assert_eq!(records, vec!["1,\"line one\nline two\",3", "4,four,5"]);
assert_eq!(
split_csv_line(&records[0], ','),
vec!["1", "line one\nline two", "3"]
);
}
#[test]
fn quoted_record_break_with_crlf_and_blank_lines() {
let records = split("a,\"b\r\nc\"\r\n\r\n\"d\",e", ',');
assert_eq!(records, vec!["a,\"b\r\nc\"", "", "\"d\",e"]);
}
#[test]
fn stray_quote_never_swallows_a_record_break() {
let records = split("say \"hi\",1\nnext,2\n", ',');
assert_eq!(records, vec!["say \"hi\",1", "next,2"]);
}
#[test]
fn custom_field_terminator_respects_quotes() {
let cells = split_csv_line("a\t\"b\tc\"\td", '\t');
assert_eq!(cells, vec!["a", "b\tc", "d"]);
}
#[test]
fn empty_fields_and_only_quotes() {
assert_eq!(split_csv_line(",\"\",x", ','), vec!["", "", "x"]);
assert_eq!(split_csv_line("a,,", ','), vec!["a", "", ""]);
}
#[test]
fn field_terminator_must_be_exactly_one_char() {
assert_eq!(field_terminator_char(None).unwrap(), ',');
assert_eq!(field_terminator_char(Some("\t")).unwrap(), '\t');
for invalid in ["||", ",;", ""] {
assert!(
field_terminator_char(Some(invalid)).is_err(),
"{invalid:?} must be rejected"
);
}
}
#[test]
fn the_cursor_counts_what_it_yields() {
let cases: &[(&str, char)] = &[
("a,b\r\nc,d\r", ','),
("1\n2\n3", '\n'),
("1,\"line one\nline two\",3\n4,four,5\n", ','),
("a,\"b\r\nc\"\r\n\r\n\"d\",e", ','),
("say \"hi\",1\nnext,2\n", ','),
("a\r", ','),
("a\r", '\n'),
("\r", ','),
("\n", '\n'),
("", ','),
("no breaks at all", ','),
];
for &(text, delim) in cases {
let walked = split(text, delim);
assert_eq!(
RecordCursor::new(text).remaining_records(delim, |_| true),
walked.len(),
"count parity on {text:?} delim {delim:?}"
);
}
}
}