typeshaper 0.1.5

TypeScript utility-type idioms (Omit, Pick, Merge, Partial…) for Rust structs — one-line type algebra expressions
Documentation
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# typeshaper

`typeshaper` lets you derive new struct types from existing ones in a single expression — omit fields, pick fields, merge two structs, make all fields optional, or restore them as required. Every generated type automatically receives conversion impls and can feed into further expressions.

[中文文档](docs/readme.zh.md)

## Have you ever written code like this?

```rust
pub struct User {
    pub id: u64,
    pub name: String,
    pub email: String,
    pub password_hash: String,
    pub role: String,
    pub active: bool,
    pub created_at: i64,
}
```

The API layer needs it, but `password_hash` must not be exposed, so you duplicate:

```rust
pub struct UserPublic {
    pub id: u64,
    pub name: String,
    pub email: String,
    // no password_hash
    pub role: String,
    pub active: bool,
    pub created_at: i64,
}

impl From<User> for UserPublic {
    fn from(u: User) -> Self {
        Self {
            id: u.id,
            name: u.name,
            email: u.email,
            role: u.role,
            active: u.active,
            created_at: u.created_at,
        }
    }
}
```

The search endpoint only needs `id` and `name`, so you duplicate again:

```rust
pub struct UserSummary {
    pub id: u64,
    pub name: String,
}
// ... another From ...
```

The patch endpoint requires all fields to be optional, so you duplicate once more:

```rust
pub struct UserPatch {
    pub id: Option<u64>,
    pub name: Option<String>,
    pub email: Option<String>,
    pub password_hash: Option<String>,
    // ...
}
// ... another From ...
```

Add one field to `User` and you must update `UserPublic`, `UserPatch`, `UserSummary` — structs, `From` impls, and every test you might have missed.

And that's just `User`. You still have `Order`, `Product`, `Article`, `Comment`…

---

## A different approach

```toml
[dependencies]
typeshaper = "0.1"
```

```rust
use typeshaper::{TypeshaperExt, typeshaper, typex};

#[typeshaper]
#[derive(Debug, Clone)]
pub struct User {
    pub id: u64,
    pub name: String,
    pub email: String,
    pub password_hash: String,
    pub role: String,
    pub active: bool,
    pub created_at: i64,
}

// Remove two fields
typex!(#[derive(Debug, Clone)] pub UserPublic  = User - [password_hash, created_at]);

// Keep only two fields
typex!(#[derive(Debug, Clone)] pub UserSummary = User & [id, name]);

// Make all fields optional
typex!(#[derive(Debug, Clone)] pub UserPatch   = User?);
```

Conversions just work:

```rust
let user: User = /* from the database */;

let public:  UserPublic  = user.clone().project(); // drops password_hash, created_at
let summary: UserSummary = user.clone().project(); // only id and name
let patch    = UserPatch::from(user);              // all fields become Option
```

Add a field to `User` — the three `typex!()` lines stay unchanged, and the new field propagates automatically.

---

## Going further: merge two sources into one

An order snapshot needs both user and address information:

```rust
#[typeshaper]
pub struct Address {
    pub street: String,
    pub city: String,
    pub country: String,
}

// Merge User and Address into a new type
typex!(#[derive(Debug, Clone)] pub OrderSnapshot = User + Address);

let snapshot = OrderSnapshot::from((user, address));
// or via .project() on a tuple:
let snapshot = (user, address).project::<OrderSnapshot>();
```

Keep only the fields that are in `User` but not in `Address`:

```rust
typex!(#[derive(Debug, Clone)] pub UserOnly = User % Address); // Diff
```

---

## Expressions compose

```rust
// Remove password_hash, then make remaining fields optional
typex!(#[derive(Debug)] pub UserSafePatch = User - [password_hash]?);

// Remove password_hash, then pick summary fields
typex!(#[derive(Debug)] pub UserSafeDto = User - [password_hash] & [id, name, email]);

// Parentheses control associativity: Partial then Required (round-trip)
typex!(#[derive(Debug)] pub UserRestored = (User - [password_hash])?!);
```

---

## Full patch round-trip

```rust
// Optional version for update endpoints
typex!(#[derive(Debug, Clone)] pub UserPatch    = User?);

// Restore to required after validation
typex!(#[derive(Debug, Clone)] pub UserVerified = UserPatch!);

// ---

let patch = UserPatch {
    name: Some("alice".into()),
    email: Some("new@example.com".into()),
    // other fields left as None — "no update"
    ..Default::default()
};

// Recover the fully-typed version if all fields are present
match UserVerified::try_from(patch) {
    Ok(verified) => { /* commit */ }
    Err(e)       => { /* report which field is missing */ }
}
```

---

## Cross-crate: define models once

Define the domain model in one crate; derive views in another without copying structs:

```rust
// core-crate/src/lib.rs
#[typeshaper(export)]          // export generates a companion macro
#[derive(Debug, Clone)]
pub struct User { /* ... */ }
// automatically exports: pub macro typeshaper_import_User!()
```

