facet_macro_parse/lib.rs
1//! TeamDman-maintained fork of [facet-macro-parse](https://github.com/facet-rs/facet/tree/65bae5c31a7ce401bc44630fb96250ea884cfd3e/facet-macro-parse), published as
2//! [teamy-facet-macro-parse](https://crates.io/crates/teamy-facet-macro-parse).
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//! # Facet Macro Parse
8//!
9//! Parsed type representations for the facet macro ecosystem.
10//!
11//! This crate provides the parsed types (`PStruct`, `PEnum`, `PContainer`, `PAttrs`, etc.)
12//! that represent the result of parsing Rust type definitions for code generation.
13//!
14//! It depends on `facet-macro-types` for the underlying unsynn grammar.
15
16#![allow(uncommon_codepoints)]
17
18// Re-export everything from facet-macro-types for convenience
19pub use facet_macro_types::*;
20
21mod parsed;
22pub use parsed::*;
23
24mod generic_params;
25pub use generic_params::*;
26
27mod unescaping;
28pub use unescaping::*;
29
30// ============================================================================
31// CONVENIENCE PARSING FUNCTIONS
32// ============================================================================
33
34/// Represents a parsed type (either a struct or an enum)
35pub enum PType {
36 /// A parsed struct
37 Struct(PStruct),
38 /// A parsed enum
39 Enum(PEnum),
40}
41
42impl PType {
43 /// Get the name identifier of the type
44 pub const fn name(&self) -> &Ident {
45 match self {
46 PType::Struct(s) => &s.container.name,
47 PType::Enum(e) => &e.container.name,
48 }
49 }
50}
51
52/// Parse a TokenStream into a `PType` (either struct or enum).
53///
54/// This is a convenience function that tries to parse the token stream
55/// as either a struct or an enum.
56pub fn parse_type(tokens: TokenStream) -> std::result::Result<PType, String> {
57 let mut iter = tokens.to_token_iter();
58
59 // Try to parse as AdtDecl which can be either Struct or Enum
60 match iter.parse::<AdtDecl>() {
61 Ok(AdtDecl::Struct(s)) => Ok(PType::Struct(PStruct::parse(&s))),
62 Ok(AdtDecl::Enum(e)) => Ok(PType::Enum(PEnum::parse(&e))),
63 Err(e) => Err(format!("failed to parse type: {e}")),
64 }
65}