diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index 0e517fcf..dcec293c 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -38,6 +38,7 @@ pub mod purs_tidy; pub mod rescript_format; pub mod roc_format; pub mod rubocop; +pub mod rubyfmt; pub mod ruff; pub mod rufo; pub mod rustfmt; diff --git a/src/formatters/rubyfmt.rs b/src/formatters/rubyfmt.rs new file mode 100644 index 00000000..d4ce114b --- /dev/null +++ b/src/formatters/rubyfmt.rs @@ -0,0 +1,43 @@ +use super::execute_command; + +#[inline] +pub fn format_using_rubyfmt( + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + let mut cmd = std::process::Command::new("rubyfmt"); + + cmd.arg("-i").arg(snippet_path); + + execute_command(&mut cmd, snippet_path) +} + +#[cfg(test)] +mod test_rubyfmt { + use crate::{ + formatters::{rubyfmt::format_using_rubyfmt, setup_snippet}, + languages::Language, + }; + + #[test_with::executable(rubyfmt)] + #[test] + fn it_should_format_ruby() { + let input = "def add( a , b ) + return a + b + end"; + + let expected_output = "def add(a, b) + return a + b +end +"; + + let snippet = setup_snippet(input, Language::Ruby.to_file_ext()) + .expect("it to create a snippet file"); + + let output = format_using_rubyfmt(snippet.path()) + .expect("it to be successful") + .1 + .expect("it to be some"); + + assert_eq!(expected_output, output); + } +} diff --git a/src/languages/ruby.rs b/src/languages/ruby.rs index b8b22d53..c17c776b 100644 --- a/src/languages/ruby.rs +++ b/src/languages/ruby.rs @@ -1,6 +1,9 @@ use schemars::JsonSchema; -use crate::formatters::{rubocop::format_using_rubocop, rufo::format_using_rufo, MdsfFormatter}; +use crate::formatters::{ + rubocop::format_using_rubocop, rubyfmt::format_using_rubyfmt, rufo::format_using_rufo, + MdsfFormatter, +}; use super::{Lang, LanguageFormatter}; @@ -8,6 +11,8 @@ use super::{Lang, LanguageFormatter}; #[cfg_attr(test, derive(PartialEq, Eq))] pub enum Ruby { #[default] + #[serde(rename = "rubyfmt")] + RubyFmt, #[serde(rename = "rubocop")] RuboCop, #[serde(rename = "rufo")] @@ -30,6 +35,7 @@ impl Default for MdsfFormatter { Self::Multiple(vec![Self::Multiple(vec![ Self::Single(Ruby::RuboCop), Self::Single(Ruby::Rufo), + Self::Single(Ruby::RubyFmt), ])]) } } @@ -43,6 +49,7 @@ impl LanguageFormatter for Ruby { match self { Self::RuboCop => format_using_rubocop(snippet_path), Self::Rufo => format_using_rufo(snippet_path), + Self::RubyFmt => format_using_rubyfmt(snippet_path), } } }