#macro #better #immediately #syntax #expression #parse #invoked

no-std iife

A macro with a better syntax for immediately invoked function expressions

1 stable release

1.0.0 Apr 6, 2024

#118 in No standard library

Download history 90/week @ 2024-03-31 40/week @ 2024-04-07 1/week @ 2024-04-14

131 downloads per month

MIT license

5KB

iife! {} macro for a better immediately invoked function expression syntax

Example

use iife::iife;
use serde_json::{json, Value};

fn request() -> Result<Value, String> {
    Ok(json!({
       "first": {
           "second": {
               "third": "value"
           }
       }
    }))
}

fn main() -> Result<(), String> {
    let response = request()?;
    
    // This iife context allows you to use ? to return an Option instead of Result
    let parsed = iife! {
        response.get("first")?.get("second")?.get("third")?.as_str()
    }
    .ok_or("Failed to parse")?;
    assert_eq!(parsed, "value");
    Ok(())
}

lib.rs:

iife! {} macro for a better immediately invoked function expression syntax

Example

use iife::iife;
use serde_json::{json, Value};

fn request() -> Result<Value, String> {
    Ok(json!({
       "first": {
           "second": {
               "third": "value"
           }
       }
    }))
}


fn main() -> Result<(), String> {
    let response = request()?;
    
    // This iife context allows you to use ? to return an Option instead of Result
    let parsed = iife! {
        response.get("first")?.get("second")?.get("third")?.as_str()
    }
    .ok_or("Failed to parse")?;

    assert_eq!(parsed, "value");

    Ok(())
}

No runtime deps