Some tools to help with the return value.
```rust use whatiwant::*;
fn login(username: String) -> bool { require!(username == "admin", false); ... }
fn login2(username: String) { require!(username == "admin"); ... } ```
Before using what_i_want
rust
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
```rust use whatiwant::*;
async fn getmutipartdata(mut mutipartdata: Multipart) -> MultipartData { while let Some(Ok(mut field)) = mutipartdata.next().await { let disposition = unwraporcontinue!(field.headers().get(&header::CONTENTDISPOSITION)); let dispositionstr = unwraporcontinue!(disposition.tostr()); let dis = unwraporcontinue!(ContentDisposition::parse(dispositionstr)); let key = unwraporcontinue!(dis.name); while let Some(Ok(chunk)) = field.next().await { ... } } MultipartData { ... } } ```
WhatIwant
```rust use whatiwant::*;
enum LoginReply { Success, Failed(i32) }
impl WhatIwant for LoginReply { fn isiwant(&self) -> bool { match self { LoginReply::Success => true, _ => false } } }
fn handle(reply: LoginReply) -> () { let re = unwraporreturn!(reply); // Do something ... } ```
```rust macrorules! unwrapordo { ($exp: expr, $do: expr) => { if $exp.isi_want() { $do } else { $exp.unwrap() } }; }
macrorules! unwraporcontinue { ($exp: expr) => { unwrapor_do!($exp, continue) }; }
macrorules! unwraporreturn { ($exp: expr) => { unwrapor_do!($exp, return) }; }
macrorules! unwraporfalse { ($exp: expr) => { unwrapor_do!($exp, return false) }; }
macrorules! unwraportrue { ($exp: expr) => { unwrapor_do!($exp, return false) }; }
macrorules! unwraporval { ($exp: expr, $val: expr) => { unwrapor_do!($exp, return $val) }; }
macro_rules! require { ($condition: expr) => { if !$condition { return; } }; ($condition: expr, $return: expr) => { if !$condition { return $return; } }; } ```