```rust
// api-crate/src/lib.rs
use core_crate::{User, typeshaper_import_User};

typeshaper_import_User!();  // registers User's field metadata in this crate

// works exactly like a locally annotated type
typex!(#[derive(Debug, Clone)] pub UserPublic = User - [password_hash, created_at]);
typex!(#[derive(Debug, Clone)] pub UserPatch  = User?);
```

---

## Re-export with new attributes

Sometimes you need to annotate a struct you didn't write — and cannot touch.

A typical case is FFI. The [`napi-rs`](https://napi.rs/) framework requires `#[napi]` on every struct it exposes to Node.js. Your domain model lives in `core-crate`; the FFI crate must not modify it — adding attributes directly to someone else's package crosses crate boundaries and won't compile.

With typeshaper, a bare source expression (`T` with no operator) rebuilds the struct verbatim so you can attach new attributes:

```rust
// napi-crate/src/lib.rs
use core_crate::{User, typeshaper_import_User};

typeshaper_import_User!();

// Exact same fields as User — zero manual duplication
typex!(#[napi] pub UserNapi = User);

// Drop sensitive fields first, then expose via napi
typex!(#[napi] pub UserPublicNapi = User - [password_hash]);
```

The `User → UserNapi` conversion impl is generated automatically. The domain struct stays clean; the FFI crate owns the annotation.

The same pattern applies whenever an attribute can't live in the source crate: `#[repr(C)]` for C FFI, `#[pyclass]` for PyO3, a custom `#[derive]` from a third-party crate, and so on.

---

## Generic types

When a source struct has type parameters, you must declare them **explicitly** in `typex!()` — on the target name and on each generic source node. This is intentional: implicit inheritance would silently produce the wrong struct when multiple type parameters come from different sources.

### Basic type parameter

```rust
#[typeshaper]
#[derive(Debug, Clone)]
pub struct Wrapper<T> {
    pub inner: T,
    pub label: String,
    pub count: usize,
}

// <T> is declared on both the target name and the source node
typex!(#[derive(Debug, Clone)] pub WrapperNoLabel<T>  = Wrapper<T> - [label]);
typex!(#[derive(Debug, Clone)] pub WrapperPartial<T>  = Wrapper<T>?);
typex!(#[derive(Debug, Clone)] pub WrapperRequired<T> = WrapperPartial<T>!);

let w = Wrapper { inner: 42u32, label: "hi".into(), count: 3 };
let no_label: WrapperNoLabel<u32> = w.project();
```

### Multiple type parameters (Merge)

When merging two generic types, the target declares all parameters; each source node uses its own:

```rust
#[typeshaper]
pub struct Person<T> { pub name: T, pub age: u8 }

#[typeshaper]
pub struct Addr<U> { pub city: U, pub zip: String }

// T comes from Person, U comes from Addr — both declared on the target
typex!(#[derive(Debug)] pub PersonWithAddr<T, U> = Person<T> + Addr<U>);

let full = PersonWithAddr::from((person, addr));
```

### Inline trait bounds and where clauses

```rust
typex!(pub PrintableValue<T: std::fmt::Display + Clone> = Printable<T> - [note]);

typex!(pub ConstrainedData<T> where T: Clone + PartialEq = Constrained<T> - [meta]);
```

### Lifetime parameters

```rust
#[typeshaper]
pub struct Borrowed<'a> { pub name: &'a str, pub value: u32 }

typex!(pub BorrowedName<'a> = Borrowed<'a> & [name]);
```

### Cross-crate generic types

Generic parameter metadata is encoded in the companion macro and fully restored on import:

```rust
// core-crate
#[typeshaper(export)]
pub struct GenericModel<T> { pub id: u64, pub payload: T, pub hidden: bool }
```

```rust
// app-crate
typeshaper_import_GenericModel!();

typex!(#[derive(Debug)] pub ModelPublic<T> = GenericModel<T> - [hidden]);
typex!(#[derive(Debug)] pub ModelDraft<T>  = GenericModel<T>?);
```

> **Compile-error guard**: forgetting type parameters is caught at compile time:
> ```
> typex!(Bad = Wrapper - [label]);
> //          ^^^^^^^ error: type `Wrapper` has generic parameters;
> //                  declare them explicitly, e.g. `Target<T> = Wrapper<T>`
> ```

---

## Reference

### Installation

```toml
[dependencies]
typeshaper = "0.1"
```

### Source annotation: `#[typeshaper]`

Add once to a source struct. Field metadata is written to the compile-time registry; the struct itself is left unchanged.

| Form | Effect |
|------|--------|
| `#[typeshaper]` | Use within the same crate |
| `#[typeshaper(export)]` | Use within the same crate + generates `typeshaper_import_T!()` for other crates |

```rust
#[typeshaper]
#[derive(Debug, Clone, PartialEq)]
pub struct User {
    pub id: u64,
    pub name: String,
    pub age: u8,
    pub email: String,
}
```

`#[typeshaper]` stacks on top of any other attributes without affecting existing behavior.

---

### Operator reference

| Syntax | Name | Meaning | Generated impl |
|--------|------|---------|----------------|
| `T` | **Rebuild** | Copy all fields unchanged; apply new attributes | `TypeshaperInto<Target> for T` |
| `T - [f1, f2]` | **Omit** | Remove listed fields | `TypeshaperInto<Target> for T` |
| `T & [f1, f2]` | **Pick** | Keep only listed fields | `TypeshaperInto<Target> for T` |
| `A + B` | **Merge** | Combine all fields of A and B (no duplicates) | `From<(A, B)> for Target` + `TypeshaperInto<Target> for (A, B)` |
| `T?` | **Partial** | Wrap every field in `Option<_>` | `From<T> for Target` |
| `T!` | **Required** | Unwrap `Option<_>` from a Partial type | `TryFrom<T> for Target` (or `From<T>` when the source has no `Option` fields) |
| `A % B` | **Diff** | Fields present in A but absent in B (matched on both field name **and** type) | `TypeshaperInto<Target> for A` |

**Composition rules**

Operators are left-associative; parentheses change precedence:

```rust
// User - [age] & [id, name]  means  (User - [age]) & [id, name]
typex!(Dto      = User - [age] & [id, name]);

// Parentheses make the right side evaluate first
typex!(Full     = User + (Badge - [label]));

// Postfix chaining
typex!(Draft    = User - [password_hash]?);
typex!(Roundtrip = (User - [password_hash])?!);
```

---

### `typex!()` syntax

```
typex!( [#[attr...]]  [vis]  TargetName[<Params>] [where ...]  =  Expr );
```

- **Attributes** (optional): placed before `TargetName`, forwarded verbatim to the generated struct; multiple attributes can be stacked. `typex!()` never adds any `#[derive]` on its own.
- **Visibility** (optional): `pub`, `pub(crate)`, `pub(super)`, etc. **Defaults to private** when omitted — the generated struct is only accessible within the same module. Write explicit `pub` for types intended to be used outside the declaring module.
- **TargetName**: the name of the generated struct; also registered in the compile-time table so it can be used as a source in subsequent `typex!()` calls.
- **`<Params>`** (optional): explicit generic or lifetime parameters for the target type — required when any source in `Expr` is a generic type. Inline bounds (`T: Clone + Debug`) and separate `where` clauses are both accepted.
- **Expr**: a type-algebra expression — see the table above. Each source node that refers to a generic type must carry matching type arguments: `Source<T>`, `Source<'a>`, etc.

```rust
typex!(
    #[derive(Debug, Clone, PartialEq)]
    #[serde(rename_all = "camelCase")]
    pub UserPublicDto = User & [id, name, email]
);
```

---

### Conversion methods

`TypeshaperExt` is blanket-implemented for all types; the target is inferred from the binding:

```rust
let public: UserPublic = user.project();   // equivalent to user.typeshaper_into()
```

`Merge` uses tuple `From` or `.project()`, `Partial` uses `From`, `Required` uses `TryFrom`:

```rust
let snapshot = OrderSnapshot::from((user, address));
let snapshot = (user, address).project::<OrderSnapshot>(); // also works
let draft    = UserPatch::from(user);
let verified = UserVerified::try_from(draft)?;
```

---

### Cross-crate usage

**Exporting crate**

```rust
// core-crate/src/lib.rs
use typeshaper::typeshaper;

#[typeshaper(export)]
#[derive(Debug, Clone)]
pub struct User {
    pub id: u64,
    pub name: String,
    pub role: String,
    pub active: bool,
}
// automatically exports: pub macro typeshaper_import_User!()
```

**Importing crate**

```rust
// app-crate/src/lib.rs
use typeshaper::typex;
use core_crate::{User, typeshaper_import_User};

typeshaper_import_User!();  // call once at module top-level

typex!(#[derive(Debug, Clone)] UserPublic = User - [role, active]);
typex!(#[derive(Debug, Clone)] UserPatch  = User?);
```

Multiple types each get their own companion macro; cross-crate Merge and Diff are fully supported:

```rust
use core_crate::{Address, typeshaper_import_Address};

typeshaper_import_User!();
typeshaper_import_Address!();

typex!(#[derive(Debug, Clone)] OrderSnapshot = User + Address);
typex!(#[derive(Debug, Clone)] UserOnly      = User % Address);
```

| | Same crate | Cross-crate |
|---|---|---|
| Source annotation | `#[typeshaper]` | `#[typeshaper(export)]` |
| Caller prerequisite | none | `typeshaper_import_T!()` |
| `typex!()` syntax | identical | identical |

---

### Supported operations

- [x] Omit — `T - [fields]`
- [x] Pick — `T & [fields]`
- [x] Merge — `A + B`
- [x] Partial — `T?`
- [x] Required — `T!`
- [x] Diff — `A % B`
- [x] Expression composition and chaining
- [x] Attribute forwarding
- [x] Cross-crate export / import
- [x] Generics, lifetimes, and trait bounds — explicit type parameters required

---

## License

Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or [MIT license](LICENSE-MIT) at your option.