ebnf_fmt/
lib.rs

1pub mod configuration;
2mod formatter;
3
4pub use configuration::Configuration;
5use ebnf_parser::{error::SyntaxError, Lexer, Parser};
6pub use formatter::Formatter;
7
8pub fn format_code(text: &str, config: &Configuration) -> Result<String, SyntaxError> {
9    Ok(Formatter::new(
10        Parser::new(Lexer::new(text)).parse()?,
11        text,
12        config,
13        |text| text,
14    )
15    .format())
16}
17
18pub fn format_code_with_comment_formatter(
19    text: &str,
20    config: &Configuration,
21    comment_formatter: impl FnMut(String) -> String,
22) -> Result<String, SyntaxError> {
23    Ok(Formatter::new(
24        Parser::new(Lexer::new(text)).parse()?,
25        text,
26        config,
27        comment_formatter,
28    )
29    .format())
30}
31
32#[cfg(test)]
33mod tests {
34    use crate::configuration::Configuration;
35
36    use super::*;
37
38    #[test]
39    fn format() {
40        let input = include_str!("../../ebnf-parser/grammar.ebnf");
41        let output = format_code(input, &Configuration::default()).unwrap();
42        println!("{output}");
43        assert!(!output.ends_with("\n\n"));
44    }
45}