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§

Macros§

Derive Macros§

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