]> git.lizzy.rs Git - rust.git/commitdiff
Better errors in jsondocck
authorNixon Enraght-Moony <nixon.emoony@gmail.com>
Wed, 24 Mar 2021 15:52:47 +0000 (15:52 +0000)
committerNixon Enraght-Moony <nixon.emoony@gmail.com>
Wed, 24 Mar 2021 16:54:12 +0000 (16:54 +0000)
Cargo.lock
src/tools/jsondocck/Cargo.toml
src/tools/jsondocck/src/cache.rs
src/tools/jsondocck/src/main.rs

index e5a7b7d9b6056c02851755c25641d38846d720cb..9b0f310e3ae44fe03949bc0c1b4ca81b51e25ec4 100644 (file)
@@ -1219,6 +1219,12 @@ dependencies = [
  "rustc-std-workspace-core",
 ]
 
+[[package]]
+name = "fs-err"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bcd1163ae48bda72a20ae26d66a04d3094135cadab911cff418ae5e33f253431"
+
 [[package]]
 name = "fs_extra"
 version = "1.1.0"
@@ -1748,6 +1754,7 @@ checksum = "92c245af8786f6ac35f95ca14feca9119e71339aaab41e878e7cdd655c97e9e5"
 name = "jsondocck"
 version = "0.1.0"
 dependencies = [
+ "fs-err",
  "getopts",
  "jsonpath_lib",
  "lazy_static",
index 97052ef58d6f2d0e7d876bacd375947595ab9540..a6efc4c9a6b5bbd8d28755076a3f05fbc095d29f 100644 (file)
@@ -12,3 +12,4 @@ lazy_static = "1.4"
 shlex = "0.1"
 serde = "1.0"
 serde_json = "1.0"
+fs-err = "2.5.0"
index 8a6a911321c345a57c8fd0384359190d18791773..a188750c56ae307df54b08a0fe143d9a3bc74b1a 100644 (file)
@@ -1,8 +1,10 @@
 use crate::error::CkError;
 use serde_json::Value;
 use std::collections::HashMap;
+use std::io;
 use std::path::{Path, PathBuf};
-use std::{fs, io};
+
+use fs_err as fs;
 
 #[derive(Debug)]
 pub struct Cache {
@@ -31,7 +33,11 @@ fn resolve_path(&mut self, path: &String) -> PathBuf {
             self.last_path = Some(resolve.clone());
             resolve
         } else {
-            self.last_path.as_ref().unwrap().clone()
+            self.last_path
+                .as_ref()
+                // FIXME: Point to a line number
+                .expect("No last path set. Make sure to specify a full path before using `-`")
+                .clone()
         }
     }
 
index bcb3f6922efaabe02ca480b930a439e8b0a75cb3..216890d59ad6c53ceddf76629618caa33fd7e541 100644 (file)
@@ -239,7 +239,20 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
             let val = cache.get_value(&command.args[0])?;
             let results = select(&val, &command.args[1]).unwrap();
             let pat = string_to_value(&command.args[2], cache);
-            results.len() == 1 && results[0] == pat.as_ref()
+            let is = results.len() == 1 && results[0] == pat.as_ref();
+            if !command.negated && !is {
+                return Err(CkError::FailedCheck(
+                    format!(
+                        "{} matched to {:?}, but expected {:?}",
+                        &command.args[1],
+                        results,
+                        pat.as_ref()
+                    ),
+                    command,
+                ));
+            } else {
+                is
+            }
         }
         CommandKind::Set => {
             // @set <name> = <path> <jsonpath>
@@ -299,7 +312,10 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
 
 fn string_to_value<'a>(s: &str, cache: &'a Cache) -> Cow<'a, Value> {
     if s.starts_with("$") {
-        Cow::Borrowed(&cache.variables[&s[1..]])
+        Cow::Borrowed(&cache.variables.get(&s[1..]).unwrap_or_else(|| {
+            // FIXME(adotinthevoid): Show line number
+            panic!("No variable: `{}`. Current state: `{:?}`", &s[1..], cache.variables)
+        }))
     } else {
         Cow::Owned(serde_json::from_str(s).unwrap())
     }