use crate::error::Error;
use crate::index::IndexedDocument;
use crate::view::JsonView;
#[inline]
pub fn json_lines(buf: &[u8]) -> JsonLines<'_> {
JsonLines { rest: buf }
}
#[derive(Debug, Clone)]
pub struct JsonLines<'a> {
rest: &'a [u8],
}
impl<'a> Iterator for JsonLines<'a> {
type Item = &'a [u8];
fn next(&mut self) -> Option<&'a [u8]> {
while !self.rest.is_empty() {
let nl = self
.rest
.iter()
.position(|&b| b == b'\n')
.unwrap_or(self.rest.len());
let mut line = &self.rest[..nl];
self.rest = if nl < self.rest.len() {
&self.rest[nl + 1..]
} else {
&[]
};
if line.last() == Some(&b'\r') {
line = &line[..line.len() - 1];
}
if !line.is_empty() {
return Some(line);
}
}
None
}
}
impl<'a> JsonLines<'a> {
#[inline]
pub fn rest(&self) -> &'a [u8] {
self.rest
}
}
pub fn read_jsonl<'a, T: JsonView + 'a>(
buf: &'a [u8],
) -> impl Iterator<Item = Result<T, Error>> + 'a {
json_lines(buf).map(T::read_from)
}
pub fn read_jsonl_indexed<'a, T: JsonView + 'a>(
buf: &'a [u8],
) -> impl Iterator<Item = Result<T, Error>> + 'a {
json_lines(buf).map(T::read_from_indexed)
}
pub fn read_line_indexed<T: JsonView>(
line: &[u8],
array_paths: &[&str],
) -> Result<T, Error> {
let doc = IndexedDocument::build(line, array_paths)?;
T::read_from_doc(&doc)
}