Skip to content

Commit

Permalink
Add pub(crate) support
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-uk1 committed Sep 6, 2017
1 parent 460041a commit d384fba
Showing 1 changed file with 84 additions and 1 deletion.
85 changes: 84 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ macro_rules! closure (
/// // will use &[u8] as input type (use this if the compiler
/// // complains about lifetime issues
/// named!(my_function<&[u8]>, tag!("abcd"));
/// //prefix them with 'pub' to make the functions public
/// // prefix them with 'pub' to make the functions public
/// named!(pub my_function, tag!("abcd"));
/// // prefix them with 'pub(crate)' to make the functions public within the crate
/// named!(pub(crate) my_function, tag!("abcd"));
/// ```
#[macro_export]
macro_rules! named (
Expand Down Expand Up @@ -157,6 +159,36 @@ macro_rules! named (
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i,$o, u32> {
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> {
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> {
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, u32> {
$submac!(i, $($args)*)
}
);
(pub(crate) $name:ident, $submac:ident!( $($args:tt)* )) => (
#[allow(unused_variables)]
pub(crate) fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], u32> {
$submac!(i, $($args)*)
}
);
);

/// Makes a function from a parser combination with arguments.
Expand All @@ -172,6 +204,16 @@ macro_rules! named_args {
$submac!(input, $($args)*)
}
};
(pub(crate) $func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
pub(crate) fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> {
$submac!(input, $($args)*)
}
};
(pub(crate) $func_name:ident < 'a > ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
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> {
$submac!(input, $($args)*)
}
};
($func_name:ident ( $( $arg:ident : $typ:ty ),* ) < $return_type:ty > , $submac:ident!( $($args:tt)* ) ) => {
fn $func_name(input: &[u8], $( $arg : $typ ),*) -> $crate::IResult<&[u8], $return_type> {
$submac!(input, $($args)*)
Expand Down Expand Up @@ -260,6 +302,36 @@ macro_rules! named_attr (
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident( $i:ty ) -> $o:ty, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i,$o, u32> {
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident<$i:ty,$o:ty,$e:ty>, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, $e> {
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident<$i:ty,$o:ty>, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name( i: $i ) -> $crate::IResult<$i, $o, u32> {
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name( i: &[u8] ) -> $crate::IResult<&[u8], $o, u32> {
$submac!(i, $($args)*)
}
);
($(#[$attr:meta])*, pub(crate) $name:ident, $submac:ident!( $($args:tt)* )) => (
$(#[$attr])*
pub(crate) fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<&[u8], &[u8], u32> {
$submac!(i, $($args)*)
}
);
);

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

mod pub_crate_named_mod {
named!(pub(crate) tst, tag!("abcd"));
}

#[test]
fn pub_crate_named_test() {
let a = &b"abcd"[..];
let res = pub_crate_named_mod::tst(a);
assert_eq!(res, Done(&b""[..], a));
}

#[test]
fn apply_test() {
fn sum2(a:u8, b:u8) -> u8 { a + b }
Expand Down

0 comments on commit d384fba

Please sign in to comment.