Proposal: compile-time API response projections (zero-DTO)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
Research direction
Start by reviewing the proposal and the linked Rustavel resource implementation and its documentation in graveyard/src/resource; the issue also links a production usage example. The proposal asks for feedback or a focused package first, with possible core integration later, so the initial outcome would be a maintainer decision on whether and where this projection design fits Laravel Rust.
Written by the indexing model from the issue text.
Description
Hi Laravel team,
First, congratulations on the Laravel Rust beta. I’ve been following the release closely and genuinely appreciate the direction you’re taking. I’ve been working on a full-stack Rust framework inspired by Laravel for most of this year (public repo: https://github.com/4xmen/rustavel). Seeing familiar patterns appear in migrations and validation in Laravel Rust made me smile, it confirmed we were sloving the same problems independently.
One piece I built that I haven’t seen yet in Laravel Rust is a thin, compile-time response projection layer (similar in intent to Eloquent API Resources, but deliberately Rust-first).
The problem
When a handler returns JSON, two common paths appear:
- Serialize the model directly → internal fields leak, and different endpoints need different shapes.
- Hand-write DTOs → repetitive mapping code, especially when data comes from multiple queries.
Both work, but both become noisy as the API grows.
What I built
A declarative macro that defines a pure serialization projection:
resource! {
pub UserSimpleResource from UserSimple {
id,
name,
email,
url = route!("api.v1.users.show", this.id).unwrap()
}
}
resource! {
pub UserSingleResource from (UserList, Vec<PostRow>, Vec<ProductRow>) {
id = this.0.id,
name = this.0.name,
email = this.0.email,
created_at = this.0.created_at,
updated_at = this.0.updated_at,
posts: Vec<PostsResource> = this.1,
products: Vec<ProductRowResource> = this.2,
}
}
Usage in a handler stays close to real query code:
let (user, comments, images, categories) = tokio::try_join!(/* queries */)?;
Ok(Json(resource_response! {
data: ProductResource::from((product, categories)),
user: UserResource::from(user),
comments: CommentResource::collection(comments),
images: ImageResource::collection(images),
}))
Key design choices:
- A Resource is not a DTO. It holds no fields; it only describes how to read a source and write JSON.
- Every expression runs against
&Source(this), so borrowing stays natural and clones are not forced. - Nested
Vec<OtherResource>/Option<OtherResource>are projected without intermediate allocations. - Composite sources (tuples) match data that arrives from parallel queries.
- Type and borrow errors come straight from rustc, not from a custom diagnostic system.
- Models intentionally do not derive
Serialize, so a forgotten nested annotation becomes a compile error instead of a data leak.
I deliberately rejected an attribute-based #[derive(Resource)] approach after using it for a while. The mapping became scattered across attributes and the transform/compute vocabulary grew awkward. The current resource! block keeps the whole projection in one place with ordinary Rust expressions.
The tested implementation that was ultimately rejected, along with its documentation, is available here:
https://github.com/4xmen/rustavel/tree/master/graveyard/src/resource
Scope
This is only about the shape of the outgoing JSON. It does not:
- load relations
- authorize
- validate
- paginate
- generate OpenAPI
Those concerns stay in the handler / service layer.
Real-world usage
This pattern is already used in production-style code:
https://github.com/4xmen/persian-json-placeholder/blob/master/app/src/models/user.rs
The same file defines multiple projections of a user (UserListResource, UserSimpleResource, UserSingleResource), which is the main reason a dedicated projection layer is useful.
Current status
- Fully implemented with tests
- Used in a real application
- Decision record and rejected designs are documented in the repository
Suggestion
I am happy to contribute in whatever form is most useful:
- Package first (preferred), publish a small, focused crate that works with the existing
Jsonresponse helpers. This lets the community try the pattern without any commitment from core. - Discussion / feedback, if the design direction is interesting, I’m glad to adapt the API surface to Laravel Rust conventions.
- Core later, only if the pattern proves valuable in practice.
I have no attachment to the current syntax details. What matters to me is the underlying idea: a compile-time, explicit, zero-DTO projection that stays out of the way of the rest of the framework.
Happy to open more examples, share production code, or adjust the design based on your feedback. And again, thank you for the work you’re doing on Laravel Rust. I’m rooting for it.
Kind regards
A1Gard
https://github.com/4xmen/rustavel
- Dominant language
- Rust
- Stars
- 2.2k
- Forks
- 105
- Avg merge
- 7h 19m
- Merged PRs (30d)
- 41
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from laravel/rust
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
laravel/rust#98 · 8 comments · 10 reactions ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neuralmagic/crucible#579 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
laravel/rust-framework#864 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
eclipse-score/kyron#249 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
seanmonstar/reqwest#3157 ·
Maintainers usually reply within 2 days