Skip to content

Commit

Permalink
[fud2] improve error message for parser errors (#2258)
Browse files Browse the repository at this point in the history
It does what it says on the tin. The error messages for the parser won't
be good, but they are better than rust panics and I think become
functional enough.

One interesting thing to note is I chose to print full paths when
referencing files. This is because the directory of scripts is often not
where `fud2` was run from and pretty unrelated to that dir.
  • Loading branch information
jku20 committed Aug 11, 2024
1 parent 66ea78b commit 5254ebd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
11 changes: 7 additions & 4 deletions fud2/fud-core/src/exec/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,10 @@ impl DriverBuilder {
}

/// Load any plugin scripts specified in the configuration file.
pub fn load_plugins(mut self, config_data: &figment::Figment) -> Self {
pub fn load_plugins(
mut self,
config_data: &figment::Figment,
) -> anyhow::Result<Self> {
// pull out things from self that we need
let plugin_dir = self.scripts_dir.take();
let plugin_files = self.script_files.take();
Expand All @@ -455,7 +458,7 @@ impl DriverBuilder {
.filter_map(|dir_entry| dir_entry.map(|p| p.path()).ok())
// filter out paths that don't have `.rhai` extension
.filter(|p| p.extension() == Some(OsStr::new("rhai"))),
);
)?;
}

// add static plugins (where string is included in binary)
Expand All @@ -467,10 +470,10 @@ impl DriverBuilder {
if let Ok(plugins) =
config_data.extract_inner::<Vec<std::path::PathBuf>>("plugins")
{
runner.add_files(plugins.into_iter());
runner.add_files(plugins.into_iter())?;
}

runner.run()
Ok(runner.run())
}

pub fn build(self) -> Driver {
Expand Down
16 changes: 13 additions & 3 deletions fud2/fud-core/src/script/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,24 @@ impl ScriptRunner {
pub fn add_files(
&mut self,
files: impl Iterator<Item = PathBuf>,
) -> &mut Self {
) -> anyhow::Result<&mut Self> {
for f in files {
let ast = self.engine.compile_file(f.clone()).unwrap();
let res = self.engine.compile_file(f.clone());
let ast = match res {
Err(e) => {
return Err(anyhow::format_err!(
"{} in {}",
e,
f.to_str().unwrap()
))
}
Ok(ast) => ast,
};
let functions =
self.resolver.as_mut().unwrap().register_path(f, ast);
self.rhai_functions = self.rhai_functions.merge(&functions);
}
self
Ok(self)
}

pub fn add_static_files(
Expand Down
2 changes: 1 addition & 1 deletion fud2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() -> anyhow::Result<()> {

#[cfg(feature = "migrate_to_scripts")]
{
bld = bld.load_plugins(&config);
bld = bld.load_plugins(&config)?;
}

let driver = bld.build();
Expand Down
4 changes: 2 additions & 2 deletions fud2/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn test_driver() -> Driver {
let mut bld = DriverBuilder::new("fud2-plugins");
let config = figment::Figment::new();
bld.scripts_dir(manifest_dir_macros::directory_path!("scripts"));
bld.load_plugins(&config).build()
bld.load_plugins(&config).unwrap().build()
}

fn driver_from_path_with_config(
Expand All @@ -36,7 +36,7 @@ fn driver_from_path_with_config(
path
);
bld.scripts_dir(&path);
bld.load_plugins(&config).build()
bld.load_plugins(&config).unwrap().build()
}

fn driver_from_path(path: &str) -> Driver {
Expand Down

0 comments on commit 5254ebd

Please sign in to comment.