Skip to content

Commit 49acec6

Browse files
Jeff Tsaijjeffcaii
authored andcommitted
feat: add some macros
1 parent e702ae6 commit 49acec6

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed

rsocket/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
pub use async_stream::stream;
9696
/// A re-export of [`async-trait`](https://docs.rs/async-trait) for use with RSocket trait implementation.
9797
pub use async_trait::async_trait;
98-
use cfg_if::cfg_if;
9998

10099
#[macro_use]
101100
extern crate anyhow;
@@ -104,8 +103,16 @@ extern crate log;
104103
#[macro_use]
105104
extern crate cfg_if;
106105

106+
#[macro_use]
107+
#[doc(hidden)]
108+
pub mod macros;
109+
107110
pub mod error;
108111
pub mod extension;
112+
pub mod prelude;
113+
pub mod runtime;
114+
pub mod transport;
115+
pub mod utils;
109116

110117
cfg_if! {
111118
if #[cfg(feature = "frame")]{
@@ -117,11 +124,7 @@ cfg_if! {
117124

118125
mod core;
119126
mod payload;
120-
pub mod prelude;
121-
pub mod runtime;
122127
mod spi;
123-
pub mod transport;
124-
pub mod utils;
125128

126129
pub type Error = Box<dyn std::error::Error + Sync + Send>;
127130
pub type Result<T> = anyhow::Result<T>;

rsocket/src/macros/composite.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#[macro_export]
2+
macro_rules! composite {
3+
($($x:expr,$y:expr),+) => {
4+
{
5+
let mut b = $crate::extension::CompositeMetadata::builder();
6+
$(
7+
b = b.push($x.into(),$y);
8+
)*
9+
b.build()
10+
}
11+
}
12+
}
13+
14+
#[cfg(test)]
15+
mod tests {
16+
use super::*;
17+
18+
#[test]
19+
fn test_composite() {
20+
let c = composite!("application/json", "123", "text/plain", "ccc");
21+
let vv: Vec<_> = c
22+
.iter()
23+
.map(|it| {
24+
format!(
25+
"{}={}",
26+
it.get_mime_type(),
27+
it.get_metadata_utf8().unwrap_or_default()
28+
)
29+
})
30+
.collect();
31+
assert_eq!(vv.join(","), "application/json=123,text/plain=ccc");
32+
}
33+
}

rsocket/src/macros/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[macro_use]
2+
mod composite;
3+
#[macro_use]
4+
mod routing;

rsocket/src/macros/routing.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#[macro_export]
2+
macro_rules! tags {
3+
($($v:expr),+) => {
4+
{
5+
let mut b = $crate::extension::RoutingMetadata::builder();
6+
$(
7+
b = b.push_str($v);
8+
)*
9+
b.build()
10+
}
11+
}
12+
}
13+
14+
#[cfg(test)]
15+
mod tests {
16+
use super::*;
17+
18+
#[test]
19+
fn test_routing() {
20+
let t = tags!("a", "b", "c");
21+
let tags = t.get_tags();
22+
println!("{:?}", tags);
23+
assert_eq!("a,b,c", t.get_tags().join(","))
24+
}
25+
}

rustfmt.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
edition = "2018"
22
group_imports="StdExternalCrate"
3-

0 commit comments

Comments
 (0)