1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// This is free and unencumbered software released into the public domain.

use crate::{grammar::model, ParseError, ParseResult, ParsedModel};

#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "std")]
pub fn parse_from_file(pathname: impl AsRef<std::path::Path>) -> ParseResult<ParsedModel> {
    parse_from_string(&std::fs::read_to_string(pathname)?)
}

#[cfg(feature = "std")]
pub fn parse_from_reader(reader: impl std::io::Read) -> ParseResult<ParsedModel> {
    parse_from_string(&std::io::read_to_string(reader)?)
}

pub fn parse_from_string(input: &str) -> ParseResult<ParsedModel> {
    let (_, model) = model(input).map_err(|err| match err {
        nom::Err::Error(error) | nom::Err::Failure(error) => ParseError::from(error),
        nom::Err::Incomplete(_) => unreachable!(),
    })?;
    Ok(model)
}