Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(protobuf): add support for buf #131

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mdsf init
| OCaml | `ocamlformat` |
| ObjectiveC | `clang-format` |
| Perl | `perltidy` |
| Protobuf | `clang-format` |
| Protobuf | `buf`, `clang-format` |
| PureScript | `purs-tidy` |
| Python | `autopep8`, `black`, `blue`, `isort`, `ruff`, `usort`, `yapf` |
| ReScript | `rescript_format` |
Expand Down
4 changes: 2 additions & 2 deletions schemas/v0.0.2/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
"protobuf": {
"default": {
"enabled": true,
"formatter": "clang-format"
"formatter": [["buf", "clang-format"]]
},
"allOf": [
{
Expand Down Expand Up @@ -1782,7 +1782,7 @@
},
"Protobuf": {
"type": "string",
"enum": ["clang-format"]
"enum": ["buf", "clang-format"]
},
"PureScript": {
"type": "string",
Expand Down
3 changes: 2 additions & 1 deletion src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod biome;
pub mod black;
pub mod blade_formatter;
pub mod blue;
pub mod buf;
pub mod clang_format;
pub mod cljstyle;
pub mod crystal_format;
Expand Down Expand Up @@ -75,7 +76,7 @@ pub fn setup_snippet(code: &str, file_ext: &str) -> std::io::Result<NamedTempFil
},
);

let mut f = if file_ext == ".cs" {
let mut f = if file_ext == ".cs" || file_ext == ".proto" {
let _ = std::fs::create_dir_all(".mdsf-cache");
b.tempfile_in(".mdsf-cache")
} else {
Expand Down
36 changes: 34 additions & 2 deletions src/languages/protobuf.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use schemars::JsonSchema;

use crate::formatters::{clang_format::format_using_clang_format, MdsfFormatter};
use crate::formatters::{
buf::format_using_buf, clang_format::format_using_clang_format, MdsfFormatter,
};

use super::{Lang, LanguageFormatter};

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum Protobuf {
#[default]
#[serde(rename = "buf")]
Buf,
#[serde(rename = "clang-format")]
ClangFormat,
}
Expand All @@ -25,7 +29,10 @@ impl Default for Lang<Protobuf> {
impl Default for MdsfFormatter<Protobuf> {
#[inline]
fn default() -> Self {
Self::Single(Protobuf::ClangFormat)
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(Protobuf::Buf),
Self::Single(Protobuf::ClangFormat),
])])
}
}

Expand All @@ -36,6 +43,7 @@ impl LanguageFormatter for Protobuf {
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::Buf => format_using_buf(snippet_path),
Self::ClangFormat => format_using_clang_format(snippet_path),
}
}
Expand Down Expand Up @@ -75,6 +83,30 @@ mod test_protobuf {
.is_none());
}

#[test_with::executable(buf)]
#[test]
fn test_buf() {
let expected_output = "service SearchService {
rpc Search(SearchRequest) returns (SearchResponse);
}
";

let l = Lang::<Protobuf> {
enabled: true,
formatter: MdsfFormatter::Single(Protobuf::Buf),
};

let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

let output = l
.format(snippet_path)
.expect("it to not fail")
.expect("it to be a snippet");

assert_eq!(output, expected_output);
}

#[test_with::executable(clang-format)]
#[test]
fn test_clang_format() {
Expand Down
Loading