Skip to content

Commit

Permalink
feat: add config parse error (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Apr 2, 2024
1 parent acbdc21 commit 48afd26
Show file tree
Hide file tree
Showing 62 changed files with 198 additions and 168 deletions.
49 changes: 34 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use schemars::JsonSchema;

use crate::languages::{
blade::Blade, c::C, clojure::Clojure, cpp::Cpp, crystal::Crystal, csharp::CSharp, css::Css,
dart::Dart, elixir::Elixir, elm::Elm, erlang::Erlang, fsharp::FSharp, gleam::Gleam, go::Go,
graphql::GraphQL, groovy::Groovy, haskell::Haskell, html::Html, java::Java,
javascript::JavaScript, json::Json, just::Just, kotlin::Kotlin, lua::Lua, markdown::Markdown,
nim::Nim, objective_c::ObjectiveC, ocaml::OCaml, perl::Perl, protobuf::Protobuf,
purescript::PureScript, python::Python, rescript::ReScript, roc::Roc, ruby::Ruby, rust::Rust,
scala::Scala, shell::Shell, sql::Sql, swift::Swift, toml::Toml, typescript::TypeScript,
vue::Vue, xml::Xml, yaml::Yaml, zig::Zig, Lang,
use crate::{
error::MdsfError,
languages::{
blade::Blade, c::C, clojure::Clojure, cpp::Cpp, crystal::Crystal, csharp::CSharp, css::Css,
dart::Dart, elixir::Elixir, elm::Elm, erlang::Erlang, fsharp::FSharp, gleam::Gleam, go::Go,
graphql::GraphQL, groovy::Groovy, haskell::Haskell, html::Html, java::Java,
javascript::JavaScript, json::Json, just::Just, kotlin::Kotlin, lua::Lua,
markdown::Markdown, nim::Nim, objective_c::ObjectiveC, ocaml::OCaml, perl::Perl,
protobuf::Protobuf, purescript::PureScript, python::Python, rescript::ReScript, roc::Roc,
ruby::Ruby, rust::Rust, scala::Scala, shell::Shell, sql::Sql, swift::Swift, toml::Toml,
typescript::TypeScript, vue::Vue, xml::Xml, yaml::Yaml, zig::Zig, Lang,
},
terminal::print_config_info,
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand Down Expand Up @@ -219,14 +223,29 @@ impl Default for MdsfConfig {

impl MdsfConfig {
#[inline]
pub fn load() -> Self {
if let Ok(raw_config) = std::fs::read_to_string("mdsf.json") {
if let Ok(config) = serde_json::from_str::<Self>(&raw_config) {
return config;
pub fn load() -> Result<Self, MdsfError> {
let dir = std::env::current_dir()?;

let path = dir.join("mdsf.json");

match std::fs::read_to_string(&path) {
Ok(raw_config) => {
if let Ok(config) = serde_json::from_str::<Self>(&raw_config) {
print_config_info(Some(&path));
Ok(config)
} else {
Err(MdsfError::ConfigParse(path))
}
}
Err(error) => match error.kind() {
std::io::ErrorKind::NotFound => {
print_config_info(None);
Ok(Self::default())
}

_ => Err(MdsfError::from(error)),
},
}

Self::default()
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pub enum MdsfError {
Io(std::io::Error),
Fmt(core::fmt::Error),
ConfigParse(std::path::PathBuf),
FileNotFound(std::path::PathBuf),
}

impl core::fmt::Display for MdsfError {
Expand All @@ -10,6 +12,14 @@ 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!(
"No file or directory with the name '{}' found",
path.display()
)),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/autopep8.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_autopep8(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("autopep8");
print_formatter_info("autopep8");

let mut cmd = std::process::Command::new("autopep8");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/beautysh.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_beautysh(
file_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("beautysh");
print_formatter_info("beautysh");

let mut cmd = std::process::Command::new("beautysh");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/biome.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
runners::{setup_npm_script, JavaScriptRuntime},
terminal::print_debug_formatter_info,
terminal::print_formatter_info,
};

use super::execute_command;
Expand All @@ -9,7 +9,7 @@ use super::execute_command;
pub fn format_using_biome(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("biome");
print_formatter_info("biome");

// NOTE: the biome docs recommend running biome using npx, and not directly
let mut cmd = setup_npm_script(JavaScriptRuntime::default(), "@biomejs/biome");
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/black.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_black(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("black");
print_formatter_info("black");

let mut cmd = std::process::Command::new("black");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/blade_formatter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
runners::{setup_npm_script, JavaScriptRuntime},
terminal::print_debug_formatter_info,
terminal::print_formatter_info,
};

use super::execute_command;
Expand All @@ -24,7 +24,7 @@ fn invote_blade_formatter(
pub fn format_using_blade_formatter(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("blade-formatter");
print_formatter_info("blade-formatter");

invote_blade_formatter(
setup_npm_script(JavaScriptRuntime::default(), "blade-formatter"),
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/blue.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_blue(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("blue");
print_formatter_info("blue");

let mut cmd = std::process::Command::new("blue");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/buf.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_buf(snippet_path: &std::path::Path) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("buf");
print_formatter_info("buf");

let mut cmd = std::process::Command::new("buf");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/clang_format.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_clang_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("clang-format");
print_formatter_info("clang-format");

let mut cmd = std::process::Command::new("clang-format");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/cljstyle.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_cljstyle(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("cljstyle");
print_formatter_info("cljstyle");

let mut cmd = std::process::Command::new("cljstyle");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/crystal_format.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_crystal_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("crystal");
print_formatter_info("crystal");

let mut cmd = std::process::Command::new("crystal");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/csharpier.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_csharpier(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("csharpier");
print_formatter_info("csharpier");

let mut cmd = std::process::Command::new("dotnet");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/dart_format.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_dart_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("dart_format");
print_formatter_info("dart_format");

let mut cmd = std::process::Command::new("dart");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/deno_fmt.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_deno_fmt(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("deno_fmt");
print_formatter_info("deno_fmt");

let mut cmd = std::process::Command::new("deno");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/efmt.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_efmt(file_path: &std::path::Path) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("efmt");
print_formatter_info("efmt");

let mut cmd = std::process::Command::new("efmt");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/elm_format.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{
runners::{setup_npm_script, JavaScriptRuntime},
terminal::print_debug_formatter_info,
terminal::print_formatter_info,
};

use super::execute_command;

#[inline]
fn set_elm_format_args(cmd: &mut std::process::Command, snippet_path: &std::path::Path) {
print_debug_formatter_info("elm_format");
print_formatter_info("elm_format");

cmd.arg("--elm-version=0.19").arg("--yes").arg(snippet_path);
}
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/erlfmt.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_erlfmt(file_path: &std::path::Path) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("erlfmt");
print_formatter_info("erlfmt");

let mut cmd = std::process::Command::new("erlfmt");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/fantomas.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_fantomas(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("fantomas");
print_formatter_info("fantomas");

let mut cmd = std::process::Command::new("fantomas");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/fourmolu.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_fourmolu(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("fourmolu");
print_formatter_info("fourmolu");

let mut cmd = std::process::Command::new("fourmolu");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/gleam_format.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_gleam_format(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("gleam_format");
print_formatter_info("gleam_format");

let mut cmd = std::process::Command::new("gleam");

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/gofmt.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::terminal::print_debug_formatter_info;
use crate::terminal::print_formatter_info;

use super::execute_command;

#[inline]
pub fn format_using_gofmt(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
print_debug_formatter_info("gofmt");
print_formatter_info("gofmt");

let mut cmd = std::process::Command::new("gofmt");

Expand Down
Loading

0 comments on commit 48afd26

Please sign in to comment.