aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs77
1 files changed, 43 insertions, 34 deletions
diff --git a/src/main.rs b/src/main.rs
index afeaf39..c93b169 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,46 +2,51 @@ use aip_filter::{parse, CompiledFilter};
use serde_json::Value as Json;
use std::{
fs::File,
- io::{self, BufRead, BufReader, BufWriter, Write},
+ io::{self, BufRead, BufReader, BufWriter, Read, Write},
path::Path,
process,
};
+const DEFAULT_MAX_RECORD_BYTES: usize = 8 * 1024 * 1024;
+
fn filter_lines(
expr: Option<&CompiledFilter<'_>>,
mut reader: impl BufRead,
out: &mut impl Write,
source: &str,
+ max_record_bytes: usize,
) -> io::Result<bool> {
let Some(expr) = expr else {
io::copy(&mut reader, out)?;
return Ok(true);
};
- let mut line_buffer = String::with_capacity(128);
+ let mut line_buffer = Vec::with_capacity(128);
let mut lineno = 0;
let mut valid = true;
- while reader.read_line(&mut line_buffer)? > 0 {
+ while (&mut reader).take(max_record_bytes as u64 + 1)
+ .read_until(b'\n', &mut line_buffer)? > 0
+ {
lineno += 1;
-
- // Skip empty lines
- if !line_buffer.trim().is_empty() {
- match serde_json::from_str::<Json>(&line_buffer) {
+ if line_buffer.len() > max_record_bytes {
+ eprintln!("{source}:{lineno}: record exceeds {max_record_bytes} bytes");
+ valid = false;
+ if line_buffer.last() != Some(&b'\n') {
+ reader.skip_until(b'\n')?;
+ }
+ } else if !line_buffer.iter().all(u8::is_ascii_whitespace) {
+ match serde_json::from_slice::<Json>(&line_buffer) {
Ok(json) => {
if expr.evaluate(&json) {
- // Write directly to stdout
- out.write_all(line_buffer.as_bytes())?;
+ out.write_all(&line_buffer)?;
}
}
Err(e) => {
- eprintln!("{}:{}: invalid JSON: {}", source, lineno, e);
+ eprintln!("{source}:{lineno}: invalid JSON: {e}");
valid = false;
}
}
}
-
- // Clear the buffer for the next line,
- // which keeps the memory allocated but resets the length to 0.
line_buffer.clear();
}
@@ -49,27 +54,31 @@ fn filter_lines(
}
fn usage(program: &str) -> ! {
- eprintln!("usage: {program} [-p|--print] <expr> [file...]");
+ eprintln!("usage: {program} [-p|--print] [--max-record-bytes N] [--] <expr> [file...]");
process::exit(2);
}
fn main() {
- let mut args: Vec<String> = std::env::args().collect();
- let program = args.remove(0);
-
- if args.is_empty() || args[0] == "-h" || args[0] == "--help" {
- usage(&program);
- }
-
- // Check for print flag
+ let mut args = std::env::args();
+ let program = args.next().unwrap();
let mut print_only = false;
- if args[0] == "-p" || args[0] == "--print" {
- print_only = true;
- args.remove(0);
- if args.is_empty() { usage(&program); }
- }
-
- let raw_expr = args.remove(0);
+ let mut max_record_bytes = DEFAULT_MAX_RECORD_BYTES;
+ let raw_expr = loop {
+ let arg = args.next().unwrap_or_else(|| usage(&program));
+ match arg.as_str() {
+ "-p" | "--print" => print_only = true,
+ "--max-record-bytes" => {
+ max_record_bytes = args.next()
+ .and_then(|value| value.parse::<usize>().ok())
+ .filter(|value| *value > 0 && value.checked_add(1).is_some())
+ .unwrap_or_else(|| usage(&program));
+ }
+ "--" => break args.next().unwrap_or_else(|| usage(&program)),
+ "-h" | "--help" => usage(&program),
+ _ => break arg,
+ }
+ };
+ let mut paths = args.peekable();
let expr = match parse(&raw_expr) {
Ok(expr) => expr,
Err(e) => {
@@ -88,14 +97,14 @@ fn main() {
}
let filter = expr.as_ref().map(|expr| expr.compile()).transpose()
.map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?;
- if args.is_empty() {
- return filter_lines(filter.as_ref(), io::stdin().lock(), &mut out, "<stdin>");
+ if paths.peek().is_none() {
+ return filter_lines(filter.as_ref(), io::stdin().lock(), &mut out, "<stdin>", max_record_bytes);
}
let mut valid = true;
- for path in &args {
- let file = File::open(Path::new(path))
+ for path in paths {
+ let file = File::open(Path::new(&path))
.map_err(|e| io::Error::new(e.kind(), format!("{path}: {e}")))?;
- valid &= filter_lines(filter.as_ref(), BufReader::new(file), &mut out, path)
+ valid &= filter_lines(filter.as_ref(), BufReader::new(file), &mut out, &path, max_record_bytes)
.map_err(|e| io::Error::new(e.kind(), format!("{path}: {e}")))?;
}
Ok(valid)