Skip to content

Commit

Permalink
Fix macro visibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Nov 19, 2021
1 parent 78d7b0e commit 666f14a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions libcnb-data/src/buildpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl Display for BuildpackApi {
}

libcnb_newtype!(
buildpack,
/// Construct a [`BuildpackId`] value at compile time.
///
/// Passing a string that is not a valid `BuildpackId` value will yield a compilation error.
Expand Down Expand Up @@ -212,6 +213,7 @@ libcnb_newtype!(
);

libcnb_newtype!(
buildpack,
/// Construct a [`StackId`] value at compile time.
///
/// Passing a string that is not a valid `StackId` value will yield a compilation error.
Expand Down
8 changes: 8 additions & 0 deletions libcnb-data/src/internals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This macro is used by all newtype literal macros to verify if the value matches the regex. It
// is not intended to be used outside of this crate. But since the code that macros expand to is
// just regular code, we need to expose this to users of this crate.
//
// We cannot use `::libcnb_proc_macros::verify_regex` in our macros directly as this would require
// every crate to explicitly import the `libcnb_proc_macros` crate as crates can't use code from
// transitive dependencies.
pub use libcnb_proc_macros::verify_regex;
7 changes: 5 additions & 2 deletions libcnb-data/src/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ pub struct Launch {
/// # Examples
/// ```
/// use libcnb_data::launch;
/// use libcnb_data::process_type;
///
/// let mut launch_toml = launch::Launch::new();
/// let web = launch::Process::new("web", "bundle", vec!["exec", "ruby", "app.rb"],
/// false, false).unwrap();
/// let web = launch::Process::new(process_type!("web"), "bundle", vec!["exec", "ruby", "app.rb"],
/// false, false);
///
/// launch_toml.processes.push(web);
/// assert!(toml::to_string(&launch_toml).is_ok());
Expand Down Expand Up @@ -88,6 +90,7 @@ pub struct Slice {
}

libcnb_newtype!(
launch,
/// Construct a [`ProcessType`] value at compile time.
///
/// Passing a string that is not a valid `ProcessType` value will yield a compilation error.
Expand Down
4 changes: 4 additions & 0 deletions libcnb-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ pub mod layer_content_metadata;
pub mod store;

mod newtypes;

// Internals that need to be public for macros
#[doc(hidden)]
pub mod internals;
13 changes: 11 additions & 2 deletions libcnb-data/src/newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ use libcnb_proc_macros as _;
/// # Usage:
/// ```
/// libcnb_newtype!(
/// // The module of this crate that exports the newtype publicly. Since it might differ from
/// // the actual module structure, the macro needs a way to determine how to import the type
/// // from a user's buildpack crate.
/// tests::doctest
/// /// RustDoc for the macro (optional)
/// buildpack_id,
/// /// RustDoc for the newtype itself (optional)
Expand All @@ -43,6 +47,7 @@ use libcnb_proc_macros as _;
/// ```
macro_rules! libcnb_newtype {
(
$path:path,
$(#[$macro_attributes:meta])*
$macro_name:ident,
$(#[$type_attributes:meta])*
Expand Down Expand Up @@ -117,10 +122,13 @@ macro_rules! libcnb_newtype {
$(#[$macro_attributes])*
macro_rules! $macro_name {
($value:expr) => {
::libcnb_proc_macros::verify_regex!(
$crate::internals::verify_regex!(
$regex,
$value,
$value.parse::<$name>().unwrap(),
{
use $crate::$path as base;
$value.parse::<base::$name>().unwrap()
},
compile_error!(concat!(
stringify!($value),
" is not a valid ",
Expand All @@ -140,6 +148,7 @@ mod test {
use super::libcnb_newtype;

libcnb_newtype!(
newtypes::test,
capitalized_name,
CapitalizedName,
CapitalizedNameError,
Expand Down
1 change: 0 additions & 1 deletion libcnb/examples/example-02-ruby-sample/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::layers::BundlerLayerLifecycle;
use crate::layers::RubyLayerLifecycle;
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
use libcnb::buildpack_main;
use libcnb::data::launch::ProcessType;
use libcnb::data::launch::{Launch, Process};
use libcnb::data::process_type;
use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder};
Expand Down
5 changes: 3 additions & 2 deletions libcnb/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ pub(crate) enum InnerBuildResult {
/// # Examples:
/// ```
/// use libcnb::build::BuildResultBuilder;
/// use libcnb_data::launch::{Launch, Process};
/// use libcnb::data::launch::{Launch, Process};
/// use libcnb::data::process_type;
///
/// let simple = BuildResultBuilder::new().build();
///
/// let with_launch = BuildResultBuilder::new()
/// .launch(Launch::new().process(Process::new("type", "command", vec!["-v"], false, false).unwrap()))
/// .launch(Launch::new().process(Process::new(process_type!("type"), "command", vec!["-v"], false, false)))
/// .build();
/// ```
pub struct BuildResultBuilder {
Expand Down

0 comments on commit 666f14a

Please sign in to comment.