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
use TokenStream;
use ;
/// Generates type-safe structural typing support for a struct.
///
/// This macro transforms a struct to track which fields are present/absent at the type level,
/// enabling compile-time enforcement of field requirements.
///
/// **Note**: If you use `#[derive(...)]` on your struct, you must add `derive-where` to your dependencies:
/// ```bash
/// cargo add derive-where
/// ```
///
/// # Generated Code
///
/// For a struct `MyStruct`, this macro generates:
///
/// - A `my_struct` module with `Fields` trait, `FieldSet<...>` type, and `with::` aliases
/// - Builder methods: `.field(value)`
/// - `.merge()`, `.extract()`, `.try_extract()` operations
///
/// Supports user-defined generic type parameters and nested structural types (see `examples/nested.rs`).
///
/// # Field States
///
/// - **Present**: Value of type `T`
/// - **Optional**: `Option<T>`
/// - **Absent**: `PhantomData<T>`
///
/// # Example
///
/// ```ignore
/// use structural_typing::{structural, presence::Present};
///
/// #[structural]
/// struct User {
/// name: String,
/// email: String,
/// }
///
/// // Build incrementally
/// let user = User::empty().name("Alice".to_owned());
///
/// // Methods can require specific fields
/// impl<F: user::Fields<name = Present>> User<F> {
/// fn greet(&self) -> String {
/// format!("Hello, {}!", self.name)
/// }
/// }
///
/// user.greet(); // ✓ Compiles
/// ```
///
/// # Serde Support
///
/// Deserialization uses a helper struct where all fields are `Option<T>` with `#[serde(default)]`,
/// then converts via `TryFrom`. Present fields error if missing, Optional fields become `Some`/`None`,
/// Absent fields are ignored.
///
/// **Limitations:**
/// - Present fields cannot use the field type's `Default` impl during deserialization
/// - Optional fields cannot distinguish between missing and explicit `null` in JSON
/// - Absent fields silently ignore data if present in input
/// - All field types must implement `Deserialize` even if marked Absent
///
/// **Custom deserializers:** If using `#[serde(deserialize_with)]`, the function must produce `Option<T>`.
///
/// **Incompatible:** `#[serde(default)]`, `#[serde(skip)]`, `#[serde(skip_deserializing)]`, `#[serde(flatten)]`.
/// **Compatible:** `rename`, `alias`, `rename_all`, `deserialize_with` (with `Option<T>` output).
///
/// # Restrictions
///
/// - Named structs only
/// - At least one field