pest_fmt/
lib.rs

1extern crate pest;
2
3#[cfg(test)]
4extern crate proc_macro;
5
6#[cfg(test)]
7extern crate quote;
8
9#[cfg(test)]
10macro_rules! expect_correction {
11    ($source:expr, $expected:expr,) => {
12        let source = indoc::indoc! { $source };
13        let expected = indoc::indoc! { $expected };
14
15        let fmt = crate::Formatter::new(source);
16
17        pretty_assertions::assert_eq!(fmt.format().unwrap().trim_end(), expected.trim_end())
18    };
19    ($source:expr, $expected:expr) => {
20        expect_correction!($source, $expected,)
21    };
22}
23
24#[macro_use]
25
26mod error;
27mod comment;
28pub mod formatter;
29mod newline;
30mod node;
31
32pub use error::{PestError, PestResult};
33pub(crate) use node::*;
34
35pub struct Formatter<'a> {
36    input: &'a str,
37
38    /// Indent space size
39    indent: usize,
40}
41
42impl<'a> Formatter<'a> {
43    /// Create new formatter
44    pub fn new(input: &'a str) -> Formatter<'a> {
45        Self { input, indent: 4 }
46    }
47
48    /// Returns the str of the range in self.input, return empty str if the
49    /// range is valid (out of bounds).
50    #[inline]
51    #[allow(unused)]
52    pub(crate) fn get_str(&self, span: (usize, usize)) -> &str {
53        let (start, end) = span;
54        // Avoid out of bounds
55        if start > end || end > self.input.len() {
56            return "";
57        }
58
59        &self.input[start..end]
60    }
61}