use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)]
pub enum Cell {
Null,
Bool(bool),
Int(i64),
Float(f64),
Number(String),
Text(String),
Json(String),
}
#[derive(Debug, Default)]
pub struct RowColumns {
names: Vec<String>,
positions: HashMap<String, usize>,
cells: Vec<Vec<Cell>>,
rows: usize,
guess: usize,
}
impl RowColumns {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, name: &str, cell: Cell) {
let position = match self.names.get(self.guess) {
Some(guessed) if guessed == name => self.guess,
_ => self.position(name),
};
self.guess = position + 1;
let column = &mut self.cells[position];
column.resize(self.rows, Cell::Null);
column.push(cell);
}
pub fn end_row(&mut self) {
self.rows += 1;
self.guess = 0;
}
pub fn approximate_bytes(&self) -> usize {
self.cells
.iter()
.flatten()
.map(|cell| {
std::mem::size_of::<Cell>()
+ match cell {
Cell::Number(text) | Cell::Text(text) | Cell::Json(text) => text.len(),
Cell::Null | Cell::Bool(_) | Cell::Int(_) | Cell::Float(_) => 0,
}
})
.sum()
}
pub fn rows(&self) -> usize {
self.rows
}
pub fn into_columns(self) -> (usize, Vec<(String, Vec<Cell>)>) {
let rows = self.rows;
let columns: Vec<(String, Vec<Cell>)> = self
.names
.into_iter()
.zip(self.cells)
.map(|(name, mut cells)| {
cells.resize(rows, Cell::Null);
(name, cells)
})
.collect();
(rows, columns)
}
fn position(&mut self, name: &str) -> usize {
match self.positions.get(name) {
Some(position) => *position,
None => {
self.names.push(name.to_owned());
self.positions.insert(name.to_owned(), self.names.len() - 1);
self.cells.push(Vec::new());
self.names.len() - 1
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JsonLayout {
Document,
Lines,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JsonRead {
pub layout: JsonLayout,
pub path: Option<String>,
pub batch_rows: usize,
}
#[derive(Debug, Default)]
pub struct JsonWriter {
text: String,
}
impl JsonWriter {
pub fn new() -> Self {
Self::default()
}
pub fn raw(&mut self, text: &str) {
self.text.push_str(text);
}
pub fn string(&mut self, text: &str) {
self.text.push('"');
let mut start = 0;
for (position, byte) in text.bytes().enumerate() {
let escape = match byte {
b'"' => "\\\"",
b'\\' => "\\\\",
b'\n' => "\\n",
b'\r' => "\\r",
b'\t' => "\\t",
0x08 => "\\b",
0x0c => "\\f",
0x00..=0x1f => "",
_ => continue,
};
self.text.push_str(&text[start..position]);
match escape.is_empty() {
true => self.text.push_str(&format!("\\u{byte:04x}")),
false => self.text.push_str(escape),
}
start = position + 1;
}
self.text.push_str(&text[start..]);
self.text.push('"');
}
pub fn float(&mut self, value: f64) {
match serde_json::Number::from_f64(value) {
Some(number) => self.text.push_str(&number.to_string()),
None => self.text.push_str("null"),
}
}
pub fn take(&mut self) -> String {
std::mem::take(&mut self.text)
}
}