Skip to main content

facet_macros_impl/
lib.rs

1//! TeamDman-maintained fork of [facet-macros-impl](https://github.com/facet-rs/facet/tree/65bae5c31a7ce401bc44630fb96250ea884cfd3e/facet-macros-impl), published as
2//! [teamy-facet-macros-impl](https://crates.io/crates/teamy-facet-macros-impl).
3//! This fork uses its own Facet crate identities; use matching Teamy packages.
4//! Upstream MIT OR Apache-2.0 licensing and attribution are retained.
5//! See the [release notes](https://github.com/TeamDman/facet/blob/teamy-main/TEAMY_RELEASE.md).
6//!
7// unsynn's Parser API intentionally returns its large Error by value.
8#![allow(clippy::result_large_err)]
9#![warn(missing_docs)]
10#![allow(uncommon_codepoints)]
11//!
12//! [![crates.io](https://img.shields.io/crates/v/teamy-facet-macros-impl.svg)](https://crates.io/crates/teamy-facet-macros-impl)
13//! [![documentation](https://docs.rs/teamy-facet-macros-impl/badge.svg)](https://docs.rs/teamy-facet-macros-impl)
14//! [![MIT/Apache-2.0 licensed](https://img.shields.io/crates/l/teamy-facet-macros-impl.svg)](./LICENSE-MIT)
15//! [![Discord](https://img.shields.io/discord/1379550208551026748?logo=discord&label=discord)](https://discord.gg/JhD7CwCJ8F)
16//!
17//! Implementation of facet derive macros, combining parsing and code generation.
18//!
19//! This crate provides the internal implementation for `#[derive(Facet)]` and related procedural macros. It's used by `facet-macros` (the proc-macro crate) and should not be used directly.
20//!
21#![doc = include_str!("../readme-footer.md")]
22
23// ============================================================================
24// RE-EXPORTS FROM FACET-MACRO-TYPES (grammar) AND FACET-MACRO-PARSE (parsed types)
25// ============================================================================
26
27// Re-export everything from facet-macro-types (includes unsynn, grammar, RenameRule)
28pub use facet_macro_types::*;
29
30// Re-export everything from facet-macro-parse (includes parsed types like PStruct, PEnum, etc.)
31pub use facet_macro_parse::*;
32
33// ============================================================================
34// FUNCTION PARSING (optional feature)
35// ============================================================================
36
37/// Parse function signature shape
38#[cfg(feature = "function")]
39pub mod function;
40
41// ============================================================================
42// CODE EMISSION
43// ============================================================================
44
45mod process_enum;
46mod process_struct;
47
48// ============================================================================
49// DOC STRIPPING DETECTION
50// ============================================================================
51
52/// Returns true if doc strings should be stripped from generated shapes.
53///
54/// Controlled by `--cfg facet_no_doc`. Set via rustflags in .cargo/config.toml,
55/// Cargo.toml profile, or RUSTFLAGS env var. The cfg is evaluated when the
56/// proc-macro is compiled.
57#[cfg(all(feature = "doc", facet_no_doc))]
58pub const fn is_no_doc() -> bool {
59    true
60}
61
62/// Returns true if doc strings should be stripped from generated shapes.
63#[cfg(all(feature = "doc", not(facet_no_doc)))]
64pub const fn is_no_doc() -> bool {
65    false
66}
67
68mod derive;
69pub use derive::*;
70
71mod plugin;
72pub use plugin::*;
73
74mod extension;
75pub use extension::*;
76
77mod on_error;
78pub use on_error::*;
79
80/// Attribute grammar infrastructure for extension crates
81pub mod attr_grammar;
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86    use quote::quote;
87
88    #[test]
89    fn test_struct_with_field_doc_comments() {
90        let input = quote! {
91            #[derive(Facet)]
92            pub struct User {
93                #[doc = " The user's unique identifier"]
94                pub id: u64,
95            }
96        };
97
98        let mut it = input.to_token_iter();
99        let parsed = it.parse::<Struct>().expect("Failed to parse struct");
100
101        // Check that we parsed the struct correctly
102        assert_eq!(parsed.name.to_string(), "User");
103
104        // Extract fields from the struct
105        if let StructKind::Struct { fields, .. } = &parsed.kind {
106            assert_eq!(fields.content.len(), 1);
107
108            // Check first field (id)
109            let id_field = &fields.content[0].value;
110            assert_eq!(id_field.name.to_string(), "id");
111
112            // Extract doc comments from id field
113            let mut doc_found = false;
114            for attr in &id_field.attributes {
115                match &attr.body.content {
116                    AttributeInner::Doc(doc_inner) => {
117                        // This should work with LiteralString
118                        assert_eq!(doc_inner.value, " The user's unique identifier");
119                        doc_found = true;
120                    }
121                    _ => {
122                        // Skip non-doc attributes
123                    }
124                }
125            }
126            assert!(doc_found, "Should have found a doc comment");
127        } else {
128            panic!("Expected a regular struct with named fields");
129        }
130    }
131}