git_checks_config/
macros.rs

1// Copyright Kitware, Inc.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use serde::de::DeserializeOwned;
10
11/// Trait for a deserialization structure of a check.
12///
13/// This trait should be implemented for any structure which can be deserialized and construct a
14/// check.
15#[allow(dead_code)] // Used in macros.
16pub trait IntoCheck: DeserializeOwned {
17    /// The check parsed by this configuration.
18    type Check;
19
20    /// Create a new instance of the check from the configuration.
21    fn into_check(self) -> Self::Check;
22}
23
24/// Register configuration structures with `inventory`.
25///
26/// A single check can implement multiple check traits, so this macro accepts multiple checks and
27/// multiple registrations with the same check. Ideally, check names should be unique globally, but
28/// nothing enforces this at the registration level.
29///
30/// ## Example
31///
32/// ```rust,ignore
33/// register_checks! {
34///     CheckConfig {
35///         "name" => CommitCheckConfig,
36///         "name/topic" => TopicCheckConfig,
37///     },
38///     OtherCheckConfig {
39///         "other_check" => BranchCheckConfig,
40///     },
41/// }
42/// ```
43#[macro_export]
44macro_rules! register_checks {
45    { $( $ty:ty { $( $name:expr => $tr:ty, )* }, )* } => {
46        $(
47            $(
48                $crate::inventory::submit! {
49                    <$tr>::new($name, $crate::CheckCtor { f: <$tr>::ctor::<$ty> })
50                }
51            )*
52        )*
53    };
54}