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
}
fn start_record(&mut self) {
self.field_started = false;
}
}
pub(crate) fn split_csv_records(text: &str, delim: char) -> Vec<&str> {
if delim == '\n' {
return text.lines().collect();
}
let mut records = Vec::new();
let mut start = 0;
let mut scan = QuoteScan::default();
let mut iter = text.char_indices().peekable();
while let Some((i, c)) = iter.next() {
match scan.advance(c, iter.peek().is_some_and(|&(_, next)| next == '"'), delim) {
QuoteEvent::EscapedQuote => {
iter.next();
}
QuoteEvent::Data('\n') if !scan.in_quotes() => {
let end = if i > start && text.as_bytes()[i - 1] == b'\r' {
i - 1
} else {
i
};
records.push(&text[start..end]);
start = i + 1;
scan.start_record();
}
QuoteEvent::Data(_)
| QuoteEvent::EnterQuotes
| QuoteEvent::ExitQuotes
| QuoteEvent::FieldEnd => {}
}
}
if start < text.len() {
let end = if text.len() > start && text.as_bytes()[text.len() - 1] == b'\r' {
text.len() - 1
} else {
text.len()
};
records.push(&text[start..end]);
}
records
}
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, split_csv_records};
#[test]
fn final_record_without_line_ending_strips_trailing_cr() {
let rs = split_csv_records("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_csv_records("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_csv_records("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_csv_records("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_csv_records("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"
);
}
}
}