3 releases
0.3.2 | Aug 19, 2024 |
---|---|
0.3.1 | Aug 3, 2024 |
0.2.2 |
|
0.2.1 |
|
0.1.7 |
|
#1143 in Rust patterns
28KB
203 lines
Crate Rust "add_macro"
This crate provides the more additional macros to help you write code faster!
Examples:
use add_macro::{ re, Display, From, FromStr };
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(())
}
Dependencies
~0.4–1.4MB
~30K SLoC