{"id":13611716,"url":"https://github.com/yanganto/struct-patch","last_synced_at":"2025-04-13T05:33:26.145Z","repository":{"id":65335806,"uuid":"590008621","full_name":"yanganto/struct-patch","owner":"yanganto","description":"A lib help you patch Rust instance, and easy to partial update configures.","archived":false,"fork":false,"pushed_at":"2025-04-10T15:51:43.000Z","size":154,"stargazers_count":23,"open_issues_count":2,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-10T16:01:16.515Z","etag":null,"topics":["derive","macro","overlay","patch","rust","struct"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yanganto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-01-17T13:08:36.000Z","updated_at":"2025-04-10T15:50:53.000Z","dependencies_parsed_at":"2024-08-01T19:45:26.664Z","dependency_job_id":"11ce486e-cc58-4218-9917-d23cf0e5b974","html_url":"https://github.com/yanganto/struct-patch","commit_stats":{"total_commits":16,"total_committers":2,"mean_commits":8.0,"dds":0.0625,"last_synced_commit":"ac0157e188cd7fc555c37efb29a05ab7abbdccde"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanganto%2Fstruct-patch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanganto%2Fstruct-patch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanganto%2Fstruct-patch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanganto%2Fstruct-patch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yanganto","download_url":"https://codeload.github.com/yanganto/struct-patch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248670513,"owners_count":21142896,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["derive","macro","overlay","patch","rust","struct"],"created_at":"2024-08-01T19:02:01.263Z","updated_at":"2025-04-13T05:33:21.124Z","avatar_url":"https://github.com/yanganto.png","language":"Rust","readme":"# Struct Patch\n[![Crates.io][crates-badge]][crate-url]\n[![MIT licensed][mit-badge]][mit-url]\n[![Docs][doc-badge]][doc-url]\n\nA lib help you patch Rust instance, and easy to partial update configures.\n\n## Introduction\nThis crate provides the `Patch` trait and an accompanying derive macro.\n\nDeriving `Patch` on a struct will generate a struct similar to the original one, but with all fields wrapped in an `Option`.  \nAn instance of such a patch struct can be applied onto the original struct, replacing values only if they are set to `Some`, leaving them unchanged otherwise.\n\n## Quick Example\n```rust\nuse struct_patch::Patch;\nuse serde::{Deserialize, Serialize};\n\n#[derive(Default, Debug, PartialEq, Patch)]\n#[patch(attribute(derive(Debug, Default, Deserialize, Serialize)))]\nstruct Item {\n    field_bool: bool,\n    field_int: usize,\n    field_string: String,\n}\n\nfn patch_json() {\n    let mut item = Item {\n        field_bool: true,\n        field_int: 42,\n        field_string: String::from(\"hello\"),\n    };\n\n    let data = r#\"{\n        \"field_int\": 7\n    }\"#;\n\n    let patch: ItemPatch = serde_json::from_str(data).unwrap();\n\n    item.apply(patch);\n    // You can do \n    // `let new_item = item \u003c\u003c patch;`\n\n    // For multiple patches,\n    // you can do this\n    // `let new_item = item \u003c\u003c patch_1 \u003c\u003c patch_2;`\n    // or make an aggregated one, but please make sure the patch fields do not conflict, else will panic\n    // ```\n    // let overall_patch = patch_1 + patch_2 + patch_3;\n    // let new_item = item \u003c\u003c overall_patch;\n    // ```\n\n    assert_eq!(\n        item,\n        Item {\n            field_bool: true,\n            field_int: 7,\n            field_string: String::from(\"hello\")\n        }\n    );\n}\n```\n\n## Documentation and Examples\nAlso, you can modify the patch structure by defining `#[patch(...)]` attributes on the original struct or fields.\n\nStruct attributes:\n- `#[patch(name = \"...\")]`: change the name of the generated patch struct.\n- `#[patch(attribute(...))]`: add attributes to the generated patch struct.\n- `#[patch(attribute(derive(...)))]`: add derives to the generated patch struct.\n\nField attributes: \n- `#[patch(skip)]`: skip the field in the generated patch struct.\n- `#[patch(name = \"...\")]`: change the type of the field in the generated patch struct.\n- `#[patch(attribute(...))]`: add attributes to the field in the generated patch struct.\n- `#[patch(attribute(derive(...)))]`: add derives to the field in the generated patch struct.\n\nPlease check the [traits][doc-traits] of document to learn more.\n\nThe [examples][examples] demo following scenarios.\n- diff two instance for a patch\n- create a patch from json string\n- rename the patch structure\n- check a patch is empty or not\n- add attribute to patch struct\n- show option field behavior\n- show operators about patches\n- show example with serde crates, ex: `humantime_serde` for duration\n\n## Features\nThis crate also includes the following optional features:\n- `status`(default): implements the `PatchStatus` trait for the patch struct, which provides the `is_empty` method.\n- `op` (default): provide operators `\u003c\u003c` between instance and patch, and `+` for patches\n  - default: when there is a field conflict between patches, `+` will add together if the `#[patch(addable)]` or `#[patch(add=fn)]` is provided, else it will panic.\n  - `merge` (optional): implements the `Merge` trait for the patch struct, which provides the `merge` method, and `\u003c\u003c` between patches.\n- `std`(optional):\n  - `box`: implements the `Patch\u003cBox\u003cP\u003e\u003e` trait for `T` where `T` implements `Patch\u003cP\u003e`.\n    This let you patch a boxed (or not) struct with a boxed patch.\n  - `option`: implements the `Patch\u003cOption\u003cP\u003e\u003e` trait for `Option\u003cT\u003e` where `T` implements `Patch\u003cP\u003e`, please take a look at the example to learn more.\n    - default: `T` needs to implement `From\u003cP\u003e`.  When patching on None, it will based on `from\u003cP\u003e` to cast T, and this let you patch structs containing fields with optional values.\n    - `none_as_default`: `T` needs to implement `Default`.  When patching on None, it will patch on a default instance, and this also let you patch structs containing fields with optional values.\n    - `keep_none`: When patching on None, it is still None.\n\n[crates-badge]: https://img.shields.io/crates/v/struct-patch.svg\n[crate-url]: https://crates.io/crates/struct-patch\n[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg\n[mit-url]: https://github.com/yanganto/struct-patch/blob/readme/LICENSE\n[doc-badge]: https://img.shields.io/badge/docs-rs-orange.svg\n[doc-url]: https://docs.rs/struct-patch/\n[doc-traits]: https://docs.rs/struct-patch/latest/struct_patch/traits/trait.Patch.html#container-attributes\n[examples]: /struct-patch/examples\n","funding_links":[],"categories":["Rust"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyanganto%2Fstruct-patch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyanganto%2Fstruct-patch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyanganto%2Fstruct-patch/lists"}