Skip to content

Commit

Permalink
refactor: remove setup_snippet boilerplate (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 7, 2024
1 parent 46f0d3c commit ef9a848
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 40 deletions.
27 changes: 18 additions & 9 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -31,15 +33,22 @@ pub fn execute_command(cmd: &mut Command) -> std::io::Result<bool> {
}

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
}
Expand Down
7 changes: 2 additions & 5 deletions src/formatters/nimpretty.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::{execute_command, read_snippet, setup_snippet};

pub fn format_using_nimpretty(code: &str) -> std::io::Result<Option<String>> {
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<Option<String>> {
let mut cmd = std::process::Command::new("nimpretty");

cmd.arg(file_path);
Expand Down
11 changes: 3 additions & 8 deletions src/formatters/ruff.rs
Original file line number Diff line number Diff line change
@@ -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<Option<String>> {
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<Option<String>> {
let mut cmd = std::process::Command::new("ruff");

cmd.arg("format");
cmd.arg("--quiet");
Expand Down
7 changes: 2 additions & 5 deletions src/formatters/rustfmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::{execute_command, read_snippet, setup_snippet};

pub fn format_using_rustfmt(code: &str) -> std::io::Result<Option<String>> {
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<Option<String>> {
let mut cmd = std::process::Command::new("rustfmt");

// Needed for async
Expand Down
11 changes: 3 additions & 8 deletions src/formatters/stylua.rs
Original file line number Diff line number Diff line change
@@ -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<Option<String>> {
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<Option<String>> {
let mut cmd = std::process::Command::new("stylua");

cmd.arg("--verify");
cmd.arg(file_path);
Expand Down
7 changes: 2 additions & 5 deletions src/formatters/zigfmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::{execute_command, read_snippet, setup_snippet};

pub fn format_using_zigfmt(code: &str) -> std::io::Result<Option<String>> {
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<Option<String>> {
let mut cmd = std::process::Command::new("zig");

cmd.arg("fmt");
Expand Down
20 changes: 20 additions & 0 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub enum Language {
Lua,
Nim,
Python,
Rust,
Zig,
}

impl Language {
pub fn from_str(input: &str) -> Option<Self> {
match input {
"lua" => Some(Self::Lua),
"nim" => Some(Self::Nim),
"python" => Some(Self::Python),
"rust" => Some(Self::Rust),
"zig" => Some(Self::Zig),
_ => None,
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:#?}");
Expand Down

0 comments on commit ef9a848

Please sign in to comment.