Address review comments

This commit is contained in:
Rune Tynan 2021-01-16 00:29:47 -05:00
parent 7715656edd
commit 66a5714c63
No known key found for this signature in database
GPG Key ID: 7ECC932F8B2C731E
3 changed files with 28 additions and 62 deletions
src/tools/jsondocck/src

@ -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)))
}
}

@ -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 {

@ -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();
// @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,
}
}
_ => {
unreachable!()
}
let val = cache.get_value(&command.args[0])?;
match select(&val, &command.args[1]) {
Ok(results) => results.len() == expected,
Err(_) => false,
}
}
};