facet_macros_impl/
lib.rs

1#![warn(missing_docs)]
2#![allow(uncommon_codepoints)]
3#![doc = include_str!("../README.md")]
4
5// ============================================================================
6// RE-EXPORTS FROM FACET-MACRO-TYPES (grammar) AND FACET-MACRO-PARSE (parsed types)
7// ============================================================================
8
9// Re-export everything from facet-macro-types (includes unsynn, grammar, RenameRule)
10pub use facet_macro_types::*;
11
12// Re-export everything from facet-macro-parse (includes parsed types like PStruct, PEnum, etc.)
13pub use facet_macro_parse::*;
14
15// ============================================================================
16// FUNCTION PARSING (optional feature)
17// ============================================================================
18
19/// Parse function signature shape
20#[cfg(feature = "function")]
21pub mod function;
22
23// ============================================================================
24// CODE EMISSION
25// ============================================================================
26
27mod process_enum;
28mod process_struct;
29
30mod derive;
31pub use derive::*;
32
33mod extension;
34pub use extension::*;
35
36mod on_error;
37pub use on_error::*;
38
39/// Attribute grammar infrastructure for extension crates
40pub mod attr_grammar;
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use quote::quote;
46
47    #[test]
48    fn test_struct_with_field_doc_comments() {
49        let input = quote! {
50            #[derive(Facet)]
51            pub struct User {
52                #[doc = " The user's unique identifier"]
53                pub id: u64,
54            }
55        };
56
57        let mut it = input.to_token_iter();
58        let parsed = it.parse::<Struct>().expect("Failed to parse struct");
59
60        // Check that we parsed the struct correctly
61        assert_eq!(parsed.name.to_string(), "User");
62
63        // Extract fields from the struct
64        if let StructKind::Struct { fields, .. } = &parsed.kind {
65            assert_eq!(fields.content.len(), 1);
66
67            // Check first field (id)
68            let id_field = &fields.content[0].value;
69            assert_eq!(id_field.name.to_string(), "id");
70
71            // Extract doc comments from id field
72            let mut doc_found = false;
73            for attr in &id_field.attributes {
74                match &attr.body.content {
75                    AttributeInner::Doc(doc_inner) => {
76                        // This should work with LiteralString
77                        assert_eq!(doc_inner.value, " The user's unique identifier");
78                        doc_found = true;
79                    }
80                    _ => {
81                        // Skip non-doc attributes
82                    }
83                }
84            }
85            assert!(doc_found, "Should have found a doc comment");
86        } else {
87            panic!("Expected a regular struct with named fields");
88        }
89    }
90}