Skip to content

Commit

Permalink
feat: log formatter errors (#183)
Browse files Browse the repository at this point in the history
* feat: log if formatter error

* refactor: propagate errors
  • Loading branch information
hougesen authored Apr 5, 2024
1 parent 0df4c30 commit 6a04e35
Show file tree
Hide file tree
Showing 127 changed files with 561 additions and 299 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: 13 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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 @@ -12,14 +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 => write!(f, "Error formatting codeblock"),
Self::MissingBinary(binary_name) => write!(f, "{binary_name} was not found in path"),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/alejandra.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_alejandra(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("alejandra");

cmd.arg("--quiet").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/autopep8.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_autopep8(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("autopep8");

cmd.arg("--in-place").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/beautysh.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_beautysh(
file_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("beautysh");

cmd.arg(file_path);
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/biome.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::execute_command;
use crate::runners::setup_npm_script;
use crate::{error::MdsfError, runners::setup_npm_script};

#[inline]
pub fn format_using_biome(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
// NOTE: the biome docs recommend running biome using npx, and not directly
let mut cmd = setup_npm_script("@biomejs/biome");

Expand Down
3 changes: 2 additions & 1 deletion src/formatters/black.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_black(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("black");

cmd.arg("--quiet").arg(snippet_path);
Expand Down
6 changes: 3 additions & 3 deletions src/formatters/blade_formatter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::execute_command;
use crate::runners::setup_npm_script;
use crate::{error::MdsfError, runners::setup_npm_script};

#[inline]
fn set_blade_formatter_args(cmd: &mut std::process::Command, snippet_path: &std::path::Path) {
Expand All @@ -10,7 +10,7 @@ fn set_blade_formatter_args(cmd: &mut std::process::Command, snippet_path: &std:
fn invote_blade_formatter(
mut cmd: std::process::Command,
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
set_blade_formatter_args(&mut cmd, snippet_path);

execute_command(&mut cmd, snippet_path)
Expand All @@ -19,7 +19,7 @@ fn invote_blade_formatter(
#[inline]
pub fn format_using_blade_formatter(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
invote_blade_formatter(setup_npm_script("blade-formatter"), snippet_path)
}

Expand Down
3 changes: 2 additions & 1 deletion src/formatters/blue.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_blue(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("blue");

cmd.arg("--quiet").arg(snippet_path);
Expand Down
5 changes: 4 additions & 1 deletion src/formatters/buf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_buf(snippet_path: &std::path::Path) -> std::io::Result<(bool, Option<String>)> {
pub fn format_using_buf(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("buf");

cmd.arg("format").arg("--write").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/cabal_format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_cabal_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("cabal");

cmd.arg("format").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/clang_format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_clang_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("clang-format");

cmd.arg("-i").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/cljstyle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_cljstyle(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("cljstyle");

cmd.arg("fix").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/crystal_format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_crystal_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("crystal");

cmd.arg("tool").arg("format").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/csharpier.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_csharpier(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("dotnet");

cmd.arg("csharpier").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/dart_format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_dart_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("dart");

cmd.arg("format").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/deno_fmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_deno_fmt(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("deno");

cmd.arg("fmt").arg("--quiet").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/efmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_efmt(file_path: &std::path::Path) -> std::io::Result<(bool, Option<String>)> {
pub fn format_using_efmt(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("efmt");

cmd.arg("-w").arg(file_path);
Expand Down
6 changes: 3 additions & 3 deletions src/formatters/elm_format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::execute_command;
use crate::runners::setup_npm_script;
use crate::{error::MdsfError, runners::setup_npm_script};

#[inline]
fn set_elm_format_args(cmd: &mut std::process::Command, snippet_path: &std::path::Path) {
Expand All @@ -10,7 +10,7 @@ fn set_elm_format_args(cmd: &mut std::process::Command, snippet_path: &std::path
fn invoke_elm_format(
mut cmd: std::process::Command,
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
set_elm_format_args(&mut cmd, snippet_path);

execute_command(&mut cmd, snippet_path)
Expand All @@ -19,7 +19,7 @@ fn invoke_elm_format(
#[inline]
pub fn format_using_elm_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
invoke_elm_format(setup_npm_script("elm-format"), snippet_path)
}

Expand Down
5 changes: 4 additions & 1 deletion src/formatters/erlfmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_erlfmt(file_path: &std::path::Path) -> std::io::Result<(bool, Option<String>)> {
pub fn format_using_erlfmt(
file_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("erlfmt");

cmd.arg("-w")
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/fantomas.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_fantomas(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("fantomas");

cmd.arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/fourmolu.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_fourmolu(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("fourmolu");

cmd.arg("-i").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/gleam_format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_gleam_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("gleam");

cmd.arg("format").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/gofmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_gofmt(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("gofmt");

cmd.arg("-w").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/gofumpt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_gofumpt(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("gofumpt");

cmd.arg("-w").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/goimports.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_goimports(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("goimports");

cmd.arg("-w").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/google_java_format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_google_java_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("google-java-format");

cmd.arg("-i").arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/hindent.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_hindent(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("hindent");

cmd.arg(snippet_path);
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/isort.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_isort(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("isort");

cmd.arg("--quiet").arg(snippet_path);
Expand Down
Loading

0 comments on commit 6a04e35

Please sign in to comment.