Skip to content

Commit ce21447

Browse files
committed
Better errors in jsondocck
1 parent 2e012ce commit ce21447

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

Cargo.lock

+7
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,12 @@ dependencies = [
12191219
"rustc-std-workspace-core",
12201220
]
12211221

1222+
[[package]]
1223+
name = "fs-err"
1224+
version = "2.5.0"
1225+
source = "registry+https://github.com/rust-lang/crates.io-index"
1226+
checksum = "bcd1163ae48bda72a20ae26d66a04d3094135cadab911cff418ae5e33f253431"
1227+
12221228
[[package]]
12231229
name = "fs_extra"
12241230
version = "1.1.0"
@@ -1748,6 +1754,7 @@ checksum = "92c245af8786f6ac35f95ca14feca9119e71339aaab41e878e7cdd655c97e9e5"
17481754
name = "jsondocck"
17491755
version = "0.1.0"
17501756
dependencies = [
1757+
"fs-err",
17511758
"getopts",
17521759
"jsonpath_lib",
17531760
"lazy_static",

src/tools/jsondocck/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ lazy_static = "1.4"
1212
shlex = "0.1"
1313
serde = "1.0"
1414
serde_json = "1.0"
15+
fs-err = "2.5.0"

src/tools/jsondocck/src/cache.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::error::CkError;
22
use serde_json::Value;
33
use std::collections::HashMap;
4+
use std::io;
45
use std::path::{Path, PathBuf};
5-
use std::{fs, io};
6+
7+
use fs_err as fs;
68

79
#[derive(Debug)]
810
pub struct Cache {
@@ -31,7 +33,11 @@ impl Cache {
3133
self.last_path = Some(resolve.clone());
3234
resolve
3335
} else {
34-
self.last_path.as_ref().unwrap().clone()
36+
self.last_path
37+
.as_ref()
38+
// FIXME: Point to a line number
39+
.expect("No last path set. Make sure to specify a full path before using `-`")
40+
.clone()
3541
}
3642
}
3743

src/tools/jsondocck/src/main.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,20 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
239239
let val = cache.get_value(&command.args[0])?;
240240
let results = select(&val, &command.args[1]).unwrap();
241241
let pat = string_to_value(&command.args[2], cache);
242-
results.len() == 1 && results[0] == pat.as_ref()
242+
let is = results.len() == 1 && results[0] == pat.as_ref();
243+
if !command.negated && !is {
244+
return Err(CkError::FailedCheck(
245+
format!(
246+
"{} matched to {:?}, but expected {:?}",
247+
&command.args[1],
248+
results,
249+
pat.as_ref()
250+
),
251+
command,
252+
));
253+
} else {
254+
is
255+
}
243256
}
244257
CommandKind::Set => {
245258
// @set <name> = <path> <jsonpath>
@@ -299,7 +312,10 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
299312

300313
fn string_to_value<'a>(s: &str, cache: &'a Cache) -> Cow<'a, Value> {
301314
if s.starts_with("$") {
302-
Cow::Borrowed(&cache.variables[&s[1..]])
315+
Cow::Borrowed(&cache.variables.get(&s[1..]).unwrap_or_else(|| {
316+
// FIXME(adotinthevoid): Show line number
317+
panic!("No variable: `{}`. Current state: `{:?}`", &s[1..], cache.variables)
318+
}))
303319
} else {
304320
Cow::Owned(serde_json::from_str(s).unwrap())
305321
}

0 commit comments

Comments
 (0)