Skip to main content

require

Macro require 

Source
macro_rules! require {
    ($root:ident $(. $field:ident)+) => { ... };
}
Expand description

Walk a chain of Option<T> fields, returning a borrow of the leaf value.

Expands into nested RequireField::require calls, stamping each hop’s identifier into a dotted path that is carried in the NifiError::MissingField error on failure.

§Example

use nifi_rust_client::{NifiError, RequireField, require};

struct Outer { inner: Option<Inner> }
struct Inner { leaf: Option<String> }

fn example() -> Result<(), NifiError> {
    let o = Outer { inner: Some(Inner { leaf: Some("v".to_string()) }) };
    let leaf: &String = require!(o.inner.leaf);
    assert_eq!(leaf, "v");

    let missing = Outer { inner: Some(Inner { leaf: None }) };
    let err = (|| -> Result<(), NifiError> {
        let _leaf: &String = require!(missing.inner.leaf);
        Ok(())
    })().unwrap_err();
    assert!(matches!(err, NifiError::MissingField { path } if path == "inner.leaf"));
    Ok(())
}

§Notes

The macro expands into an expression block that uses the ? operator, so the surrounding function must return Result<_, impl From<NifiError>>. To use it inside a function that returns something else, wrap the call in a closure that returns Result<_, NifiError>, as shown above.