Skip to content

Commit

Permalink
[fud2] Rhai Plugins (#2078)
Browse files Browse the repository at this point in the history
* initial implementation of rhai based plugin system

* cleanup EmitSetup trait situation + appease clippy

* fix tests by emitting everything to an intermediate buffer first

* use thread_local + once_cell::Lazy to avoid constructing the engine over and over

* remove unused import

* DriverBuilder::find_state + define plugins in fud2.toml

* try building more complicated plugins

* unused annotation for add_file

* nicer errors for rhai

* remove unused imports

* clean reporting code slightly

* create a new emitter for each rhai function

* RhaiEmitter::with to simplifies buffering + unbuffering the emitter

* separate plugins.rs into multiple files + rename things

* fix lib.rs

* error reporting for setup_refs

* small string change

* change Into to From
  • Loading branch information
sgpthomas authored Jun 2, 2024
1 parent 71edd9c commit 6b8390e
Show file tree
Hide file tree
Showing 17 changed files with 858 additions and 34 deletions.
109 changes: 109 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fud2/fud-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ camino = "1.1.6"
anyhow.workspace = true
log.workspace = true
env_logger.workspace = true
rhai = { version = "1.18.0" }
once_cell = "1.19.0"
ariadne = "0.4.1"
71 changes: 54 additions & 17 deletions fud2/fud-core/src/exec/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{OpRef, Operation, Request, Setup, SetupRef, State, StateRef};
use crate::{run, utils};
use camino::{Utf8Path, Utf8PathBuf};
use cranelift_entity::{PrimaryMap, SecondaryMap};
use std::collections::HashMap;
use std::{collections::HashMap, error::Error, fmt::Display};

#[derive(PartialEq)]
enum Destination {
Expand Down Expand Up @@ -225,14 +225,35 @@ impl Driver {
}

pub struct DriverBuilder {
name: String,
pub name: String,
setups: PrimaryMap<SetupRef, Setup>,
states: PrimaryMap<StateRef, State>,
ops: PrimaryMap<OpRef, Operation>,
rsrc_dir: Option<Utf8PathBuf>,
rsrc_files: Option<FileData>,
}

#[derive(Debug)]
pub enum DriverError {
UnknownState(String),
UnknownSetup(String),
}

impl Display for DriverError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DriverError::UnknownState(state) => {
write!(f, "Unknown state: {state}")
}
DriverError::UnknownSetup(setup) => {
write!(f, "Unknown state: {setup}")
}
}
}
}

impl Error for DriverError {}

impl DriverBuilder {
pub fn new(name: &str) -> Self {
Self {
Expand All @@ -252,21 +273,12 @@ impl DriverBuilder {
})
}

fn add_op<T: run::EmitBuild + 'static>(
&mut self,
name: &str,
setups: &[SetupRef],
input: StateRef,
output: StateRef,
emit: T,
) -> OpRef {
self.ops.push(Operation {
name: name.into(),
setups: setups.into(),
input,
output,
emit: Box::new(emit),
})
pub fn find_state(&self, needle: &str) -> Result<StateRef, DriverError> {
self.states
.iter()
.find(|(_, State { name, .. })| needle == name)
.map(|(state_ref, _)| state_ref)
.ok_or_else(|| DriverError::UnknownState(needle.to_string()))
}

pub fn add_setup<T: run::EmitSetup + 'static>(
Expand All @@ -284,6 +296,31 @@ impl DriverBuilder {
self.add_setup(name, func)
}

pub fn find_setup(&self, needle: &str) -> Result<SetupRef, DriverError> {
self.setups
.iter()
.find(|(_, Setup { name, .. })| needle == name)
.map(|(setup_ref, _)| setup_ref)
.ok_or_else(|| DriverError::UnknownSetup(needle.to_string()))
}

pub fn add_op<T: run::EmitBuild + 'static>(
&mut self,
name: &str,
setups: &[SetupRef],
input: StateRef,
output: StateRef,
emit: T,
) -> OpRef {
self.ops.push(Operation {
name: name.into(),
setups: setups.into(),
input,
output,
emit: Box::new(emit),
})
}

pub fn op(
&mut self,
name: &str,
Expand Down
2 changes: 2 additions & 0 deletions fud2/fud-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod cli;
pub mod config;
pub mod exec;
pub mod run;
pub mod script;
pub mod utils;

pub use exec::{Driver, DriverBuilder};
pub use script::LoadPlugins;
Loading

0 comments on commit 6b8390e

Please sign in to comment.