dhall/semantics/
parse.rs

1use std::path::Path;
2use url::Url;
3
4use crate::error::Error;
5use crate::semantics::resolve::{download_http_text, ImportLocation};
6use crate::syntax::{binary, parse_expr};
7use crate::Parsed;
8
9pub fn parse_file(f: &Path) -> Result<Parsed, Error> {
10    let path = crate::resolve::resolve_home(f)?;
11    let text = std::fs::read_to_string(path)?;
12    let expr = parse_expr(&text)?;
13    let root = ImportLocation::local_dhall_code(f.to_owned());
14    Ok(Parsed(expr, root))
15}
16
17pub fn parse_remote(url: Url) -> Result<Parsed, Error> {
18    let body = download_http_text(url.clone())?;
19    let expr = parse_expr(&body)?;
20    let root = ImportLocation::remote_dhall_code(url);
21    Ok(Parsed(expr, root))
22}
23
24pub fn parse_str(s: &str) -> Result<Parsed, Error> {
25    let expr = parse_expr(s)?;
26    let root = ImportLocation::dhall_code_of_unknown_origin();
27    Ok(Parsed(expr, root))
28}
29
30pub fn parse_binary(data: &[u8]) -> Result<Parsed, Error> {
31    let expr = binary::decode(data)?;
32    let root = ImportLocation::dhall_code_of_unknown_origin();
33    Ok(Parsed(expr, root))
34}
35
36pub fn parse_binary_file(f: &Path) -> Result<Parsed, Error> {
37    let data = crate::utils::read_binary_file(f)?;
38    let expr = binary::decode(&data)?;
39    let root = ImportLocation::local_dhall_code(f.to_owned());
40    Ok(Parsed(expr, root))
41}