1 unstable release
0.1.0 | Apr 6, 2025 |
---|
#3 in #macron
45 downloads per month
Used in macron
5KB
76 lines
Introduction
🔧 Comprehensive Rust macros toolkit for everyday development tasks. Includes convenient string formatting (str!), regex pattern matching (re!), streamlined collection creation, and custom derive macros for Display, Error, From, and Into traits. Boost your productivity with intuitive syntax and simplify your codebase while maintaining performance and safety. Perfect for both small projects and large-scale applications.
🚀 Empower your Rust development with this versatile set of macros, designed to make common tasks easier and more enjoyable.
Examples
String operations:
String
// simple string:
let s = str!("Hello, World!");
assert_eq!(s, "Hello, World!");
// from integer:
let s = str!(10);
assert_eq!(s, "10");
// from refference:
let r = 10.2;
let s = str!(r);
assert_eq!(s, "10.2");
// string formatting with arguments:
let s = str!("Hello, {}!", "World");
assert_eq!(s, "Hello, World!");
// string formatting with named arguments:
let name = "World";
let s = str!("Hello, {name}!");
assert_eq!(s, "Hello, World!");
Regex (writed for crate regex)
let re = re!(r"^Hello, \w+!$");
assert!(re.is_match("Hello, World!"));
// with formatting:
let re = re!(r"^Hello, {}!$", "World");
assert!(re.is_match("Hello, World!"));
Collections:
VecDeque
let mut deque = vec_deque![1, 2, 3];
assert_eq!(deque.len(), 3);
assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_back(), Some(3));
HashMap
let key = "one";
let val = 1;
let map = hash_map! {
key => val,
"two": 2,
"three" => 3,
"four": 4
};
assert_eq!(map.get("one"), Some(&1));
assert_eq!(map.get("two"), Some(&2));
HashSet
let set = hash_set![1, 2, 3];
assert!(set.contains(&1));
assert!(set.contains(&2));
BTreeMap
let key = "one";
let val = 1;
let map = btree_map! {
key => val,
"two": 2,
"three" => 3,
"four": 4
};
assert_eq!(map.get("one"), Some(&1));
assert_eq!(map.get("two"), Some(&2));
BTreeSet
let set = btree_set![4, 5, 6];
assert!(set.contains(&4));
assert!(set.contains(&5));
BinaryHeap
let mut heap = binary_heap![3, 1, 2];
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.pop(), Some(1));
LinkedList
let mut list = linked_list![10, 20, 30];
assert_eq!(list.len(), 3);
assert_eq!(list.pop_front(), Some(10));
assert_eq!(list.pop_back(), Some(30));
Derive macros:
impl Display
#[derive(Display)]
#[display = "Hello, {name}!"]
struct Hello {
pub name: &'static str,
}
let hello = Hello { name: "World" };
assert_eq!(format!("{hello}"), "Hello, World!");
#[derive(Display)]
enum Animals {
#[display = "it's a dog"]
Dog,
#[display = "cat '{0}'"]
Cat(&'static str, u8),
#[display = "bird '{name}'"]
Bird { name: &'static str, age: u8 },
}
let dog = Animals::Dog;
assert_eq!(format!("{dog}"), "it's a dog");
let cat = Animals::Cat("Tomas", 1);
assert_eq!(format!("{cat}"), "cat 'Tomas'");
let bird = Animals::Bird { name: "Kesha", age: 1 };
assert_eq!(format!("{bird}"), "bird 'Kesha'");
impl Error
#[derive(Debug, Error)]
struct Error {
source: Option<Box<dyn std::error::Error>>
}
impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "it's my custom error")
}
}
let err = Error { source: None };
assert_eq!(format!("{err}"), "it's my custom error");
#[derive(Debug, Error)]
enum Error {
WithAnySource { source: Option<Box<dyn std::error::Error>>, msg: String },
WithCertainSource(#[source] SourceError),
WithoutSource
}
impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match &self {
Self::WithAnySource { source, msg } => write!(f, "the error with a message: '{msg}' and a source: '{src}'", src = if let Some(src) = source.as_deref() { src.to_string() }else { "no source".to_owned() }),
Self::WithCertainSource(src) => write!(f, "the error with the source: '{src}'"),
Self::WithoutSource => write!(f, "the error without a source"),
}
}
}
#[derive(Debug, Error)]
enum SourceError {
Error
}
impl ::std::fmt::Display for SourceError {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match &self {
Self::Error => write!(f, "the source of error"),
}
}
}
let err = Error::WithAnySource { source: Some(Box::new(SourceError::Error)), msg: "the message of error".to_owned() };
let err2 = Error::WithCertainSource( SourceError::Error );
let err3 = Error::WithoutSource;
assert_eq!(format!("{err}"), "the error with a message: 'the message of error' and a source: 'the source of error'");
assert_eq!(format!("{err2}"), "the error with the source: 'the source of error'");
assert_eq!(format!("{err3}"), "the error without a source");
impl From
#[derive(From, Debug, PartialEq)]
#[from(Insertion, "Self { insertion: value }")]
struct Test {
insertion: Insertion,
}
#[derive(Debug, PartialEq)]
struct Insertion;
assert_eq!(Test { insertion: Insertion {} }, Test::from(Insertion {}));
#[derive(From, Debug, PartialEq)]
#[from(Insertion, "Self::Insertion(value)")]
enum Test {
Insertion(Insertion),
#[from(Insertion2, "value")]
Insertion2(Insertion2),
#[from(Insertion3, "insertion: value")]
Insertion3 { insertion: Insertion3 },
#[from]
Insertion4(Insertion4),
#[from]
Insertion5 { insertion: Insertion5 },
}
#[derive(Debug, PartialEq)]
struct Insertion;
#[derive(Debug, PartialEq)]
struct Insertion2;
#[derive(Debug, PartialEq)]
struct Insertion3;
#[derive(Debug, PartialEq)]
struct Insertion4;
#[derive(Debug, PartialEq)]
struct Insertion5;
assert_eq!(Test::Insertion(Insertion {}), Test::from(Insertion {}));
assert_eq!(Test::Insertion2(Insertion2 {}), Test::from(Insertion2 {}));
assert_eq!(Test::Insertion3 { insertion: Insertion3 {} }, Test::from(Insertion3 {}));
assert_eq!(Test::Insertion4(Insertion4 {}), Test::from(Insertion4 {}));
assert_eq!(Test::Insertion5 { insertion: Insertion5 {} }, Test::from(Insertion5 {}));
impl Into
#[derive(Into, Debug, PartialEq)]
#[into(Insertion, "self.insertion")]
struct Test {
insertion: Insertion,
}
#[derive(Debug, PartialEq)]
struct Insertion;
assert_eq!(Insertion {}, Test { insertion: Insertion {} }.into());
#[derive(Into, Debug, PartialEq)]
#[into(Insertion, "match self { Self::Insertion(ins) => ins }")]
enum Test {
Insertion(Insertion),
}
#[derive(Debug, PartialEq)]
struct Insertion;
assert_eq!(Insertion {}, Test::Insertion(Insertion {}).into());
Map
let (k, v) = ("one", 1);
let map = map! {
k => v,
"two": 2,
"three" => 3,
"four": 4,
};
assert_eq!(map, [("one", 1), ("two", 2), ("three", 3), ("four", 4)])
Licensing
Distributed under the MIT license.
Feedback
You can contact me via GitHub or send a message to my Telegram @fuderis.
This library is constantly evolving, and I welcome your suggestions and feedback.
Dependencies
~210–650KB
~15K SLoC