1use anyhow::{bail, Context, Result};
2use std::path::Path;
3use toml::macros::Deserialize;
4
5pub fn from_file(ncl_path: &Path) -> Result<toml::Table> {
6 let username = whoami::username();
7 let mut hostname = whoami::fallible::hostname()?;
8 hostname.make_ascii_lowercase();
9 let field_path_raw = format!("{username}@{hostname}");
10
11 use nickel_lang_core::{
12 error::report::ErrorFormat, eval::cache::lazy::CBNCache, identifier::LocIdent,
13 pretty::ident_quoted, program::Program as Prog,
14 };
15 let field_path = ident_quoted(&LocIdent::new(field_path_raw));
16 use std::io::stderr;
18 let mut prog = Prog::<CBNCache>::new_from_file(&ncl_path, stderr())?;
19 let res_field = prog.parse_field_path(field_path.clone());
20 let Ok(field) = res_field else {
21 prog.report(res_field.unwrap_err(), ErrorFormat::Text);
22 bail!("failed to parse {field_path:?} as Nickel path");
23 };
24 prog.field = field;
25 let res_term = prog.eval_full_for_export();
26 let Ok(term) = res_term else {
27 prog.report(res_term.unwrap_err(), ErrorFormat::Text);
28 bail!("script {ncl_path:?} failed");
29 };
30 let toml = toml::Table::deserialize(term).context("loading Nickel output to TOML")?;
31 Ok(toml)
32}