Skip to content

Commit

Permalink
Fix builder macro hygiene
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Sep 10, 2023
1 parent 75bc082 commit c4babaa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
29 changes: 23 additions & 6 deletions cursive-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,11 +1328,22 @@ inventory::collect!(CallbackRecipe);
#[cfg(feature = "builder")]
inventory::collect!(WrapperRecipe);

#[cfg(not(feature = "builder"))]
#[macro_export]
/// Define a recipe to build this view from a config file.
macro_rules! raw_recipe {
($name:ident from $config_builder:expr) => {
};
(with $name:ident, $builder:expr) => {
};
($name:ident, $builder:expr) => {
};
}
#[cfg(feature = "builder")]
#[macro_export]
/// Define a recipe to build this view from a config file.
macro_rules! raw_recipe {
($name:ident from $config_builder:expr) => {
#[cfg(feature = "builder")]
$crate::submit! {
$crate::builder::Recipe {
name: stringify!($name),
Expand All @@ -1344,12 +1355,11 @@ macro_rules! raw_recipe {
}
};
(with $name:ident, $builder:expr) => {
#[cfg(feature = "builder")]
$crate::submit! {
$crate::builder::WrapperRecipe {
name: stringify!($name),
builder: |config, context| {
let builder: fn(&::serde_json::Value, &$crate::builder::Context) -> Result<_, $crate::builder::Error> = $builder;
let builder: fn(&$crate::reexports::serde_json::Value, &$crate::builder::Context) -> Result<_, $crate::builder::Error> = $builder;
let wrapper = (builder)(config, context)?;

Ok(Box::new(move |view| {
Expand All @@ -1361,24 +1371,31 @@ macro_rules! raw_recipe {
}
};
($name:ident, $builder:expr) => {
#[cfg(feature = "builder")]
$crate::submit! {
$crate::builder::Recipe {
name: stringify!($name),
builder: |config, context| {
let builder: fn(&::serde_json::Value, &$crate::builder::Context) -> Result<_,$crate::builder::Error> = $builder;
let builder: fn(&$crate::reexports::serde_json::Value, &$crate::builder::Context) -> Result<_,$crate::builder::Error> = $builder;
(builder)(config, context).map($crate::views::BoxedView::boxed)
},
}
}
};
}

#[cfg(not(feature = "builder"))]
#[macro_export]
/// Define a macro for a variable builder.
macro_rules! var_recipe {
($name: expr, $builder:expr) => {
};
}

#[cfg(feature = "builder")]
#[macro_export]
/// Define a macro for a variable builder.
macro_rules! var_recipe {
($name: expr, $builder:expr) => {
#[cfg(feature = "builder")]
$crate::submit! {
$crate::builder::CallbackRecipe {
name: $name,
Expand Down
2 changes: 2 additions & 0 deletions cursive-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub mod reexports {

#[cfg(feature = "ansi")]
pub use ansi_parser;

pub use serde_json;
}

pub use cursive_macros::{callback_helpers, recipe};
Expand Down

0 comments on commit c4babaa

Please sign in to comment.