macroforge 0.1.33 → 0.1.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -339
- package/index.d.ts +955 -21
- package/index.js +52 -52
- package/js/serde/index.d.ts +242 -16
- package/js/serde/index.mjs +12 -4
- package/js/serde/index.ts +264 -21
- package/js/traits/index.d.ts +227 -2
- package/js/traits/index.ts +229 -11
- package/js/utils/index.d.ts +41 -0
- package/js/utils/index.ts +42 -0
- package/package.json +7 -7
package/js/traits/index.ts
CHANGED
|
@@ -1,51 +1,269 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* # Macroforge Traits Module
|
|
3
|
+
*
|
|
4
|
+
* This module defines TypeScript interfaces that correspond to Rust-like traits.
|
|
5
|
+
* These interfaces describe the shape of the methods generated by Macroforge's
|
|
6
|
+
* derive macros for interfaces, enums, and type aliases.
|
|
7
|
+
*
|
|
8
|
+
* For **classes**, the generated methods are instance methods (e.g., `user.clone()`).
|
|
9
|
+
* For **interfaces/enums/type aliases**, the generated methods are namespace
|
|
10
|
+
* functions that take the value as the first parameter (e.g., `User.clone(user)`).
|
|
11
|
+
*
|
|
12
|
+
* ## Usage
|
|
13
|
+
*
|
|
14
|
+
* These traits are primarily used for type checking generated code:
|
|
15
|
+
*
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import type { Clone, Debug } from "macroforge/traits";
|
|
18
|
+
*
|
|
19
|
+
* // Type-check that a namespace implements Clone
|
|
20
|
+
* const UserNs: Clone<User> = User;
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @module macroforge/traits
|
|
4
24
|
*/
|
|
5
25
|
|
|
6
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Trait for types that can be deep-copied.
|
|
28
|
+
*
|
|
29
|
+
* Analogous to Rust's `Clone` trait. The generated `clone` method creates
|
|
30
|
+
* an independent copy of the value.
|
|
31
|
+
*
|
|
32
|
+
* @template T - The type being cloned
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* // For interfaces/type aliases, use as namespace:
|
|
37
|
+
* const cloned = UserNs.clone(original);
|
|
38
|
+
*
|
|
39
|
+
* // For classes, methods are on the instance:
|
|
40
|
+
* const cloned = original.clone();
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
7
43
|
export interface Clone<T> {
|
|
44
|
+
/**
|
|
45
|
+
* Creates a deep copy of the value.
|
|
46
|
+
* @param self - The value to clone
|
|
47
|
+
* @returns A new independent copy of the value
|
|
48
|
+
*/
|
|
8
49
|
readonly clone: (self: T) => T;
|
|
9
50
|
}
|
|
10
51
|
|
|
11
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Trait for types with a human-readable string representation.
|
|
54
|
+
*
|
|
55
|
+
* Analogous to Rust's `Debug` trait. The generated `toString` method
|
|
56
|
+
* produces a string like `"TypeName { field1: value1, field2: value2 }"`.
|
|
57
|
+
*
|
|
58
|
+
* @template T - The type being formatted
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* console.log(User.toString(user));
|
|
63
|
+
* // Output: "User { id: 1, name: Alice }"
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
12
66
|
export interface Debug<T> {
|
|
67
|
+
/**
|
|
68
|
+
* Returns a debug string representation of the value.
|
|
69
|
+
* @param self - The value to format
|
|
70
|
+
* @returns A human-readable string for debugging
|
|
71
|
+
*/
|
|
13
72
|
readonly toString: (self: T) => string;
|
|
14
73
|
}
|
|
15
74
|
|
|
16
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Trait for types with a default value.
|
|
77
|
+
*
|
|
78
|
+
* Analogous to Rust's `Default` trait. The generated `defaultValue` method
|
|
79
|
+
* creates an instance with all fields set to their default values.
|
|
80
|
+
*
|
|
81
|
+
* @template T - The type being constructed
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const user = User.defaultValue();
|
|
86
|
+
* // Creates: { id: 0, name: "", active: false }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
17
89
|
export interface Default<T> {
|
|
90
|
+
/**
|
|
91
|
+
* Creates a new instance with default values for all fields.
|
|
92
|
+
* @returns A new instance with default field values
|
|
93
|
+
*/
|
|
18
94
|
readonly defaultValue: () => T;
|
|
19
95
|
}
|
|
20
96
|
|
|
21
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Trait for types that support equality comparison.
|
|
99
|
+
*
|
|
100
|
+
* Analogous to Rust's `PartialEq` trait. The generated `equals` method
|
|
101
|
+
* performs structural equality comparison on all non-skipped fields.
|
|
102
|
+
*
|
|
103
|
+
* @template T - The type being compared
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* if (User.equals(user1, user2)) {
|
|
108
|
+
* console.log("Users are equal");
|
|
109
|
+
* }
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
22
112
|
export interface PartialEq<T> {
|
|
113
|
+
/**
|
|
114
|
+
* Compares two values for equality.
|
|
115
|
+
* @param self - The first value
|
|
116
|
+
* @param other - The second value (accepts unknown for flexibility)
|
|
117
|
+
* @returns `true` if the values are equal, `false` otherwise
|
|
118
|
+
*/
|
|
23
119
|
readonly equals: (self: T, other: unknown) => boolean;
|
|
24
120
|
}
|
|
25
121
|
|
|
26
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Trait for types that can produce a hash code.
|
|
124
|
+
*
|
|
125
|
+
* Analogous to Rust's `Hash` trait. The generated `hashCode` method
|
|
126
|
+
* computes a 32-bit integer hash suitable for use in hash-based collections.
|
|
127
|
+
*
|
|
128
|
+
* **Hash Contract**: Objects that are equal (via `PartialEq`) must produce
|
|
129
|
+
* the same hash code.
|
|
130
|
+
*
|
|
131
|
+
* @template T - The type being hashed
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const hash = User.hashCode(user);
|
|
136
|
+
* // Use in Map: map.set(hash, user);
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
27
139
|
export interface Hash<T> {
|
|
140
|
+
/**
|
|
141
|
+
* Computes a hash code for the value.
|
|
142
|
+
* @param self - The value to hash
|
|
143
|
+
* @returns A 32-bit integer hash code
|
|
144
|
+
*/
|
|
28
145
|
readonly hashCode: (self: T) => number;
|
|
29
146
|
}
|
|
30
147
|
|
|
31
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Trait for types with partial ordering.
|
|
150
|
+
*
|
|
151
|
+
* Analogous to Rust's `PartialOrd` trait. The generated `compareTo` method
|
|
152
|
+
* returns a number for comparable values, or `null` for incomparable values.
|
|
153
|
+
*
|
|
154
|
+
* @template T - The type being compared
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const cmp = User.compareTo(user1, user2);
|
|
159
|
+
* if (cmp !== null) {
|
|
160
|
+
* if (cmp < 0) console.log("user1 < user2");
|
|
161
|
+
* else if (cmp > 0) console.log("user1 > user2");
|
|
162
|
+
* else console.log("user1 == user2");
|
|
163
|
+
* } else {
|
|
164
|
+
* console.log("Incomparable");
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
32
168
|
export interface PartialOrd<T> {
|
|
169
|
+
/**
|
|
170
|
+
* Compares two values for ordering.
|
|
171
|
+
* @param self - The first value
|
|
172
|
+
* @param other - The second value
|
|
173
|
+
* @returns `-1` if self < other, `0` if equal, `1` if self > other, or `null` if incomparable
|
|
174
|
+
*/
|
|
33
175
|
readonly compareTo: (self: T, other: unknown) => number | null;
|
|
34
176
|
}
|
|
35
177
|
|
|
36
|
-
|
|
178
|
+
/**
|
|
179
|
+
* Trait for types with total ordering.
|
|
180
|
+
*
|
|
181
|
+
* Analogous to Rust's `Ord` trait. The generated `compareTo` method
|
|
182
|
+
* always returns a number (never null) - all values are comparable.
|
|
183
|
+
*
|
|
184
|
+
* @template T - The type being compared
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* // Sort an array using Ord
|
|
189
|
+
* users.sort((a, b) => User.compareTo(a, b));
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
37
192
|
export interface Ord<T> {
|
|
193
|
+
/**
|
|
194
|
+
* Compares two values for ordering (total order).
|
|
195
|
+
* @param self - The first value
|
|
196
|
+
* @param other - The second value
|
|
197
|
+
* @returns `-1` if self < other, `0` if equal, `1` if self > other
|
|
198
|
+
*/
|
|
38
199
|
readonly compareTo: (self: T, other: T) => number;
|
|
39
200
|
}
|
|
40
201
|
|
|
41
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Trait for types that can be serialized to JSON.
|
|
204
|
+
*
|
|
205
|
+
* Analogous to Rust's serde `Serialize` trait. The generated methods
|
|
206
|
+
* convert objects to JSON with cycle detection via `__id`/`__ref` markers.
|
|
207
|
+
*
|
|
208
|
+
* @template T - The type being serialized
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```typescript
|
|
212
|
+
* const json = User.toStringifiedJSON(user);
|
|
213
|
+
* // => '{"__type":"User","__id":1,"name":"Alice"}'
|
|
214
|
+
*
|
|
215
|
+
* const obj = User.toObject(user);
|
|
216
|
+
* // => { __type: "User", __id: 1, name: "Alice" }
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
42
219
|
export interface Serialize<T> {
|
|
220
|
+
/**
|
|
221
|
+
* Serializes the value to a JSON string.
|
|
222
|
+
* @param self - The value to serialize
|
|
223
|
+
* @returns JSON string with `__type` and `__id` markers
|
|
224
|
+
*/
|
|
43
225
|
readonly toStringifiedJSON: (self: T) => string;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Converts the value to a plain JavaScript object.
|
|
229
|
+
* @param self - The value to convert
|
|
230
|
+
* @returns Plain object suitable for JSON.stringify
|
|
231
|
+
*/
|
|
44
232
|
readonly toObject: (self: T) => Record<string, unknown>;
|
|
45
233
|
}
|
|
46
234
|
|
|
47
|
-
|
|
235
|
+
/**
|
|
236
|
+
* Trait for types that can be deserialized from JSON.
|
|
237
|
+
*
|
|
238
|
+
* Analogous to Rust's serde `Deserialize` trait. The generated methods
|
|
239
|
+
* parse JSON, resolve `__ref` markers, and validate field values.
|
|
240
|
+
*
|
|
241
|
+
* @template T - The type being deserialized
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* import { Result } from "macroforge/utils";
|
|
246
|
+
*
|
|
247
|
+
* const result = User.fromStringifiedJSON(json);
|
|
248
|
+
* if (Result.isOk(result)) {
|
|
249
|
+
* const user = result.value;
|
|
250
|
+
* } else {
|
|
251
|
+
* console.error(result.error); // Validation errors
|
|
252
|
+
* }
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
48
255
|
export interface Deserialize<T> {
|
|
256
|
+
/**
|
|
257
|
+
* Deserializes a value from a JSON string.
|
|
258
|
+
* @param json - JSON string to parse
|
|
259
|
+
* @returns The deserialized value (may throw on invalid input)
|
|
260
|
+
*/
|
|
49
261
|
readonly fromStringifiedJSON: (json: string) => T;
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Deserializes a value from a plain JavaScript object.
|
|
265
|
+
* @param obj - Object to deserialize from
|
|
266
|
+
* @returns The deserialized value (may throw on invalid input)
|
|
267
|
+
*/
|
|
50
268
|
readonly fromObject: (obj: unknown) => T;
|
|
51
269
|
}
|
package/js/utils/index.d.ts
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # Macroforge Utils Module
|
|
3
|
+
*
|
|
4
|
+
* This module provides Rust-like utility types for use in generated macro code.
|
|
5
|
+
* It re-exports `Result` and `Option` from `@rydshift/mirror` to provide
|
|
6
|
+
* ergonomic error handling and null safety in TypeScript.
|
|
7
|
+
*
|
|
8
|
+
* ## Result<T, E>
|
|
9
|
+
*
|
|
10
|
+
* A discriminated union representing either success (`Ok<T>`) or failure (`Err<E>`).
|
|
11
|
+
* Used by the `Deserialize` macro for validation errors.
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { Result } from "macroforge/utils";
|
|
15
|
+
*
|
|
16
|
+
* const result = User.fromStringifiedJSON(json);
|
|
17
|
+
* if (Result.isOk(result)) {
|
|
18
|
+
* const user = result.value;
|
|
19
|
+
* } else {
|
|
20
|
+
* console.error(result.error); // Array of field errors
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* ## Option<T>
|
|
25
|
+
*
|
|
26
|
+
* A discriminated union representing either a value (`Some<T>`) or absence (`None`).
|
|
27
|
+
* Used by `PartialOrd` macro for incomparable values.
|
|
28
|
+
*
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { Option } from "macroforge/utils";
|
|
31
|
+
*
|
|
32
|
+
* const cmp = user1.compareTo(user2);
|
|
33
|
+
* if (Option.isSome(cmp)) {
|
|
34
|
+
* console.log(cmp.value); // -1, 0, or 1
|
|
35
|
+
* } else {
|
|
36
|
+
* console.log("Values are incomparable");
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @module macroforge/utils
|
|
41
|
+
*/
|
|
1
42
|
export { Result, Option } from "@rydshift/mirror/declarative";
|
|
2
43
|
/**
|
|
3
44
|
*
|
package/js/utils/index.ts
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # Macroforge Utils Module
|
|
3
|
+
*
|
|
4
|
+
* This module provides Rust-like utility types for use in generated macro code.
|
|
5
|
+
* It re-exports `Result` and `Option` from `@rydshift/mirror` to provide
|
|
6
|
+
* ergonomic error handling and null safety in TypeScript.
|
|
7
|
+
*
|
|
8
|
+
* ## Result<T, E>
|
|
9
|
+
*
|
|
10
|
+
* A discriminated union representing either success (`Ok<T>`) or failure (`Err<E>`).
|
|
11
|
+
* Used by the `Deserialize` macro for validation errors.
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { Result } from "macroforge/utils";
|
|
15
|
+
*
|
|
16
|
+
* const result = User.fromStringifiedJSON(json);
|
|
17
|
+
* if (Result.isOk(result)) {
|
|
18
|
+
* const user = result.value;
|
|
19
|
+
* } else {
|
|
20
|
+
* console.error(result.error); // Array of field errors
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* ## Option<T>
|
|
25
|
+
*
|
|
26
|
+
* A discriminated union representing either a value (`Some<T>`) or absence (`None`).
|
|
27
|
+
* Used by `PartialOrd` macro for incomparable values.
|
|
28
|
+
*
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { Option } from "macroforge/utils";
|
|
31
|
+
*
|
|
32
|
+
* const cmp = user1.compareTo(user2);
|
|
33
|
+
* if (Option.isSome(cmp)) {
|
|
34
|
+
* console.log(cmp.value); // -1, 0, or 1
|
|
35
|
+
* } else {
|
|
36
|
+
* console.log("Values are incomparable");
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @module macroforge/utils
|
|
41
|
+
*/
|
|
42
|
+
|
|
1
43
|
// Re-export Result and Option from @rydshift/mirror for use in generated code
|
|
2
44
|
export { Result, Option } from "@rydshift/mirror/declarative";
|
|
3
45
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "macroforge",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.34",
|
|
4
4
|
"description": "TypeScript macro expansion engine powered by Rust and SWC",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -67,12 +67,12 @@
|
|
|
67
67
|
"node": ">= 18"
|
|
68
68
|
},
|
|
69
69
|
"optionalDependencies": {
|
|
70
|
-
"@macroforge/bin-darwin-x64": "0.1.
|
|
71
|
-
"@macroforge/bin-darwin-arm64": "0.1.
|
|
72
|
-
"@macroforge/bin-linux-x64-gnu": "0.1.
|
|
73
|
-
"@macroforge/bin-linux-arm64-gnu": "0.1.
|
|
74
|
-
"@macroforge/bin-win32-x64-msvc": "0.1.
|
|
75
|
-
"@macroforge/bin-win32-arm64-msvc": "0.1.
|
|
70
|
+
"@macroforge/bin-darwin-x64": "0.1.34",
|
|
71
|
+
"@macroforge/bin-darwin-arm64": "0.1.34",
|
|
72
|
+
"@macroforge/bin-linux-x64-gnu": "0.1.34",
|
|
73
|
+
"@macroforge/bin-linux-arm64-gnu": "0.1.34",
|
|
74
|
+
"@macroforge/bin-win32-x64-msvc": "0.1.34",
|
|
75
|
+
"@macroforge/bin-win32-arm64-msvc": "0.1.34"
|
|
76
76
|
},
|
|
77
77
|
"scripts": {
|
|
78
78
|
"build:serde": "bun build js/serde/index.ts --outfile js/serde/index.mjs && bun x tsc js/serde/index.ts --declaration --emitDeclarationOnly --outDir js/serde --lib ES2024 --skipLibCheck",
|