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
Execute if the condition is true, otherwise return
If it’s not what you want, then do continue
If it’s not what you want, then do what you want
If it’s not what you want, then do return false
If it’s not what you want, then do return ()
If it’s not what you want, then do return true
If it’s not what you want, then do return <defined return value>
Traits
Implement WhatIwant
and let us know what you want