Skip to content

Commit

Permalink
refactor: propagate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Apr 5, 2024
1 parent 807d6cd commit 4c5114b
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 54 deletions.
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ precommit:
just test
just update-readme
cargo run -- init
cargo run -- format .
npx prettier --write mdsf.json
typos .

Expand Down
19 changes: 12 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
pub enum MdsfError {
Io(std::io::Error),
Fmt(core::fmt::Error),
// TODO: use &std::path::Path
ConfigParse(std::path::PathBuf),
// TODO: use &std::path::Path
FileNotFound(std::path::PathBuf),
FormatterError,
// TODO: use &str
MissingBinary(String),
}

impl core::fmt::Display for MdsfError {
Expand All @@ -13,15 +17,16 @@ impl core::fmt::Display for MdsfError {
match self {
Self::Io(e) => e.fmt(f),
Self::Fmt(e) => e.fmt(f),
Self::ConfigParse(path) => f.write_fmt(format_args!(
"Error parsing config found at '{}'",
path.display()
)),
Self::FileNotFound(path) => f.write_fmt(format_args!(
Self::ConfigParse(path) => {
write!(f, "Error parsing config found at '{}'", path.display())
}
Self::FileNotFound(path) => write!(
f,
"No file or directory with the name '{}' found",
path.display()
)),
Self::FormatterError => f.write_str("Error formatting"),
),
Self::FormatterError => write!(f, "Error formatting codeblock"),
Self::MissingBinary(binary_name) => write!(f, "{binary_name} was not found in path"),
}
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use schemars::JsonSchema;
use tempfile::NamedTempFile;
use which::which;

use crate::{
config::MdsfConfig, error::MdsfError, languages::Language, terminal::print_binary_not_in_path,
LineInfo, DEBUG,
};
use crate::{config::MdsfConfig, error::MdsfError, languages::Language, LineInfo, DEBUG};

pub mod alejandra;
pub mod autopep8;
Expand Down Expand Up @@ -148,8 +145,12 @@ pub fn execute_command(
cmd: &mut Command,
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
if !binary_in_path(cmd.get_program()) {
return Ok((true, None));
let binary_name = cmd.get_program();

if !binary_in_path(binary_name) {
return Err(MdsfError::MissingBinary(
binary_name.to_string_lossy().to_string(),
));
}

handle_post_execution(spawn_command(cmd), snippet_path)
Expand Down Expand Up @@ -236,10 +237,5 @@ where

#[inline]
pub fn binary_in_path(binary_name: &OsStr) -> bool {
if which(binary_name).is_ok() {
true
} else {
print_binary_not_in_path(&binary_name.to_string_lossy());
false
}
which(binary_name).is_ok()
}
11 changes: 6 additions & 5 deletions src/formatters/npm_groovy_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ fn invoke_npm_groovy_lint(
pub fn format_using_npm_groovy_lint(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let path_result =
invoke_npm_groovy_lint(std::process::Command::new("npm-groovy-lint"), snippet_path)?;

if !path_result.0 {
return Ok(path_result);
if let Ok(path_result) =
invoke_npm_groovy_lint(std::process::Command::new("npm-groovy-lint"), snippet_path)
{
if !path_result.0 {
return Ok(path_result);
}
}

invoke_npm_groovy_lint(setup_npm_script("npm-groovy-lint"), snippet_path)
Expand Down
10 changes: 5 additions & 5 deletions src/formatters/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ pub fn format_using_prettier(
snippet_path: &std::path::Path,
embedded_language_formatting: bool,
) -> Result<(bool, Option<String>), MdsfError> {
let global_result = invoke_prettier(
if let Ok(path_result) = invoke_prettier(
std::process::Command::new("prettier"),
snippet_path,
embedded_language_formatting,
)?;

if !global_result.0 {
return Ok(global_result);
) {
if !path_result.0 {
return Ok(path_result);
}
}

invoke_prettier(
Expand Down
1 change: 0 additions & 1 deletion src/formatters/scalafmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub fn format_using_scalafmt(
#[cfg(test)]
{
cmd.arg("--config-str").arg("\"version=3.8.0\"");

cmd.arg("--debug");
};

Expand Down
9 changes: 5 additions & 4 deletions src/formatters/standardjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ fn invoke_standardjs(
pub fn format_using_standardjs(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let global_result = invoke_standardjs(std::process::Command::new("standard"), snippet_path)?;

if !global_result.0 {
return Ok(global_result);
if let Ok(path_result) = invoke_standardjs(std::process::Command::new("standard"), snippet_path)
{
if !path_result.0 {
return Ok(path_result);
}
}

invoke_standardjs(setup_npm_script("standard"), snippet_path)
Expand Down
9 changes: 5 additions & 4 deletions src/formatters/stylelint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ fn invoke_stylelint(
pub fn format_using_stylelint(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let global_result = invoke_stylelint(std::process::Command::new("stylelint"), snippet_path)?;

if !global_result.0 {
return Ok(global_result);
if let Ok(path_result) = invoke_stylelint(std::process::Command::new("stylelint"), snippet_path)
{
if !path_result.0 {
return Ok(path_result);
}
}

invoke_stylelint(setup_npm_script("stylelint"), snippet_path)
Expand Down
8 changes: 4 additions & 4 deletions src/formatters/stylua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ fn invoke_stylua(
pub fn format_using_stylua(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let path_result = invoke_stylua(std::process::Command::new("stylua"), snippet_path)?;

if !path_result.0 {
return Ok(path_result);
if let Ok(path_result) = invoke_stylua(std::process::Command::new("stylua"), snippet_path) {
if !path_result.0 {
return Ok(path_result);
}
}

invoke_stylua(setup_npm_script("@johnnymorganz/stylua-bin"), snippet_path)
Expand Down
8 changes: 4 additions & 4 deletions src/formatters/taplo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ fn invoke_taplo(
pub fn format_using_taplo(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let path_result = invoke_taplo(std::process::Command::new("taplo"), snippet_path)?;

if !path_result.0 {
return Ok(path_result);
if let Ok(path_result) = invoke_taplo(std::process::Command::new("taplo"), snippet_path) {
if !path_result.0 {
return Ok(path_result);
}
}

invoke_taplo(setup_npm_script("@taplo/cli"), snippet_path)
Expand Down
20 changes: 17 additions & 3 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use schemars::JsonSchema;
use crate::{
error::MdsfError,
formatters::MdsfFormatter,
terminal::{print_error_formatting, print_formatter_info},
terminal::{print_binary_not_in_path, print_error_formatting, print_formatter_info},
LineInfo,
};

Expand Down Expand Up @@ -445,8 +445,22 @@ impl<T: LanguageFormatter + core::fmt::Display> Lang<T> {

let r = f.format_snippet(snippet_path);

if r.is_err() {
print_error_formatting(&formatter_name, info);
if let Err(e) = &r {
if let MdsfError::MissingBinary(binary) = e {
print_binary_not_in_path(
if &formatter_name == binary {
formatter_name
} else {
format!("{binary} ({formatter_name})")
}
.as_str(),
);

return Ok((false, None));
} else if matches!(e, MdsfError::FormatterError) {
print_error_formatting(&formatter_name, info);
return Ok((false, None));
}
}

r
Expand Down
12 changes: 7 additions & 5 deletions src/terminal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub fn print_error(error: &MdsfError) {
#[inline]
pub fn print_formatter_info(formatter: &str, info: &LineInfo) {
debug!(
"{} formatting '{}' block from :{} to :{} using {formatter}",
"{}:{} to :{} {} block using {formatter}",
info.filename.display(),
info.language,
info.start,
info.end
info.end,
info.language
);
}

Expand Down Expand Up @@ -51,13 +51,15 @@ pub fn print_unknown_javascript_runtime(value: u8, fallback: JavaScriptRuntime)

#[inline]
pub fn print_binary_not_in_path(binary_name: &str) {
warn!("'{binary_name}' not found in path");
warn!("{binary_name} not found in path");
}

#[inline]
pub fn print_error_formatting(formatter_name: &str, info: &LineInfo) {
error!(
"{} error formatting using {formatter_name}",
"{}:{} to :{} error formatting using {formatter_name}",
info.filename.display(),
info.start,
info.end
);
}

0 comments on commit 4c5114b

Please sign in to comment.