iife

Crates.ioiife
lib.rsiife
version1.0.0
sourcesrc
created_at2024-04-06 15:35:02.780079
updated_at2024-04-06 15:35:02.780079
descriptionA macro with a better syntax for immediately invoked function expressions
homepage
repositoryhttps://github.com/kpids/iife
max_upload_size
id1198353
size4,053
(KPidS)

documentation

https://docs.rs/iife

README

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(())
}
Commit count: 1

cargo fmt