Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying a crate root in the automock proc macro. #520

Open
Jezza opened this issue Oct 18, 2023 · 3 comments
Open

Allow specifying a crate root in the automock proc macro. #520

Jezza opened this issue Oct 18, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@Jezza
Copy link

Jezza commented Oct 18, 2023

There's a lot of hardcoded ::mockall in the crate.

It makes it very difficult to use it in some common crate as users of this common crate need to also pull in mockall.

It should work very similar to how serde does it.
https://serde.rs/container-attrs.html#crate

@asomers
Copy link
Owner

asomers commented Oct 19, 2023

Are you talking about some kind of transitive dependency? Can you provide an example that demonstrates your problem?

@Jezza
Copy link
Author

Jezza commented Oct 19, 2023

More or less.

We have a common crate that we use to work with tonic, and in that common crate, we have a macro used to generate a lot of the boilerplate we need.

We generate some stuff, including a Client trait.
On that trait, we slap a #[cfg_attr(any(test, feature = "mock"), $crate::export::mockall::automock)]

This gives us the ability to use all the amazing features of mockall.

We need to invoke that macro with the context of the callee crate.
Ideally, this crate just needs to pull in our common library, and we'll handle stuff for it.
But because mockall uses ::mockall, we still have to pull in mockall.

Common crate:

#[macro_export]
macro_rules! client {
    (
        impl $trait_:ident for $client:ident {
            $(
                fn $fn_name:ident($req_ty:path) -> $resp_ty:path;
            )*
        }
    ) => {
      #[cfg_attr(any(test, feature = "mock"), $crate::export::mockall::automock)]
      pub trait $trait_ {}
    };
)

Crate 1:

// mockall will generate the mock here. (`::mockall`)
// but this crate might not have the mockall dependency.
common_crate::client! {
  ...
}

I hope that clarifies it. :D

@asomers asomers added the enhancement New feature or request label Oct 19, 2023
@asomers
Copy link
Owner

asomers commented Oct 19, 2023

And what change are you looking for? Can you show an example of how you would like to use this new feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants