Crate add_macro

Source
Expand description

githubcrates-iodocs-rs

This crate provides the more additional macros to help you write code faster!

§Examples:

use add_macro::{ re, Display, From, FromStr };

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Display, From)]
enum Error {
    #[from]
    Io(std::io::Error),
 
    #[display = "Incorrect E-mail address format"]
    IncorrectEmail,
}

#[derive(Debug, Display, FromStr)]
#[display = "{value}"]
struct Email {
    value: String,
}

impl Email {
    fn parse(s: &str) -> Result<Self> {
        let re = re!(r"^[\w\-]+@[\w\-]+\.\w+$");

        if re.is_match(s) {
            Ok(Self {
                value: s.into(),
            })
        } else {
            Err(Error::IncorrectEmail)
        }
    }
}

#[derive(Debug, Display)]
#[display = "Name: {name}, Age: {age}, E-mail: {email}"]
struct Person {
    name: String,
    age: u8,
    email: Email,
}

impl Person {
    pub fn new<S>(name: S, age: u8, email: Email) -> Self
    where S: Into<String> {
        Self {
            name: name.into(),
            age,
            email,
        }
    }
}

fn main() -> Result<()> {
    let bob = Person::new("Bob", 22, "bob@example.loc".parse()?);

    println!("{bob}");

    Ok(())
}

Modules§

collections
io
string

Macros§

btree_map
This macros provides the fast creating BTreeMap object
btree_set
This macros provides the fast creating BTreeSet object
deq
This macros provides the fast creating VecDeque object
heap
This macros provides the fast creating BinaryHeap object
input
This macros provides the usefull reading buffer by using io::stdin
list
This macros provides the fast creating LinkedList object
map
This macros provides the fast creating HashMap object
re
This macros provides the fast creating Regex object
set
This macros provides the fast creating HashSet object
str
This macros provides the fast creating String object

Derive Macros§

Display
This macros provides the implementation of trait Display
Error
This macros provides the implementation of trait Error
From
This macros provides the implementation of trait From
FromStr
This macros provides the implementation of the trait FromStr
Into
This macros provides the implementation of trait Into