Added backticks around variable data in error info. Less ambiguous when input is empty.

This commit is contained in:
Csonka Mihaly 2020-03-22 16:31:49 +01:00
parent 3c5d5aba98
commit 800a7e61a7
5 changed files with 27 additions and 25 deletions

View file

@ -11,7 +11,7 @@ pub fn read_lines<P>(filename: P) -> Result<Vec<String>, Error>
where
P: AsRef<Path> + 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<String, Error> {
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<PathBuf, Error> {
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<String, Error> {
.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<String, Error> {
}
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<String, Error> {

View file

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

View file

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

View file

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

View file

@ -67,7 +67,7 @@ fn parse_opts(text: &str) -> Result<FzfOpts, Error> {
}
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<FzfOpts, Error> {
fn parse_variable_line(line: &str) -> Result<(&str, &str, Option<FzfOpts>), 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