| Crates.io | partial_struct |
| lib.rs | partial_struct |
| version | 0.4.5 |
| created_at | 2025-02-13 15:30:22.993046+00 |
| updated_at | 2025-04-30 15:50:36.636206+00 |
| description | A proc-macro crate that generates partial versions of Rust structs. Specify omitted fields and custom derives via attributes, then use the generated conversion method to build the full struct. |
| homepage | |
| repository | https://github.com/EstebanForero/partial_struct |
| max_upload_size | |
| id | 1554379 |
| size | 20,485 |
Partial Struct is a procedural macro crate that generates “partial” versions of Rust structs. It creates new structs that contain a subset of the fields from your original struct—omitting those you specify via the attribute—and automatically provides bidirectional conversion functions.
• Generate Partial Structs: Automatically create one or more partial structs for a given full struct. Each partial struct contains all fields from the full struct except those specified to omit.
• Custom Target Name:
Optionally specify the name of the generated partial struct via a literal. If omitted, the generated struct is
named "Partial
• Custom Derives: Optionally add extra derives (e.g., Debug, Clone) to the generated partial struct using a derive(...) clause.
• Bidirectional Conversion:
The macro implements two conversions:
- A method on the generated partial struct (named to_<base_struct>() in snake case) that takes
the omitted fields as parameters and reconstructs the full struct.
- An implementation of From
Add the following to your Cargo.toml:
[dependencies] partial_struct = "0.1.0"
Annotate your struct with #[derive(Partial)] and attach one or more #[partial(...)] attributes to configure the output. The attribute supports three optional parts (order does not matter):
Example 1: Explicit Target Name, Extra Derives, and Omitted Fields
#[derive(Partial)]
#[partial("UserConstructor", derive(Debug, Clone), omit(id, secret))]
pub struct User {
id: uuid::Uuid,
name: String,
secret: String,
}
This generates:
#[derive(Debug, Clone)]
pub struct UserConstructor {
pub name: String,
}
impl UserConstructor {
pub fn to_user(self, id: uuid::Uuid, secret: String) -> User {
User { name: self.name, id, secret }
}
}
impl From<User> for UserConstructor {
fn from(full: User) -> Self {
Self { name: full.name }
}
}
Example 2: Default Target Name
#[derive(Partial)]
#[partial(derive(Debug), omit(x))]
pub struct Car {
x: u32,
model: String,
}
Since no target name is provided, the generated struct is named "PartialCar" and the conversion method is
named "to_car()". Also, an implementation of From
Example 3: Multiple Partial Attributes
#[derive(Partial)]
#[partial("UserInfo", derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq), omit(password))]
#[partial("UserCreation", derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq), omit(id_user, password, registration_date, email_verified, user_rol))]
pub struct User { ... }
This will generate two partial versions (UserInfo and UserCreation), each with its own conversion method.
When you derive Partial on a struct, the macro:
This crate minimizes compile time by enabling only the minimal syn features required for parsing. In the Cargo.toml for this crate, syn is included with:
syn = { version = "2.0.98", default-features = false, features = ["parsing"] }
This project is dual-licensed under the MIT or Apache-2.0 license, at your option.
Contributions, issues, and feature requests are welcome. Please check the issue tracker on the project's repository for more information.
Esteban estebanmff@outlook.com