1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
use aip_filter::{parse, Expr, Filterable, Value};
use serde_json::Value as Json;
use std::{
fs::File,
io::{self, BufRead, BufReader, BufWriter, Write},
path::Path,
process,
};
struct JsonRecord<'j>(&'j Json);
impl<'j> Filterable for JsonRecord<'j> {
fn field(&self, name: &str) -> Option<Value<'_>> {
json_to_value(self.0.get(name)?)
}
fn field_path(&self, path: &[&str]) -> Option<Value<'_>> {
let mut cur = self.0;
for &segment in path {
cur = cur.get(segment)?;
}
json_to_value(cur)
}
fn all_field_values(&self) -> Vec<Value<'_>> {
match self.0 {
Json::Object(map) => map.values().filter_map(json_to_value).collect(),
_ => json_to_value(self.0).into_iter().collect(),
}
}
fn matches_global(&self, search: &str) -> bool {
match self.0 {
Json::Object(map) => map
.values()
.any(|value| JsonRecord(value).matches_global(search)),
Json::Array(items) => items
.iter()
.any(|value| JsonRecord(value).matches_global(search)),
_ => json_to_value(self.0).is_some_and(|value| value.matches_global(search)),
}
}
}
fn json_to_value(v: &Json) -> Option<Value<'_>> {
match v {
Json::String(s) => Some(Value::String(s.as_str())),
Json::Bool(b) => Some(Value::Bool(*b)),
Json::Null => Some(Value::Null),
Json::Number(n) => {
if let Some(i) = n.as_i64() { return Some(Value::Int(i)); }
if let Some(u) = n.as_u64() { return Some(Value::Uint(u)); }
n.as_f64().map(Value::Float)
}
Json::Array(arr) => Some(Value::List(
arr.iter().filter_map(json_to_value).collect(),
)),
Json::Object(map) => Some(Value::Map(
map.iter()
.filter_map(|(key, value)| {
json_to_value(value).map(|value| (key.as_str(), value))
})
.collect(),
)),
}
}
fn filter_lines(
expr: Option<&Expr>,
mut reader: impl BufRead,
out: &mut impl Write,
source: &str,
) -> 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 lineno = 0;
let mut valid = true;
while reader.read_line(&mut line_buffer)? > 0 {
lineno += 1;
// Skip empty lines
if !line_buffer.trim().is_empty() {
match serde_json::from_str::<Json>(&line_buffer) {
Ok(json) => {
if expr.evaluate(&JsonRecord(&json)) {
// Write directly to stdout
out.write_all(line_buffer.as_bytes())?;
}
}
Err(e) => {
eprintln!("{}:{}: invalid JSON: {}", source, lineno, e);
valid = false;
}
}
}
// Clear the buffer for the next line,
// which keeps the memory allocated but resets the length to 0.
line_buffer.clear();
}
Ok(valid)
}
fn usage(program: &str) -> ! {
eprintln!("usage: {program} [-p|--print] <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 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 expr = match parse(&raw_expr) {
Ok(expr) => expr,
Err(e) => {
eprintln!("{program}: {e}");
process::exit(1);
}
};
let mut out = BufWriter::new(io::stdout().lock());
let result = (|| -> io::Result<bool> {
if print_only {
if let Some(expr) = &expr {
writeln!(out, "{expr}")?;
}
return Ok(true);
}
if args.is_empty() {
return filter_lines(expr.as_ref(), io::stdin().lock(), &mut out, "<stdin>");
}
let mut valid = true;
for path in &args {
let file = File::open(Path::new(path))
.map_err(|e| io::Error::new(e.kind(), format!("{path}: {e}")))?;
valid &= filter_lines(expr.as_ref(), BufReader::new(file), &mut out, path)
.map_err(|e| io::Error::new(e.kind(), format!("{path}: {e}")))?;
}
Ok(valid)
})();
let flushed = out.flush();
match result.and_then(|valid| flushed.map(|()| valid)) {
Ok(true) => {}
Ok(false) => process::exit(1),
Err(e) => {
eprintln!("{program}: {e}");
process::exit(1);
}
}
}
|