plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
7bbcc54
— Peter John
2026-07-19T17:32:24+05:30
feat: implement format_source wrapping topiary-core
- plum-core/src/formatter.rs +44 -5
plum-core/src/formatter.rs
CHANGED
|
@@ -1,19 +1,58 @@
|
|
|
1
1
|
// plum-core/src/formatter.rs
|
|
2
|
+
use std::io::Cursor;
|
|
3
|
+
|
|
4
|
+
use topiary_core::{formatter, Language, Operation, TopiaryQuery};
|
|
5
|
+
|
|
6
|
+
const FORMAT_QUERY: &str =
|
|
7
|
+
include_str!("../../tooling/tree-sitter-plum/queries/plum/format.scm");
|
|
8
|
+
|
|
2
9
|
#[derive(Debug)]
|
|
3
10
|
pub enum FormatterError {
|
|
4
|
-
TopiaryCoreError(
|
|
11
|
+
TopiaryCoreError(topiary_core::FormatterError),
|
|
5
12
|
}
|
|
6
13
|
|
|
7
14
|
impl std::fmt::Display for FormatterError {
|
|
8
15
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
9
16
|
match self {
|
|
10
|
-
FormatterError::TopiaryCoreError(
|
|
17
|
+
FormatterError::TopiaryCoreError(e) => write!(f, "Topiary error: {e}"),
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl std::error::Error for FormatterError {
|
|
23
|
+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
24
|
+
match self {
|
|
25
|
+
FormatterError::TopiaryCoreError(e) => Some(e),
|
|
11
26
|
}
|
|
12
27
|
}
|
|
13
28
|
}
|
|
14
29
|
|
|
15
|
-
impl
|
|
30
|
+
impl From<topiary_core::FormatterError> for FormatterError {
|
|
31
|
+
fn from(e: topiary_core::FormatterError) -> Self {
|
|
32
|
+
FormatterError::TopiaryCoreError(e)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
16
35
|
|
|
17
|
-
pub fn format_source(
|
|
36
|
+
pub fn format_source(source: &str) -> Result<String, FormatterError> {
|
|
37
|
+
let ts_language: tree_sitter::Language = tree_sitter_plum::LANGUAGE.into();
|
|
38
|
+
let facade_grammar: topiary_tree_sitter_facade::Language = ts_language.into();
|
|
39
|
+
let query = TopiaryQuery::new(&facade_grammar, FORMAT_QUERY)?;
|
|
40
|
+
let language = Language {
|
|
41
|
+
name: "plum".to_owned(),
|
|
42
|
+
query,
|
|
43
|
+
grammar: facade_grammar,
|
|
44
|
+
indent: None,
|
|
45
|
+
};
|
|
46
|
+
let mut input = Cursor::new(source.as_bytes());
|
|
47
|
+
let mut output = Vec::new();
|
|
48
|
+
formatter(
|
|
49
|
+
&mut input,
|
|
50
|
+
&mut output,
|
|
18
|
-
|
|
51
|
+
&language,
|
|
52
|
+
Operation::Format {
|
|
53
|
+
skip_idempotence: false,
|
|
54
|
+
tolerate_parsing_errors: false,
|
|
55
|
+
},
|
|
56
|
+
)?;
|
|
57
|
+
Ok(String::from_utf8(output).expect("Topiary output is valid UTF-8"))
|
|
19
58
|
}
|