Skip to main content

Operators

Struct Operators 

Source
pub struct Operators { /* private fields */ }
Expand description

Canonical operator registry.

Use Operators::register to add host-specific operators (typically x.*).

Implementations§

Source§

impl Operators

Source

pub fn new() -> Self

Build the default canonical operator registry.

Examples found in repository?
examples/run_data_eng.rs (line 34)
13fn main() {
14    let mut doc = read_program(WRANGLE_SCRIPT).unwrap_or_else(|e| {
15        eprintln!("parse: {:?}", e);
16        process::exit(1);
17    });
18
19    // Inject data from Rust (e.g. from DB, file, API)
20    let raw_data = json!([
21        { "region": "APAC", "amount": 100, "product": "A" },
22        { "region": "EMEA", "amount": 50, "product": "B" },
23        { "region": "APAC", "amount": 80, "product": "A" },
24        { "region": "Americas", "amount": 120, "product": "C" },
25        { "region": "EMEA", "amount": 30, "product": "B" },
26        { "region": "APAC", "amount": 60, "product": "C" },
27    ]);
28    doc["state"]["server"] = json!({ "raw_data": raw_data });
29
30    let program = parse_program(&doc).unwrap_or_else(|e| {
31        eprintln!("validate: {} - {}", e.code, e.message);
32        process::exit(1);
33    });
34    let operators = Operators::new();
35    let out = execute_program(&program, &operators).unwrap_or_else(|e| {
36        eprintln!("run: {} - {}", e.code, e.message);
37        process::exit(1);
38    });
39
40    println!("server.result (by region: count, total amount, desc):");
41    println!("{}", serde_json::to_string_pretty(&out.state.server["result"]).unwrap());
42}
More examples
Hide additional examples
examples/run_josie.rs (line 32)
11fn main() {
12    let path = match env::args().nth(1) {
13        Some(p) => p,
14        None => {
15            eprintln!("Usage: run_josie <path.josie>");
16            eprintln!("  e.g. cargo run -p josie-core --example run_josie -- examples/hello.josie");
17            process::exit(1);
18        }
19    };
20    let src = fs::read_to_string(&path).unwrap_or_else(|e| {
21        eprintln!("read {}: {}", path, e);
22        process::exit(1);
23    });
24    let doc = read_program(&src).unwrap_or_else(|e| {
25        eprintln!("parse {}: {:?}", path, e);
26        process::exit(1);
27    });
28    let program = parse_program(&doc).unwrap_or_else(|e| {
29        eprintln!("validate {}: {} ({})", path, e.code, e.message);
30        process::exit(1);
31    });
32    let operators = Operators::new();
33    let out = execute_program(&program, &operators).unwrap_or_else(|e| {
34        eprintln!("run {}: {} - {}", path, e.code, e.message);
35        process::exit(1);
36    });
37    println!("value: {}", serde_json::to_string_pretty(&out.value).unwrap());
38    let state_json = serde_json::json!({
39        "client": out.state.client,
40        "server": out.state.server,
41    });
42    println!("state: {}", serde_json::to_string_pretty(&state_json).unwrap());
43}
examples/run_backend.rs (line 61)
13fn main() {
14    let mut doc = read_program(BACKEND_SCRIPT).unwrap_or_else(|e| {
15        eprintln!("parse: {:?}", e);
16        process::exit(1);
17    });
18
19    // Inject people data (e.g. from DB or API)
20    let raw_people = json!([
21        {
22            "id": 1,
23            "first": "Jane",
24            "last": "Doe",
25            "age": 28,
26            "city": "London",
27            "country": "UK",
28            "email": "jane.doe@example.com",
29            "phone": "+44 20 7946 0958",
30            "tags": ["customer", "vip"]
31        },
32        {
33            "id": 2,
34            "first": "Bob",
35            "last": "Smith",
36            "age": 34,
37            "city": "Berlin",
38            "country": "DE",
39            "email": "bob.smith@example.com",
40            "phone": "+49 30 12345678",
41            "tags": ["partner"]
42        },
43        {
44            "id": 3,
45            "first": "Alice",
46            "last": "Wu",
47            "age": 41,
48            "city": "Singapore",
49            "country": "SG",
50            "email": "alice.wu@example.com",
51            "phone": null,
52            "tags": []
53        },
54    ]);
55    doc["state"]["server"] = json!({ "raw_people": raw_people });
56
57    let program = parse_program(&doc).unwrap_or_else(|e| {
58        eprintln!("validate: {} - {}", e.code, e.message);
59        process::exit(1);
60    });
61    let operators = Operators::new();
62    let out = execute_program(&program, &operators).unwrap_or_else(|e| {
63        eprintln!("run: {} - {}", e.code, e.message);
64        process::exit(1);
65    });
66
67    println!("server.response (custom JSON shape):");
68    println!("{}", serde_json::to_string_pretty(&out.state.server["response"]).unwrap());
69}
Source

pub fn get(&self, name: &str) -> Option<Operator>

Lookup an operator by name.

Source

pub fn register( &mut self, name: impl Into<String>, operator: Operator, ) -> Option<Operator>

Register or replace an operator. Returns previous handler if present.

Trait Implementations§

Source§

impl Default for Operators

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> 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, 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.