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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use TokenStream;
use parse_macro_input;
/// Converts a struct of named components into a composable template.
///
/// This macro simplifies component-based model creation by transforming a
/// struct into a generic template and automatically generating a trait that
/// defines associated type mappings. This approach facilitates type-safe and
/// flexible composition of components.
///
/// When applied to a struct, this macro:
///
/// - Replaces the original struct with a generic version (`{StructName}`).
/// - Generates a trait (`{StructName}Types`) that preserves the original field
/// types and provides aliases to common struct variants.
///
/// ## Naming Conventions
///
/// - `{StructName}`: The generic version of the struct.
/// - `{StructName}Types`: A trait exposing:
/// - Associated types for each original field.
/// - Additional type aliases:
/// - `__Concrete`: The original struct type with concrete component types.
/// - `__Inputs`: A struct variant with fields using `<CompType as Component>::Input`.
/// - `__Outputs`: A struct variant with fields using `<CompType as Component>::Output`.
///
/// Field names are transformed into `UpperCamelCase` for generic parameters and
/// associated types.
///
/// ## Restrictions
///
/// - Structs must use named fields.
/// - Field types must implement `twine_core::Component`.
/// - Only documentation attributes (`///`) are permitted.
/// - Generic parameters are not supported.
///
/// ## Types Trait Usage
///
/// Access original field types generically:
///
/// ```ignore
/// type AddOneType = <() as MyComponentsTypes>::AddOne;
/// let adder: AddOneType = Adder::new(1);
///
/// // Access the concrete composed struct.
/// type Concrete = <() as MyComponentsTypes>::__Concrete;
/// let components: Concrete = MyComponents {
/// add_one: Adder::new(1),
/// add_two: Adder::new(2),
/// math: Arithmetic,
/// };
/// ```
///
/// ## Example
///
/// ### Input
///
/// ```ignore
/// #[composable]
/// pub struct MyComponents {
/// pub add_one: Adder<f64>,
/// pub add_two: Adder<f64>,
/// pub math: Arithmetic,
/// }
/// ```
///
/// ### Expanded
///
/// ```ignore
/// pub struct MyComponents<AddOne, AddTwo, Math> {
/// pub add_one: AddOne,
/// pub add_two: AddTwo,
/// pub math: Math,
/// }
///
/// pub trait MyComponentsTypes {
/// type __Concrete;
/// type __Inputs;
/// type __Outputs;
/// type AddOne;
/// type AddTwo;
/// type Math;
/// }
///
/// impl MyComponentsTypes for () {
/// type __Concrete = MyComponents<
/// Adder<f64>,
/// Adder<f64>,
/// Arithmetic
/// >;
///
/// type __Inputs = MyComponents<
/// <Adder<f64> as twine_core::Component>::Input,
/// <Adder<f64> as twine_core::Component>::Input,
/// <Arithmetic as twine_core::Component>::Input
/// >;
///
/// type __Outputs = MyComponents<
/// <Adder<f64> as twine_core::Component>::Output,
/// <Adder<f64> as twine_core::Component>::Output,
/// <Arithmetic as twine_core::Component>::Output
/// >;
///
/// type AddOne = Adder<f64>;
/// type AddTwo = Adder<f64>;
/// type Math = Arithmetic;
/// }
/// ```
/// Creates a component from multiple `#[composable]` components.
///
/// This macro generates a concrete struct implementing `twine_core::Component`.
/// The generated component wraps an internal `twine_core::Twine` chain,
/// automatically managing component execution order based on user-specified
/// connections between the top-level input and component outputs.
///
/// ## Restrictions
///
/// - Exactly two type aliases must be defined:
/// - `Input`: The top-level input type.
/// - `Components`: References a struct previously defined with `#[composable]`.
/// - Connection expressions may only reference fields from:
/// - The top-level `input`, or
/// - Outputs from other components (`output.{component_name}`).
/// - All referenced components must implement `twine_core::Component`.
/// - Cyclic dependencies between components are not currently permitted.
///
/// ## Example
///
/// ```ignore
/// #[composable]
/// pub struct CalcComponents {
/// pub adder: Adder<i32>,
/// pub multiplier: Multiplier<i32>,
/// }
///
/// pub struct CalcInput {
/// value: i32,
/// }
///
/// #[compose(Calculator)]
/// fn compose() {
/// type Input = CalcInput;
/// type Components = CalcComponents;
///
/// Connections {
/// adder: input.value,
/// multiplier: output.adder,
/// }
/// }
///
/// let calculator = Calculator::new(CalcComponents {
/// adder: Adder::new(1),
/// multiplier: Multiplier::new(2),
/// });
///
/// let result = calculator.call(CalcInput { value: 10 }).unwrap();
/// ```
/// Derives the `TimeIntegrable` trait for structs containing state variables.
///
/// This macro automates the implementation of time integration for simulation
/// state structs by generating the necessary boilerplate code for numerical
/// integration. It creates a derivatives struct and implements the required
/// traits for time-stepping operations.
///
/// When applied to a struct, this macro:
///
/// - Generates a derivatives struct (`{StructName}Dt`) with `TimeDerivativeOf<T>`
/// fields corresponding to each original field.
/// - Implements `Div<Time>` to convert state variables to their time derivatives.
/// - Implements `TimeIntegrable` to perform time-stepping integration.
///
/// ## Naming Conventions
///
/// - `{StructName}Dt`: A struct containing time derivatives of each field,
/// with field names suffixed with `_dt`.
///
/// ## Restrictions
///
/// - Structs must use named fields.
/// - Generic parameters are not supported.
/// - All field types must support division by `Time` and addition operations
/// required for integration (the compiler will enforce these constraints).
///
/// ## Integration Pattern
///
/// The generated code follows this mathematical pattern for each field:
///
/// ```text
/// new_value = old_value + derivative * dt
/// ```
///
/// Where `derivative` comes from dividing the field by time, and `dt` is the
/// integration time step.
///
/// ## Example
///
/// ### Input
///
/// ```ignore
/// #[derive(TimeIntegrable)]
/// struct StateVariables {
/// temperature: ThermodynamicTemperature,
/// pressure: Pressure,
/// }
/// ```
///
/// ### Expanded
///
/// ```ignore
/// struct StateVariablesDt {
/// temperature_dt: TimeDerivativeOf<ThermodynamicTemperature>,
/// pressure_dt: TimeDerivativeOf<Pressure>,
/// }
///
/// impl Div<Time> for StateVariables {
/// type Output = StateVariablesDt;
///
/// fn div(self, rhs: Time) -> Self::Output {
/// Self::Output {
/// temperature_dt: self.temperature / rhs,
/// pressure_dt: self.pressure / rhs,
/// }
/// }
/// }
///
/// impl TimeIntegrable for StateVariables {
/// fn step_by_time(self, derivative: StateVariablesDt, dt: Time) -> Self {
/// Self {
/// temperature: self.temperature + derivative.temperature_dt * dt,
/// pressure: self.pressure + derivative.pressure_dt * dt,
/// }
/// }
/// }
/// ```