#sugar #macro #fallible #sweet #api-bindings

no-std candy

Syntaxic sugar for Rust: macros for lighter error handling code, and more

6 releases

0.1.5 Jul 4, 2019
0.1.4 Jul 2, 2019
0.1.3 Jan 3, 2019
0.1.2 Dec 9, 2018

#3 in #sweet

25 downloads per month

MIT license

17KB
369 lines

candy-rs

Syntaxic sugar for Rust: macros for lighter error handling code, and more.

Repository Latest version Documentation License

Examples

//! Run with `cargo run --example do_loop -- <number>`

#[macro_use] extern crate candy;

use ::std::*;

type ErrorMsg = borrow::Cow<'static, str>;

fallible! {
fn main ()
    ->  ()
    =>! ErrorMsg
:
    let input_number: u64 = {
        let (mb_argv_0, mb_argv_1) = {
            let mut args = env::args();
            (args.next(), args.next())
        };
        let prog_name = mb_argv_0.unwrap();
        match mb_argv_1
            	.and_then(|argv_1| argv_1.parse().ok())
        {
        	Some(number) => number,
        	_ => throw!(format!("Usage: {} <number>", prog_name)),
        }
    };
    collatz_conjecture(input_number);
}

fn collatz_conjecture (mut n: u64)
{
    do_loop!({
        println!("n = {}", n);
        if n % 2 == 0 {
            n /= 2;
        } else {
            n = 3 * n + 1;
        };
    } while n != 1);
    println!("Did reach 1.");
}
//! Run with `cargo run --example catch`

#[macro_use] extern crate candy;

use ::std::{
    *,
    io::Write,
};

fn main ()
{
    debug_print_all([0b101010, 0x45].iter())
}

fn debug_print_all (
    iterable: impl IntoIterator<Item = impl fmt::Debug>,
)
{
    let to_stdout = &mut io::stdout();
    
    // `catch!` allows using the `?` operator. Isn't that nice?
    match catch!({
        write!(to_stdout, "[")?;
        let mut iterator = iterable.into_iter();
        let mut count = 0;
        if let Some(first) = iterator.next() {
            count += 1;
            write!(to_stdout, "{:?}", first)?;
            while let Some(next) = iterator.next() {
                count += 1;
                write!(to_stdout, ", {:?}", next)?;
            };
        };
        write!(to_stdout, "]\n")?;
        count
    } -> usize =>! io::Error)
    {
        Err(io_err) => {
            eprintln!(
                "{:?} : could not write to stdout!? Oh well, who cares?",
                io_err,
            );
        },
        Ok(n) => {
            eprintln!("Successfully wrote {} elements to stdout", n);
        },
    }
}

Usage

  • Add this line to your Cargo.toml (under [dependencies]):

    candy = "0.1.5"
    
  • Add this to your .rs code:

    #[macro_use] extern crate candy;
    

No runtime deps

Features