diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index ef4c11b..80eadc9 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -2,6 +2,8 @@ use std::{io::Write, process::Command}; use tempfile::NamedTempFile; +use crate::languages::Language; + use self::{ nimpretty::format_using_nimpretty, ruff::format_using_ruff, rustfmt::format_using_rustfmt, stylua::format_using_stylua, zigfmt::format_using_zigfmt, @@ -31,15 +33,22 @@ pub fn execute_command(cmd: &mut Command) -> std::io::Result { } pub fn format_snippet(language: &str, code: String) -> String { - let mut f = if let Ok(Some(formatted_code)) = match language { - "rust" => format_using_rustfmt(&code), - "lua" => format_using_stylua(&code), - "python" => format_using_ruff(&code), - "nim" => format_using_nimpretty(&code), - "zig" => format_using_zigfmt(&code), - _ => Ok(None), - } { - return formatted_code; + let mut f = if let Some(l) = Language::from_str(language) { + if let Ok(snippet) = setup_snippet(&code) { + let snippet_path = snippet.path(); + + if let Ok(Some(formatted_code)) = match l { + Language::Lua => format_using_stylua(snippet_path), + Language::Nim => format_using_nimpretty(snippet_path), + Language::Python => format_using_ruff(snippet_path), + Language::Rust => format_using_rustfmt(snippet_path), + Language::Zig => format_using_zigfmt(snippet_path), + } { + return formatted_code; + } + } + + code } else { code } diff --git a/src/formatters/nimpretty.rs b/src/formatters/nimpretty.rs index df64769..7f74c29 100644 --- a/src/formatters/nimpretty.rs +++ b/src/formatters/nimpretty.rs @@ -1,9 +1,6 @@ -use super::{execute_command, read_snippet, setup_snippet}; - -pub fn format_using_nimpretty(code: &str) -> std::io::Result> { - let file = setup_snippet(code)?; - let file_path = file.path(); +use super::{execute_command, read_snippet}; +pub fn format_using_nimpretty(file_path: &std::path::Path) -> std::io::Result> { let mut cmd = std::process::Command::new("nimpretty"); cmd.arg(file_path); diff --git a/src/formatters/ruff.rs b/src/formatters/ruff.rs index be33386..e0e19ba 100644 --- a/src/formatters/ruff.rs +++ b/src/formatters/ruff.rs @@ -1,12 +1,7 @@ -use std::process::Command; +use super::{execute_command, read_snippet}; -use super::{execute_command, read_snippet, setup_snippet}; - -pub fn format_using_ruff(code: &str) -> std::io::Result> { - let file = setup_snippet(code)?; - let file_path = file.path(); - - let mut cmd = Command::new("ruff"); +pub fn format_using_ruff(file_path: &std::path::Path) -> std::io::Result> { + let mut cmd = std::process::Command::new("ruff"); cmd.arg("format"); cmd.arg("--quiet"); diff --git a/src/formatters/rustfmt.rs b/src/formatters/rustfmt.rs index 1233d55..e3a28b0 100644 --- a/src/formatters/rustfmt.rs +++ b/src/formatters/rustfmt.rs @@ -1,9 +1,6 @@ -use super::{execute_command, read_snippet, setup_snippet}; - -pub fn format_using_rustfmt(code: &str) -> std::io::Result> { - let file = setup_snippet(code)?; - let file_path = file.path(); +use super::{execute_command, read_snippet}; +pub fn format_using_rustfmt(file_path: &std::path::Path) -> std::io::Result> { let mut cmd = std::process::Command::new("rustfmt"); // Needed for async diff --git a/src/formatters/stylua.rs b/src/formatters/stylua.rs index ec53581..7ef1e5e 100644 --- a/src/formatters/stylua.rs +++ b/src/formatters/stylua.rs @@ -1,12 +1,7 @@ -use std::process::Command; +use super::{execute_command, read_snippet}; -use super::{execute_command, read_snippet, setup_snippet}; - -pub fn format_using_stylua(code: &str) -> std::io::Result> { - let file = setup_snippet(code)?; - let file_path = file.path(); - - let mut cmd = Command::new("stylua"); +pub fn format_using_stylua(file_path: &std::path::Path) -> std::io::Result> { + let mut cmd = std::process::Command::new("stylua"); cmd.arg("--verify"); cmd.arg(file_path); diff --git a/src/formatters/zigfmt.rs b/src/formatters/zigfmt.rs index 14010c4..143e200 100644 --- a/src/formatters/zigfmt.rs +++ b/src/formatters/zigfmt.rs @@ -1,9 +1,6 @@ -use super::{execute_command, read_snippet, setup_snippet}; - -pub fn format_using_zigfmt(code: &str) -> std::io::Result> { - let file = setup_snippet(code)?; - let file_path = file.path(); +use super::{execute_command, read_snippet}; +pub fn format_using_zigfmt(file_path: &std::path::Path) -> std::io::Result> { let mut cmd = std::process::Command::new("zig"); cmd.arg("fmt"); diff --git a/src/languages/mod.rs b/src/languages/mod.rs new file mode 100644 index 0000000..3bb1cf4 --- /dev/null +++ b/src/languages/mod.rs @@ -0,0 +1,20 @@ +pub enum Language { + Lua, + Nim, + Python, + Rust, + Zig, +} + +impl Language { + pub fn from_str(input: &str) -> Option { + match input { + "lua" => Some(Self::Lua), + "nim" => Some(Self::Nim), + "python" => Some(Self::Python), + "rust" => Some(Self::Rust), + "zig" => Some(Self::Zig), + _ => None, + } + } +} diff --git a/src/main.rs b/src/main.rs index b50181a..eca37fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use markdown::{generate_markdown, Block}; mod config; mod formatters; +mod languages; fn format_file(path: &std::path::Path) -> std::io::Result<()> { println!("Formatting {path:#?}");