Module builder

Module builder 

Source
Expand description

Helper for building command from node-definition files.

Node-definition files are JSON files matching the Definition struct.

§Example

A command that adds 2 numbers:

use flow_lib::command::{*, prelude::*, builder::*};

inventory::submit!(CommandDescription::new("add", |_| build()));

const DEFINITION: &str = r#"
{
  "type": "native",
  "data": {
    "node_id": "add"
  },
  "sources": [
    {
      "name": "result",
      "type": "i64"
    }
  ],
  "targets": [
    {
      "name": "a",
      "type_bounds": ["i64"],
      "required": true,
      "passthrough": false
    },
    {
      "name": "b",
      "type_bounds": ["i64"],
      "required": true,
      "passthrough": false
    }
  ]
}
"#;

fn build() -> BuildResult {
    static CACHE: BuilderCache = BuilderCache::new(|| {
        CmdBuilder::new(DEFINITION)?
            .check_name("add")
    });
    Ok(CACHE.clone()?.build(run))
}

#[derive(serde::Deserialize, Debug)]
struct Input {
    a: i64,
    b: i64,
}

#[derive(serde::Serialize, Debug)]
struct Output {
    result: i64,
}

async fn run(_: CommandContext, input: Input) -> Result<Output, CommandError> {
    Ok(Output { result: input.a + input.b })
}

Structs§

CmdBuilder
Create a command from node-definition file and an async fn run() function.

Enums§

BuilderError

Type Aliases§

BuildResult
fn build() -> BuildResult.
BuilderCache
Use this to cache computation such as parsing JSON node-definition.