about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRune Tynan <runetynan@gmail.com>2021-01-16 00:29:47 -0500
committerRune Tynan <runetynan@gmail.com>2021-01-19 14:24:39 -0500
commit66a5714c63bd765af9176828aa0db7e77910647d (patch)
treef9f7dc350df2dabeb4023d04440775a849d03ed7
parent7715656edd201b8c6bbddf0040f424c27e4db4df (diff)
downloadrust-66a5714c63bd765af9176828aa0db7e77910647d.tar.gz
rust-66a5714c63bd765af9176828aa0db7e77910647d.zip
Address review comments
-rw-r--r--src/tools/jsondocck/src/cache.rs22
-rw-r--r--src/tools/jsondocck/src/config.rs8
-rw-r--r--src/tools/jsondocck/src/main.rs62
3 files changed, 29 insertions, 63 deletions
diff --git a/src/tools/jsondocck/src/cache.rs b/src/tools/jsondocck/src/cache.rs
index 32c32bc0890..a885d3c6557 100644
--- a/src/tools/jsondocck/src/cache.rs
+++ b/src/tools/jsondocck/src/cache.rs
@@ -1,8 +1,8 @@
 use crate::error::CkError;
 use serde_json::Value;
 use std::collections::HashMap;
-use std::fs;
 use std::path::{Path, PathBuf};
+use std::{fs, io};
 
 #[derive(Debug)]
 pub struct Cache {
@@ -15,28 +15,25 @@ pub struct Cache {
 impl Cache {
     pub fn new(doc_dir: &str) -> Cache {
         Cache {
-            root: <str as AsRef<Path>>::as_ref(doc_dir).to_owned(),
+            root: Path::new(doc_dir).to_owned(),
             files: HashMap::new(),
             values: HashMap::new(),
             last_path: None,
         }
     }
 
-    fn resolve_path(&mut self, path: &String) -> Result<PathBuf, CkError> {
+    fn resolve_path(&mut self, path: &String) -> PathBuf {
         if path != "-" {
             let resolve = self.root.join(path);
             self.last_path = Some(resolve.clone());
-            Ok(resolve)
+            resolve
         } else {
-            match &self.last_path {
-                Some(p) => Ok(p.clone()),
-                None => unreachable!(),
-            }
+            self.last_path.as_ref().unwrap().clone()
         }
     }
 
-    pub fn get_file(&mut self, path: &String) -> Result<String, CkError> {
-        let path = self.resolve_path(path)?;
+    pub fn get_file(&mut self, path: &String) -> Result<String, io::Error> {
+        let path = self.resolve_path(path);
 
         if let Some(f) = self.files.get(&path) {
             return Ok(f.clone());
@@ -47,24 +44,21 @@ impl Cache {
         self.files.insert(path, file.clone());
 
         Ok(file)
-        // Err(_) => Err(CkError::FailedCheck(format!("File {:?} does not exist / could not be opened", path)))
     }
 
     pub fn get_value(&mut self, path: &String) -> Result<Value, CkError> {
-        let path = self.resolve_path(path)?;
+        let path = self.resolve_path(path);
 
         if let Some(v) = self.values.get(&path) {
             return Ok(v.clone());
         }
 
         let file = fs::File::open(&path)?;
-        // Err(_) => return Err(CkError::FailedCheck(format!("File {:?} does not exist / could not be opened", path)))
 
         let val = serde_json::from_reader::<_, Value>(file)?;
 
         self.values.insert(path, val.clone());
 
         Ok(val)
-        // Err(_) => Err(CkError::FailedCheck(format!("File {:?} did not contain valid JSON", path)))
     }
 }
diff --git a/src/tools/jsondocck/src/config.rs b/src/tools/jsondocck/src/config.rs
index e76ebe1fa80..799a465e23d 100644
--- a/src/tools/jsondocck/src/config.rs
+++ b/src/tools/jsondocck/src/config.rs
@@ -22,16 +22,12 @@ pub fn parse_config(args: Vec<String>) -> Config {
         panic!()
     }
 
-    let matches = &match opts.parse(args_) {
-        Ok(m) => m,
-        Err(f) => panic!("{:?}", f),
-    };
+    let matches = opts.parse(args_).unwrap();
 
     if matches.opt_present("h") || matches.opt_present("help") {
         let message = format!("Usage: {} <doc-dir> <template>", argv0);
         println!("{}", opts.usage(&message));
-        println!();
-        panic!()
+        std::process::exit(1);
     }
 
     Config {
diff --git a/src/tools/jsondocck/src/main.rs b/src/tools/jsondocck/src/main.rs
index 6155128f1a7..7407aaf441c 100644
--- a/src/tools/jsondocck/src/main.rs
+++ b/src/tools/jsondocck/src/main.rs
@@ -62,28 +62,15 @@ impl CommandKind {
             return false;
         }
 
-        match self {
-            CommandKind::Has => {
-                if args[0] == "-" && command_num == 0 {
-                    print_err(
-                        &format!("Tried to use the previous path in the first command"),
-                        lineno,
-                    );
-                    return false;
-                }
-            }
-            CommandKind::Count => {
-                if args[0] == "-" && command_num == 0 {
-                    print_err(
-                        &format!("Tried to use the previous path in the first command"),
-                        lineno,
-                    );
-                    return false;
-                }
-                if args[2].parse::<usize>().is_err() {
-                    print_err(&format!("Third argument to @count must be a valid usize"), lineno);
-                    return false;
-                }
+        if args[0] == "-" && command_num == 0 {
+            print_err(&format!("Tried to use the previous path in the first command"), lineno);
+            return false;
+        }
+
+        if let CommandKind::Count = self {
+            if args[2].parse::<usize>().is_err() {
+                print_err(&format!("Third argument to @count must be a valid usize"), lineno);
+                return false;
             }
         }
 
@@ -132,10 +119,7 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
             None => continue,
         };
 
-        let negated = match cap.name("negated") {
-            Some(m) => m.as_str() == "!",
-            None => false,
-        };
+        let negated = cap.name("negated").unwrap().as_str() == "!";
         let cmd = cap.name("cmd").unwrap().as_str();
 
         let cmd = match cmd {
@@ -209,26 +193,18 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
                         Err(_) => false,
                     }
                 }
-                _ => {
-                    unreachable!()
-                }
+                _ => unreachable!(),
             }
         }
         CommandKind::Count => {
-            match command.args.len() {
-                // @count <path> <jsonpath> <count> = Check that the jsonpath matches exactly [count] times
-                3 => {
-                    let expected: usize = command.args[2].parse().unwrap();
-
-                    let val = cache.get_value(&command.args[0])?;
-                    match select(&val, &command.args[1]) {
-                        Ok(results) => results.len() == expected,
-                        Err(_) => false,
-                    }
-                }
-                _ => {
-                    unreachable!()
-                }
+            // @count <path> <jsonpath> <count> = Check that the jsonpath matches exactly [count] times
+            assert_eq!(command.args.len(), 3);
+            let expected: usize = command.args[2].parse().unwrap();
+
+            let val = cache.get_value(&command.args[0])?;
+            match select(&val, &command.args[1]) {
+                Ok(results) => results.len() == expected,
+                Err(_) => false,
             }
         }
     };