1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! Library for parsing, validating and normalizing flatbuffer files.
//!
//! This library is an internal implementation
//! detail of [planus-cli](https://docs.rs/planus-cli).
//!
//! Feel free to use it, however there are no stability guarantees.

#[macro_use]
extern crate lalrpop_util;

mod ast_convert;
mod intermediate_language;

mod ctx;
mod error;
mod parser;
mod pretty_print;
mod util;

use std::path::Path;

pub use error::ErrorKind;
pub use intermediate_language::translate_files;

pub fn format_file(path: &impl AsRef<Path>, ignore_errors: bool) -> Option<String> {
    let mut ctx = ctx::Ctx::default();
    let file_id = ctx.add_file(path, []).unwrap();
    if let Some(parsed) = ctx.parse_file(file_id) {
        if ctx.has_errors() && !ignore_errors {
            None
        } else {
            let mut s = String::new();
            if pretty_print::pretty_print(ctx.get_source(file_id), &parsed, &mut s).is_ok() {
                Some(s)
            } else {
                None
            }
        }
    } else {
        None
    }
}