mimicry_derive/lib.rs
1//! Procedural macros for [`mimicry`].
2//!
3//! See [`mimicry`] docs for examples of usage.
4//!
5//! [`mimicry`]: https://docs.rs/mimicry/
6
7#![recursion_limit = "128"]
8// Documentation settings.
9#![doc(html_root_url = "https://docs.rs/mimicry-derive/0.1.0")]
10// Linter settings.
11#![warn(missing_debug_implementations, missing_docs, bare_trait_objects)]
12#![warn(clippy::all, clippy::pedantic)]
13#![allow(clippy::must_use_candidate, clippy::module_name_repetitions)]
14
15extern crate proc_macro;
16
17mod call_real_impl;
18mod function;
19mod mock_impl;
20mod utils;
21
22use proc_macro::TokenStream;
23
24/// Derives the `Mock` trait for a type, allowing to use it as a state for mocking.
25///
26/// # Container attributes
27///
28/// Container attributes are placed in a `#[mock(...)]` attribute on a struct / enum.
29///
30/// ## `shared`
31///
32/// Signals to use the [`Shared`] wrapper for the mock state; by default,
33/// the [`ThreadLocal`] wrapper is used. Can be specified as `#[mock(shared)]` or
34/// `#[mock(shared = true)]`.
35///
36/// ## `mut`
37///
38/// Signals to use the [`Mut`] wrapper for the mock state. With this flag set, mock methods
39/// will receive `&Mut<Self>` as the first arg instead of `&self`.
40///
41/// # Examples
42///
43/// See [`ThreadLocal`] and [`Shared`] docs for examples of usage.
44///
45/// [`Shared`]: https://docs.rs/mimicry/latest/mimicry/struct.Shared.html
46/// [`ThreadLocal`]: https://docs.rs/mimicry/latest/mimicry/struct.ThreadLocal.html
47/// [`Mut`]: https://docs.rs/mimicry/latest/mimicry/struct.Mut.html
48#[proc_macro_derive(Mock, attributes(mock))]
49pub fn mock_derive(input: TokenStream) -> TokenStream {
50 mock_impl::impl_mock(input)
51}
52
53/// Derives the `CallReal` trait for a struct allowing to switch to real implementations
54/// for partial mocking or spying.
55///
56/// # Field attributes
57///
58/// Field attributes are placed in a `#[mock(...)]` attribute on a struct / enum.
59///
60/// ## `switch`
61///
62/// Indicates that a field is a [`RealCallSwitch`]. This is usually detected automatically
63/// by the field type, so an explicit declaration is reserved for extraordinary cases.
64/// Specified as `#[mock(switch)]`.
65///
66/// [`RealCallSwitch`]: https://docs.rs/mimicry/latest/mimicry/struct.RealCallSwitch.html
67#[proc_macro_derive(CallReal, attributes(mock))]
68pub fn call_real_derive(input: TokenStream) -> TokenStream {
69 call_real_impl::impl_call_real(input)
70}
71
72/// Injects mocking logic into a function / method.
73///
74/// You may want to use this attribute conditionally, e.g.,
75/// behind a `#[cfg_attr(test, _)]` wrapper.
76///
77/// # Attributes
78///
79/// Attributes are specified according to standard Rust conventions:
80/// `#[mock(attr1 = "value1", ...)]`.
81///
82/// ## `using`
83///
84/// Specifies a [path] string to the mock state. The path can point to the type of the mock state
85/// (e.g., `"mocks::State"`); in this case, the mock impl is an inherent function of the state
86/// with the same name as the mocked function / method. Alternatively, a path can specify
87/// the function name as well (e.g., `"mocks::State::mock_something"`); this is useful in case
88/// of name collision. The choice of these 2 options is auto-detected based on the last segment
89/// in the path: if it starts with an uppercase letter, it is considered a mock state type;
90/// otherwise, it is considered a type + function.
91///
92/// ## `rename`
93///
94/// Specifies a pattern to use when accessing mock impl methods. A pattern is a string with `{}`
95/// denoting a placeholder for the mocked function name. For example, `mock_{}` pattern will
96/// rename `len` to `mock_len`.
97///
98/// This attribute is mostly useful for impl blocks.
99///
100/// # Supported items
101///
102/// The `mock` attribute can be used on functions / methods. Pretty much all signatures
103/// are supported, e.g., generic functions, non-`'static` args, return types
104/// with dependent / elided lifetime, etc. `const` functions are not supported.
105///
106/// The `mock` attribute can also be placed on an impl block (including a trait implementation).
107/// In this case, it will apply to all methods in the block. If necessary, mocking options can
108/// be overridden for separate methods in the block by adding a `mock` attribute on them.
109///
110/// # Examples
111///
112/// See [`mimicry`] docs for examples of usage.
113///
114/// [path]: https://docs.rs/syn/latest/syn/struct.Path.html
115/// [`mimicry`]: https://docs.rs/mimicry/
116#[proc_macro_attribute]
117pub fn mock(attr: TokenStream, item: TokenStream) -> TokenStream {
118 function::wrap(attr, item)
119}