assert_type_match/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
mod args;
mod enums;
mod fields;
mod flags;
mod structs;
mod utils;
mod variants;
mod wrap;
use crate::args::Args;
use crate::enums::enum_assert;
use crate::structs::struct_assert;
use crate::wrap::wrap_assertions;
use proc_macro::TokenStream;
use syn::{parse_macro_input, Data, DeriveInput};
const ATTRIBUTE: &str = "assert_type_match";
/// An attribute macro that can be used to statically verify that the annotated struct or enum
/// matches the structure of a foreign type.
///
/// For structs, it will verify that the fields of the annotated struct match the fields of the
/// foreign struct.
///
/// For enums, it will verify that the variants of the annotated enum match the variants of the
/// foreign enum, and that the fields of each variant match the fields of the corresponding variant
/// in the foreign enum.
///
/// This will also output the original annotated struct or enum,
/// unless the `test_only` argument is set to `true`.
///
/// # Arguments
///
/// This macro accepts arguments to control its behavior.
/// These arguments are passed as a comma-separated list after the foreign type.
///
/// All boolean arguments may be set to true by using either the `foo` or `foo = true` syntax.
///
/// ## `test_only`
///
/// Type: `bool`
///
/// Controls whether to output the annotated struct or enum in the generated code.
///
/// ## `from`
///
/// Type: `bool`
///
/// Controls whether a `From` implementation should be generated.
///
/// If true, two `From` implementations will be generated:
/// one for converting from the annotated type to the foreign type,
/// and one for converting from the foreign type to the annotated type.
///
/// ## `skip_name`
///
/// Type: `bool`
///
/// Controls whether checking that the name of the annotated struct or enum matches
/// the name of the foreign type should be skipped.
///
/// For example, comparing `struct Foo(u32)` to `struct Bar(u32)` would pass
/// when this argument is set to `true`.
///
/// ## `skip_types`
///
/// Type: `bool`
///
/// Controls whether checking field types should be skipped.
///
/// For example, comparing `struct Foo(i32)` to `struct Foo(f32)` would pass
/// when this argument is set to `true`.
///
/// ## Field Arguments
///
/// This macro also supports field attributes.
///
/// These are also defined with the `#[assert_type_match(...)]` attribute.
///
/// ### `skip`
///
/// Type: `bool`
///
/// Controls whether the field should be skipped.
///
/// This allows you to skip fields that are not present on the foreign type.
///
/// ### `skip_type`
///
/// Type: `bool`
///
/// Controls whether checking the field type should be skipped.
///
/// ## Variant Arguments
///
/// This macro also supports variant attributes.
///
/// These are also defined with the `#[assert_type_match(...)]` attribute.
///
/// ### `skip`
///
/// Type: `bool`
///
/// Controls whether the variant should be skipped.
///
/// This allows you to skip variants that are not present on the foreign type.
///
/// # Example
///
/// A passing example:
///
/// ```
/// # use assert_type_match::assert_type_match;
/// mod other {
/// pub struct Test {
/// pub x: i32,
/// pub y: i32,
/// }
/// }
///
/// #[assert_type_match(other::Test)]
/// struct Test {
/// x: i32,
/// y: i32,
/// }
/// ```
///
/// A failing example:
///
/// ```compile_fail
/// # use assert_type_match::assert_type_match;
/// mod other {
/// pub struct Test {
/// pub x: i32,
/// pub y: i32,
/// }
/// }
///
/// #[assert_type_match(other::Test)]
/// struct Test {
/// x: i32,
/// z: i32, // Error: struct `other::Test` has no field named `z`
/// }
/// ```
///
#[proc_macro_attribute]
pub fn assert_type_match(args: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let args = parse_macro_input!(args as Args);
wrap_assertions(input, args, |input, args| match &input.data {
Data::Struct(data) => struct_assert(data, input, args),
Data::Enum(data) => enum_assert(data, input, args),
Data::Union(data) => Err(syn::Error::new(
data.union_token.span,
"unions are not supported",
)),
})
.into()
}