From 4d389ad4e6223ba56ec739fca55ad2e170a06a2b Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Sun, 23 Aug 2020 14:46:27 +0200 Subject: Adding ingredient/combine separation --- Cargo.toml | 2 ++ rif/README.md | 20 ++++++++++++++++++++ rif/src/lib.rs | 4 ++-- rif/src/recipe.rs | 19 +++++++++++++------ rif/src/schema.rs | 49 +++++++++++++++++++++++-------------------------- rif/src/thread.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 6 files changed, 101 insertions(+), 35 deletions(-) create mode 100644 rif/README.md diff --git a/Cargo.toml b/Cargo.toml index a6253e7..e756beb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,7 @@ [workspace] members = [ + # Utility libraries "rif", + "cookbook-render", ] \ No newline at end of file diff --git a/rif/README.md b/rif/README.md new file mode 100644 index 0000000..e99b4c1 --- /dev/null +++ b/rif/README.md @@ -0,0 +1,20 @@ +# recipe instruction format (`rif`) + +A strongly typed expression format for recipes. + +Recipes consist of a schema which specifies available ingredients and +worksteps, some metadata that is largely computed from the actual +recipe, but can be queried from the main object, as well as a directed +graph of ingredient measures and worksteps separated into threads. + +A thread is a set of instructions that can be executed by a single +actor. Each thread has a set of inputs (ingredients) and an output, +which is it's own type, a mix of ingredients created by the thread. + + +## How to use + +```rust +let schema = Schema::load("./main.rifs")?; +let recp = Recipe:: +``` diff --git a/rif/src/lib.rs b/rif/src/lib.rs index 047dc24..a27d90b 100644 --- a/rif/src/lib.rs +++ b/rif/src/lib.rs @@ -25,5 +25,5 @@ mod schema; mod thread; pub use recipe::{Metadata, Recipe}; -pub use schema::{Ingredient, Schema, Workstep}; -pub use thread::Thread; +pub use schema::{Ingredient, Material, Schema, Workstep}; +pub use thread::{IngredientLike, Instruction, Measure, Thread}; diff --git a/rif/src/recipe.rs b/rif/src/recipe.rs index 21b7912..0e3b8fe 100644 --- a/rif/src/recipe.rs +++ b/rif/src/recipe.rs @@ -2,16 +2,23 @@ use crate::{Ingredient, Schema, Thread, Workstep}; use std::collections::BTreeSet; /// A recipe with instructions to execute -pub struct Recipe -where - I: Ingredient, - W: Workstep, -{ - schema: Schema, +pub struct Recipe { + schema: Schema, metadata: Metadata, threads: BTreeSet, } +impl Recipe { + /// Create a new recipe from a schema, with a name and version + pub fn new(schema: Schema, name: String, version: String) -> Self { + Self { + schema, + metadata: Metadata { name, version }, + threads: Default::default(), + } + } +} + pub struct Metadata { pub name: String, pub version: String, diff --git a/rif/src/schema.rs b/rif/src/schema.rs index 28c1304..ac3052e 100644 --- a/rif/src/schema.rs +++ b/rif/src/schema.rs @@ -2,37 +2,34 @@ use identity::Identity as Id; use std::collections::BTreeSet; /// A schema format backing a recipe set -pub struct Schema -where - I: Ingredient, - W: Workstep, -{ +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub struct Schema { /// Set of ingredients present in a recipe - pub ingredients: BTreeSet, + pub ingredients: BTreeSet, /// Set of allowed work-steps in a recipe - pub worksteps: BTreeSet, + pub worksteps: BTreeSet, } -/// A string-tagged ingredient in a recipe -pub trait Ingredient { - /// Get a human readable ingredient name - fn name(&self) -> String; - /// Get the ingredient slug - fn slug(&self) -> String; - /// A machine-efficient, random ID - fn id(&self) -> Id; +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub struct Ingredient { + pub name: String, + pub slug: String, + pub id: Id, } /// An execution step -pub trait Workstep { - /// Get a human readable workstep name - fn name(&self) -> String; - /// Get the workstep slug - fn slug(&self) -> String; - /// Get the human readable workstep description - fn description(&self) -> String; - /// A machine-efficient, random ID - fn id(&self) -> Id; - /// Execute some custom code for the step - fn run(&self) -> Option<()>; +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub struct Workstep { + pub id: Id, + pub name: String, + pub slug: String, + pub description: String, +} + +/// A piece of gear required to execute an action +pub struct Material { + pub id: Id, + pub name: String, + pub slug: String, + pub description: String, } diff --git a/rif/src/thread.rs b/rif/src/thread.rs index c543850..5de9cd5 100644 --- a/rif/src/thread.rs +++ b/rif/src/thread.rs @@ -1 +1,41 @@ -pub struct Thread {} +use crate::{Ingredient, Workstep}; +use std::collections::BTreeSet; + +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub struct Thread { + pub name: String, + pub instructions: Vec, + pub inputs: BTreeSet, + pub outputs: BTreeSet, +} + +/// An ingredient with precise attached measurements +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub struct Measure { + /// Base ingredient from the schema + pub ingredient: Ingredient, + /// Numerical amount + pub amount: usize, + /// Scale offset + pub scale: u64, +} + +/// Differentiator between raw schema ingredient or work result +pub enum IngredientLike { + /// An ingredient backed by the recipe schema + Raw(Ingredient), + /// An ingredient produced by a subset of the recipe + Combine(Combine), +} + +/// A specific variation of a `Workstep` which acts on Measures +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub struct Instruction {} + +/// The result of an instruction, which is not a base ingredient +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub struct Combine { + inputs: BTreeSet, + name: String, + slug: String, +} -- cgit v1.2.3