Skip to content

oOp995/round_about

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

round_about

Fast debugging for Result types and easy conversions between Result and Option.

for detailed usage see documentation and examples

debug.rs

debug with actions

result to option

Debug result

use std::num::ParseIntError;
use round_about::*;
fn main() {
    let operation = "37".parse::<i32>();
    let _=dbg_result!(&operation);
    let _:Result<i32,ParseIntError> = dbg_result!(operation);
}

Debug with actions

use round_about::*;
use std::num::ParseIntError;
fn main() {
    let process = "37s".parse::<i32>();
    //evaluate process
    //print debug
    //take action based on pre-defined `Fn(T)->U`
    let fini: (Result<i32, ParseIntError>, i32) = dbg_result!(process, ok_action, error_action);

    //extra sugar
    //re-use the returned value from your branches functions
    match fini.1 {
        1 => {
            println!("moved through ok action")
        }
        0 => {
            println!("moved through error action")
        }
        _ => {
            unreachable!()
        }
    }
    /*
    if let 1=fini.1{
        // ...
    }
    if let 0=fini.1{
        //..
    }
    */

    
    //super-extra sugar
    let option = round_about!(fini.0 => Option);
    assert_eq!(option, None);
}
fn ok_action() -> i32 {
    println!("Ok Action branch");
    1
}
fn error_action() -> i32 {
    println!("Error Action branch");
    0
}

Result<T,E> to Option<T>

use round_about::round_about;
use std::num::ParseIntError;
fn main() {
    let result:Result<i32, ParseIntError>="32".parse::<i32>();
    let option:Option<i32>=round_about!(result => Option);
    if let Some(value)=option{
        assert_eq!(value,32);
        //..
    }
}

Option<T> to Result<T,()>

use round_about::{round_about};
fn main() {
    let option: Option<i32> = Some(12);
    let result: Result<i32, ()> = round_about!(option => Result);
    if let Ok(value) = result {
        assert_eq!(value, 12);
    }
}

Option<T> to Result<T,UnitError>

requires unit-error feature

[dependencies.round_about]
version="0.1.1"
features=["unit_error"]
use round_about::*;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
    let i = probogate_error_from_option()?;
    Ok(())
}

// your function
fn probogate_error_from_option() -> Result<i32, Box<dyn Error>> {
    let v = vec![1, 2, 3];
    let i = v.get(5);
    let r = round_about!(i => Result)?;
    Ok(1)
}

Installation

[dependencies]
round_about = "0.1.1"

With UnitError (implements std::error::Error):

round_about = { version = "0.1.1", features = ["unit_error"] }

License

MIT OR Apache-2.0

About

Rust fast debugging for Result types and easy conversions between Result and Option

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages