Skip to content

Commit d384fba

Browse files
committed
Add pub(crate) support
1 parent 460041a commit d384fba

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

src/macros.rs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ macro_rules! closure (
8989
/// // will use &[u8] as input type (use this if the compiler
9090
/// // complains about lifetime issues
9191
/// named!(my_function<&[u8]>, tag!("abcd"));
92-
/// //prefix them with 'pub' to make the functions public
92+
/// // prefix them with 'pub' to make the functions public
9393
/// named!(pub my_function, tag!("abcd"));
94+
/// // prefix them with 'pub(crate)' to make the functions public within the crate
95+
/// named!(pub(crate) my_function, tag!("abcd"));
9496
/// ```
9597
#[macro_export]
9698
macro_rules! named (
@@ -157,6 +159,36 @@ macro_rules! named (
157159
$submac!(i, $($args)*)
158160
}
159161
);
162+
(pub(crate) $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => (
163+
#[allow(unused_variables)]
164+
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i,$o, u32> {
165+
$submac!(i, $($args)*)
166+
}
167+
);
168+
(pub(crate) $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => (
169+
#[allow(unused_variables)]
170+
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> {
171+
$submac!(i, $($args)*)
172+
}
173+
);
174+
(pub(crate) $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => (
175+
#[allow(unused_variables)]
176+
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> {
177+
$submac!(i, $($args)*)
178+
}
179+
);
180+
(pub(crate) $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => (
181+
#[allow(unused_variables)]
182+
pub(crate) fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, u32> {
183+
$submac!(i, $($args)*)
184+
}
185+
);
186+
(pub(crate) $name:ident, $submac:ident!( $($args:tt)* )) => (
187+
#[allow(unused_variables)]
188+
pub(crate) fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], u32> {
189+
$submac!(i, $($args)*)
190+
}
191+
);
160192
);
161193

162194
/// Makes a function from a parser combination with arguments.
@@ -172,6 +204,16 @@ macro_rules! named_args {
172204
$submac!(input, $($args)*)
173205
}
174206
};
207+
(pub(crate) $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
208+
pub(crate) fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> {
209+
$submac!(input, $($args)*)
210+
}
211+
};
212+
(pub(crate) $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
213+
pub(crate) fn $func_name<'this_is_probably_unique_i_hope_please, 'a>(input: &'this_is_probably_unique_i_hope_please [u8], $( $arg : $typ ),*) -> $crate::IResult<&'this_is_probably_unique_i_hope_please [u8], $return_type> {
214+
$submac!(input, $($args)*)
215+
}
216+
};
175217
($func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
176218
fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> {
177219
$submac!(input, $($args)*)
@@ -260,6 +302,36 @@ macro_rules! named_attr (
260302
$submac!(i, $($args)*)
261303
}
262304
);
305+
($(#[$attr:meta])*, pub(crate) $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => (
306+
$(#[$attr])*
307+
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i,$o, u32> {
308+
$submac!(i, $($args)*)
309+
}
310+
);
311+
($(#[$attr:meta])*, pub(crate) $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => (
312+
$(#[$attr])*
313+
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> {
314+
$submac!(i, $($args)*)
315+
}
316+
);
317+
($(#[$attr:meta])*, pub(crate) $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => (
318+
$(#[$attr])*
319+
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> {
320+
$submac!(i, $($args)*)
321+
}
322+
);
323+
($(#[$attr:meta])*, pub(crate) $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => (
324+
$(#[$attr])*
325+
pub(crate) fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, u32> {
326+
$submac!(i, $($args)*)
327+
}
328+
);
329+
($(#[$attr:meta])*, pub(crate) $name:ident, $submac:ident!( $($args:tt)* )) => (
330+
$(#[$attr])*
331+
pub(crate) fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], u32> {
332+
$submac!(i, $($args)*)
333+
}
334+
);
263335
);
264336

265337
/// Used to wrap common expressions and function as macros
@@ -1228,6 +1300,17 @@ mod tests {
12281300
assert_eq!(res, Done(&b""[..], a));
12291301
}
12301302

1303+
mod pub_crate_named_mod {
1304+
named!(pub(crate) tst, tag!("abcd"));
1305+
}
1306+
1307+
#[test]
1308+
fn pub_crate_named_test() {
1309+
let a = &b"abcd"[..];
1310+
let res = pub_crate_named_mod::tst(a);
1311+
assert_eq!(res, Done(&b""[..], a));
1312+
}
1313+
12311314
#[test]
12321315
fn apply_test() {
12331316
fn sum2(a:u8, b:u8) -> u8 { a + b }

0 commit comments

Comments
 (0)