From 800a7e61a7287c79eb2943b4ec367c582a579734 Mon Sep 17 00:00:00 2001 From: Csonka Mihaly Date: Sun, 22 Mar 2020 16:31:49 +0100 Subject: [PATCH] Added backticks around variable data in error info. Less ambiguous when input is empty. --- src/filesystem.rs | 22 ++++++++++++---------- src/flows/core.rs | 2 +- src/flows/repo.rs | 4 ++-- src/handler.rs | 4 ++-- src/parser.rs | 20 ++++++++++---------- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/filesystem.rs b/src/filesystem.rs index 914a860..7e3873c 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -11,7 +11,7 @@ pub fn read_lines

(filename: P) -> Result, Error> where P: AsRef + Display, { - let error_string = format!("Failed to read lines from {}", filename); + let error_string = format!("Failed to read lines from `{}`", filename); let file = File::open(filename)?; io::BufReader::new(file) .lines() @@ -24,7 +24,7 @@ pub fn pathbuf_to_string(pathbuf: PathBuf) -> Result { pathbuf .as_os_str() .to_str() - .ok_or_else(|| anyhow!("Invalid path {}", pathbuf.display())) + .ok_or_else(|| anyhow!("Invalid path `{}`", pathbuf.display())) .map(str::to_string) } @@ -44,14 +44,16 @@ fn follow_symlink(pathbuf: PathBuf) -> Result { let o_str = o .as_os_str() .to_str() - .ok_or_else(|| anyhow!("Invalid path {}", o.display()))?; + .ok_or_else(|| anyhow!("Invalid path `{}`", o.display()))?; if o_str.starts_with('.') { let parent_str = pathbuf .parent() - .ok_or_else(|| anyhow!("{} has no parent", pathbuf.display()))? + .ok_or_else(|| anyhow!("`{}` has no parent", pathbuf.display()))? .as_os_str() .to_str() - .ok_or_else(|| anyhow!("Parent of {} is an invalid path", pathbuf.display()))?; + .ok_or_else(|| { + anyhow!("Parent of `{}` is an invalid path", pathbuf.display()) + })?; let path_str = format!("{}/{}", parent_str, o_str); let p = PathBuf::from(path_str); follow_symlink(p) @@ -76,18 +78,18 @@ fn cheat_paths_from_config_dir() -> Result { .and_then(pathbuf_to_string) .and_then(|path| { fs::read_dir(path.clone()) - .with_context(|| format!("Unable to read directory {}", &path)) + .with_context(|| format!("Unable to read directory `{}`", &path)) .map(|entries| (path, entries)) }) .and_then(|(path, dir_entries)| { let mut paths_str = String::from(""); for entry in dir_entries { - let path = entry.with_context(|| format!("Unable to read directory {}", path))?; + let path = entry.with_context(|| format!("Unable to read directory `{}`", path))?; paths_str.push_str( path.path() .into_os_string() .to_str() - .ok_or_else(|| anyhow!("Invalid path {}", path.path().display()))?, + .ok_or_else(|| anyhow!("Invalid path `{}`", path.path().display()))?, ); paths_str.push_str(":"); } @@ -104,11 +106,11 @@ pub fn cheat_paths(config: &Config) -> Result { } pub fn create_dir(path: &str) -> Result<(), Error> { - fs::create_dir_all(path).with_context(|| format!("Failed to create directory {}", path)) + fs::create_dir_all(path).with_context(|| format!("Failed to create directory `{}`", path)) } pub fn remove_dir(path: &str) -> Result<(), Error> { - fs::remove_dir_all(path).with_context(|| format!("Failed to remove directory {}", path)) + fs::remove_dir_all(path).with_context(|| format!("Failed to remove directory `{}`", path)) } pub fn tmp_path_str() -> Result { diff --git a/src/flows/core.rs b/src/flows/core.rs index 826b527..a06e3f9 100644 --- a/src/flows/core.rs +++ b/src/flows/core.rs @@ -148,7 +148,7 @@ fn replace_variables_from_snippet( let value = values .get(variable_name) .map(|s| s.to_string()) - .ok_or_else(|| anyhow!(format!("No value for variable {}", variable_name))) + .ok_or_else(|| anyhow!(format!("No value for variable `{}`", variable_name))) .or_else(|_| { variables .get(&tags, &variable_name) diff --git a/src/flows/repo.rs b/src/flows/repo.rs index 2932d31..8c9a71d 100644 --- a/src/flows/repo.rs +++ b/src/flows/repo.rs @@ -17,7 +17,7 @@ pub fn browse() -> Result<(), Error> { let repo_url = "https://github.com/denisidoro/cheats"; Repository::clone(repo_url, &repo_path_str) - .with_context(|| format!("Failed to clone {}", repo_url))?; + .with_context(|| format!("Failed to clone `{}`", repo_url))?; let repos = fs::read_to_string(format!("{}/featured_repos.txt", &repo_path_str)) .context("Unable to fetch featured repositories")?; @@ -53,7 +53,7 @@ pub fn add(uri: String) -> Result<(), Error> { eprintln!("Cloning {} into {}...\n", &actual_uri, &tmp_path_str); Repository::clone(actual_uri.as_str(), &tmp_path_str) - .with_context(|| format!("Failed to clone {}", actual_uri))?; + .with_context(|| format!("Failed to clone `{}`", actual_uri))?; let all_files = WalkDir::new(&tmp_path_str) .into_iter() diff --git a/src/handler.rs b/src/handler.rs index c8f91b0..3699396 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -28,11 +28,11 @@ pub fn handle_config(mut config: Config) -> Result<(), Error> { Widget { shell } => flows::shell::main(&shell[..]), Fn { func, args } => flows::func::main(func.clone(), args.to_vec()) - .with_context(|| format!("Failed to execute function {}", func)), + .with_context(|| format!("Failed to execute function `{}`", func)), Repo { cmd } => match cmd { RepoCommand::Add { uri } => flows::repo::add(uri.clone()) - .with_context(|| format!("Failed to import cheatsheets from {}", uri)), + .with_context(|| format!("Failed to import cheatsheets from `{}`", uri)), RepoCommand::Browse => { flows::repo::browse().context("Failed to browse featured cheatsheets") } diff --git a/src/parser.rs b/src/parser.rs index ab92871..6c3d80d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -67,7 +67,7 @@ fn parse_opts(text: &str) -> Result { } Ok(()) } else if let [flag] = flag_and_value { - Err(anyhow!("No value provided for the flag {}", flag)) + Err(anyhow!("No value provided for the flag `{}`", flag)) } else { unreachable!() // Chunking by 2 allows only for tuples of 1 or 2 items... } @@ -88,23 +88,23 @@ fn parse_opts(text: &str) -> Result { fn parse_variable_line(line: &str) -> Result<(&str, &str, Option), Error> { let caps = VAR_LINE_REGEX.captures(line).ok_or_else(|| { anyhow!( - "No variables, command, and options found in the line {}", + "No variables, command, and options found in the line `{}`", line ) })?; let variable = caps .get(1) - .ok_or_else(|| anyhow!("No variable captured in the line {}", line))? + .ok_or_else(|| anyhow!("No variable captured in the line `{}`", line))? .as_str() .trim(); let mut command_plus_opts = caps .get(2) - .ok_or_else(|| anyhow!("No command and options captured in the line {}", line))? + .ok_or_else(|| anyhow!("No command and options captured in the line `{}`", line))? .as_str() .split("---"); let command = command_plus_opts .next() - .ok_or_else(|| anyhow!("No command captured in the line {}", line))?; + .ok_or_else(|| anyhow!("No command captured in the line `{}`", line))?; let command_options = command_plus_opts.next().map(parse_opts).transpose()?; Ok((variable, command, command_options)) } @@ -180,7 +180,7 @@ fn read_file( snippet = String::from(""); let (variable, command, opts) = parse_variable_line(&line).with_context(|| { format!( - "Failed to parse variable line. See line nr.{} in cheatsheet {}", + "Failed to parse variable line. See line nr.{} in cheatsheet `{}`", line_nr + 1, path ) @@ -224,16 +224,16 @@ pub fn read_all( let folders = paths_from_path_param(&paths); for folder in folders { - let dir_entries = - fs::read_dir(folder).with_context(|| format!("Unable to read directory {}", folder))?; + let dir_entries = fs::read_dir(folder) + .with_context(|| format!("Unable to read directory `{}`", folder))?; for entry in dir_entries { let path = entry - .with_context(|| format!("Unable to read directory {}", folder))? + .with_context(|| format!("Unable to read directory `{}`", folder))? .path(); let path_str = path .to_str() - .ok_or_else(|| anyhow!("Invalid path {}", path.display()))?; + .ok_or_else(|| anyhow!("Invalid path `{}`", path.display()))?; if path_str.ends_with(".cheat") && read_file(path_str, &mut variables, &mut visited_lines, stdin).is_ok() && !found_something