Evaluator

Struct Evaluator 

Source
pub struct Evaluator<Ref, Value> { /* private fields */ }
Expand description

A generic evaluator that evaluates modules of type Value.

This evaluator can be used to decouple the way modules are read from the way they are evaluated. It follows design paradigms of the “sans-io” pattern.

This evaluator does not do anything on its own, it needs external code to “drive” it. Something like an “event loop”.

let mut evaluator = Evaluator::new();

evaluator.import("module 1");
while let Some(this) = evaluator.next() {
    println!("evaluating {this}...");

    // Read the value of the module here from the filesystem, network,
    // environment, etc.
    let value: i32 = unimplemented!();

    // Resolve what other modules this module wants to import. This is also
    // specific to how you read a module. For example, it could be the
    // top-level "imports" array of the JSON file.
    let imports = unimplemented!();

    evaluator.eval(this, imports, value).unwrap();
}

let value = evaluator.finish().unwrap();
println!("final value: {value}");

This pattern allows you to implement whatever kind of module logic you want inside the loop without modifying the evaluator.

Implementations§

Source§

impl<Ref, Value> Evaluator<Ref, Value>

Source

pub const fn new() -> Self

Create a new evaluator.

Source

pub const fn with(value: Value) -> Self

Create a new evaluator with an initial value.

Calling finish on the returned Evaluator will never return None.

Source

pub fn import<I>(&mut self, imports: I)
where I: Into<Imports<Ref>>,

Specify additional imports to be evaluated.

This function is usually only used to provide the first module.

Source

pub fn next(&mut self) -> Option<Ref>

Get the next module in the evaluation order.

Modules are evaluated in DFS order.

Source

pub fn finish(self) -> Option<Value>

Finish the evaluation and get the result.

Returns Some with the final value or None if no module has been evaluated successfully and the Evaluator was created without an initial value.

See: Evaluator::with.

Source§

impl<Ref, Value> Evaluator<Ref, Value>
where Ref: Display,

Source

pub fn trace(&self, this: Ref) -> Trace

Build the module trace for this module.

This function can be used to attach a module trace to module::Error on any user-thrown errors during the evaluation loop.

§Example
use module::Error;

let mut evaluator = Evaluator::new();

evaluator.import("module 1");
while let Some(this) = evaluator.next() {
    // ...

    if this == "module 2" {
        return Err(Error::custom("module 2 is not allowed to be evaluated"))
            .with_trace(|| evaluator.trace(this));
    }

    // ...

    evaluator.eval(this, imports, value)?;
}

let value = evaluator.finish().unwrap();
Ok(value)
Source§

impl<Ref, Value> Evaluator<Ref, Value>
where Ref: Display, Value: Merge,

Source

pub fn eval( &mut self, this: Ref, imports: Imports<Ref>, value: Value, ) -> Result<()>

Evaluate the module this.

  • imports: any modules this imports
  • value: the value of the this module

Returns the result of the Merge operation on value. The returned error has the appropriate trace attached.

See: Evaluator::trace.

Trait Implementations§

Source§

impl<Ref: Clone, Value: Clone> Clone for Evaluator<Ref, Value>

Source§

fn clone(&self) -> Evaluator<Ref, Value>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Ref: Debug, Value: Debug> Debug for Evaluator<Ref, Value>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Ref, Value> Default for Evaluator<Ref, Value>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Ref, Value> Freeze for Evaluator<Ref, Value>
where Value: Freeze,

§

impl<Ref, Value> RefUnwindSafe for Evaluator<Ref, Value>
where Value: RefUnwindSafe, Ref: RefUnwindSafe,

§

impl<Ref, Value> Send for Evaluator<Ref, Value>
where Value: Send, Ref: Send,

§

impl<Ref, Value> Sync for Evaluator<Ref, Value>
where Value: Sync, Ref: Sync,

§

impl<Ref, Value> Unpin for Evaluator<Ref, Value>
where Value: Unpin, Ref: Unpin,

§

impl<Ref, Value> UnwindSafe for Evaluator<Ref, Value>
where Value: UnwindSafe, Ref: UnwindSafe + RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.