Skip to main content

servo_deny_public_fields/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use synstructure::{self, decl_derive};
6
7decl_derive!([DenyPublicFields] => deny_public_fields_derive);
8
9fn deny_public_fields_derive(s: synstructure::Structure) -> proc_macro2::TokenStream {
10    s.each(|binding| {
11        if binding.ast().vis != syn::Visibility::Inherited {
12            panic!(
13                "Field `{}` should not be public",
14                binding.ast().ident.as_ref().unwrap_or(&binding.binding)
15            );
16        }
17
18        proc_macro2::TokenStream::new()
19    });
20
21    proc_macro2::TokenStream::new()
22}
23
24#[test]
25#[should_panic(expected = "Field `v1` should not be public")]
26fn deny_public_fields_failing() {
27    synstructure::test_derive! {
28        deny_public_fields_derive {
29            struct Foo {
30                pub v1: i32,
31                v2: i32
32            }
33        }
34        expands to {}
35    };
36}
37
38#[test]
39fn deny_public_fields_ok() {
40    synstructure::test_derive! {
41        deny_public_fields_derive {
42            struct Foo {
43                v1: i32,
44                v2: i32
45            }
46        }
47        expands to {}
48    };
49}