Crate what_i_want

Source
Expand description

Some tools to help with the return value

§Examples

use what_i_want::*;

fn login(username: String) -> bool {
    require!(username == "admin", false);
    ...
}

fn login2(username: String) {
    require!(username == "admin");
    ...
}

§Handling Result and Option

§Examples

// Before using `what_i_want`
pub async fn get_mutipart_data(mut mutipart_data: Multipart) -> MultipartData {
    // Nested hell, and different Enum (Result, Option) handling
    // Of course this code is just for demonstration
    while let Some(Ok(mut field)) = mutipart_data.next().await {
        if let Some(disposition) = field.headers().get(&header::CONTENT_DISPOSITION) {
            if let Ok(disposition_str) = disposition.to_str() {
                if let Some(dis) = ContentDisposition::parse(disposition_str) {
                    if let Some(key) = dis.name {
                        while let Some(Ok(chunk)) = field.next().await {
                            ...
                        }
                    }
                }
            }
        }
    }
    MultipartData { ... }
}

// After using `what_i_want`
use what_i_want::*;

async fn get_mutipart_data(mut mutipart_data: Multipart) -> MultipartData {
    while let Some(Ok(mut field)) = mutipart_data.next().await {
        let disposition = unwrap_or_continue!(field.headers().get(&header::CONTENT_DISPOSITION));
        let disposition_str = unwrap_or_continue!(disposition.to_str());
        let dis = unwrap_or_continue!(ContentDisposition::parse(disposition_str));
        let key = unwrap_or_continue!(dis.name);
        while let Some(Ok(chunk)) = field.next().await {
            ...
        }
    }
    MultipartData { ... }
}

Macros§

require
Execute if the condition is true, otherwise return
unwrap_or_continue
If it’s not what you want, then do continue
unwrap_or_do
If it’s not what you want, then do what you want
unwrap_or_false
If it’s not what you want, then do return false
unwrap_or_return
If it’s not what you want, then do return ()
unwrap_or_true
If it’s not what you want, then do return true
unwrap_or_val
If it’s not what you want, then do return <defined return value>

Traits§

WhatIwant
Implement WhatIwant and let us know what you want