{"id":14542075,"url":"https://github.com/elastio/bon","last_synced_at":"2026-03-14T01:55:42.449Z","repository":{"id":250595402,"uuid":"822211460","full_name":"elastio/bon","owner":"elastio","description":"Next-gen compile-time-checked builder generator, named function's arguments, and more!","archived":false,"fork":false,"pushed_at":"2025-04-21T03:31:30.000Z","size":6153,"stargazers_count":1569,"open_issues_count":22,"forks_count":24,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-23T20:59:21.658Z","etag":null,"topics":["asynchronous","builder","builder-pattern","builders","code-generation","constructor","constructors","data-structures","derive","derive-macro","generator","macro","macros","no-std","no-std-alloc","rust","setter","setter-methods","setters"],"latest_commit_sha":null,"homepage":"https://bon-rs.com","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elastio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE-APACHE","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},"funding":{"patreon":"Veetaha","ko_fi":"Veetaha","open_collective":"bon-rs"}},"created_at":"2024-06-30T15:36:46.000Z","updated_at":"2025-04-23T13:51:53.000Z","dependencies_parsed_at":"2024-08-06T22:02:31.305Z","dependency_job_id":"46b38878-db5b-4a63-a3c8-8e7a3895ef7e","html_url":"https://github.com/elastio/bon","commit_stats":null,"previous_names":["elastio/bon"],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elastio%2Fbon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elastio%2Fbon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elastio%2Fbon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elastio%2Fbon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elastio","download_url":"https://codeload.github.com/elastio/bon/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250514767,"owners_count":21443208,"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":["asynchronous","builder","builder-pattern","builders","code-generation","constructor","constructors","data-structures","derive","derive-macro","generator","macro","macros","no-std","no-std-alloc","rust","setter","setter-methods","setters"],"created_at":"2024-09-05T23:01:26.464Z","updated_at":"2026-03-14T01:55:42.438Z","avatar_url":"https://github.com/elastio.png","language":"Rust","readme":"\u003ca href=\"https://bon-rs.com/guide/overview\"\u003e\n    \u003cimg\n        src=\"https://bon-rs.com/bon-home.png\"\n        alt=\"bon home\"\n    /\u003e\n\u003c/a\u003e\n\n\u003cdiv align=\"center\"\u003e\n    \u003ca href=\"https://github.com/elastio/bon\"\u003e\u003cimg\n        alt=\"github\"\n        src=\"https://img.shields.io/badge/github-elastio/bon-228b22?style=for-the-badge\u0026labelColor=555555\u0026logo=github\"\n        height=\"25\"\n    /\u003e\u003c/a\u003e\n    \u003ca href=\"https://crates.io/crates/bon\"\u003e\u003cimg\n        alt=\"crates.io\"\n        src=\"https://img.shields.io/crates/v/bon.svg?style=for-the-badge\u0026color=e37602\u0026logo=rust\"\n        height=\"25\"\n    /\u003e\u003c/a\u003e\n    \u003ca href=\"https://docs.rs/bon/latest/bon/\"\u003e\u003cimg\n        alt=\"docs.rs\"\n        src=\"https://img.shields.io/badge/docs.rs-bon-3b74d1?style=for-the-badge\u0026labelColor=555555\u0026logo=docs.rs\"\n        height=\"25\"\n    /\u003e\u003c/a\u003e\n        \u003ca href=\"https://docs.rs/bon/latest/bon/\"\u003e\u003cimg\n        alt=\"docs.rs\"\n        src=\"https://img.shields.io/badge/MSRV-1.59.0-b83fbf?style=for-the-badge\u0026labelColor=555555\u0026logo=docs.rs\"\n        height=\"25\"\n    /\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n\u003cbr/\u003e\n\n\u003cdiv align=\"center\"\u003e\n    \u003ctable\u003e\n        \u003ctbody\u003e\n            \u003ctr\u003e\n                \u003ctd\u003e\u003ca href=\"https://bon-rs.com/guide/overview\"\u003e📖 Guide Book\u003c/a\u003e\u003c/td\u003e\n                \u003ctd\u003eNarrative introduction\u003c/td\u003e\n            \u003c/tr\u003e\n            \u003ctr\u003e\n                \u003ctd\u003e\u003ca href=\"https://bon-rs.com/reference/builder\"\u003e🔍 API Reference\u003c/a\u003e\u003c/td\u003e\n                \u003ctd\u003eAttributes API index\u003c/td\u003e\n            \u003c/tr\u003e\n        \u003c/tbody\u003e\n    \u003c/table\u003e\n\u003c/div\u003e\n\n\u003c!-- #region overview --\u003e\n\n# Overview\n\n`bon` is a Rust crate for generating compile-time-checked builders for structs and functions. It also provides idiomatic partial application with optional and named parameters for functions and methods.\n\nIf you wonder \"Why would I use builders?\", see the [motivational blog post](https://bon-rs.com/blog/how-to-do-named-function-arguments-in-rust).\n\n## Function Builder\n\nYou can turn a function with positional parameters into a function with named parameters with `#[builder]`.\n\n```rust\nuse bon::builder;\n\n#[builder]\nfn greet(name: \u0026str, level: Option\u003cu32\u003e) -\u003e String {\n    let level = level.unwrap_or(0);\n\n    format!(\"Hello {name}! Your level is {level}\")\n}\n\nlet greeting = greet()\n    .name(\"Bon\")\n    .level(24) // \u003c- setting `level` is optional, we could omit it\n    .call();\n\nassert_eq!(greeting, \"Hello Bon! Your level is 24\");\n```\n\nAny syntax for functions is supported including `async`, fallible, generic functions, `impl Trait`, etc.\n\nMany things are customizable with additional attributes described in the [API reference](https://bon-rs.com/reference/builder), but let's see what else `bon` has to offer.\n\n## Struct Builder\n\nUse `#[derive(Builder)]` to generate a builder for a struct.\n\n```rust\nuse bon::Builder;\n\n#[derive(Builder)]\nstruct User {\n    name: String,\n    is_admin: bool,\n    level: Option\u003cu32\u003e,\n}\n\nlet user = User::builder()\n    .name(\"Bon\".to_owned())\n    // `level` is optional, we could omit it here\n    .level(24)\n    // call setters in any order\n    .is_admin(true)\n    .build();\n\nassert_eq!(user.name, \"Bon\");\nassert_eq!(user.level, Some(24));\nassert!(user.is_admin);\n```\n\n## Method Builder\n\nAssociated methods require `#[bon]` on top of the impl block additionally.\n\n### Method `new`\n\nThe method named `new` generates `builder()/build()` methods.\n\n```rust\nuse bon::bon;\n\nstruct User {\n    id: u32,\n    name: String,\n}\n\n#[bon]\nimpl User {\n    #[builder]\n    fn new(id: u32, name: String) -\u003e Self {\n        Self { id, name }\n    }\n}\n\nlet user = User::builder()\n    .id(1)\n    .name(\"Bon\".to_owned())\n    .build();\n\nassert_eq!(user.id, 1);\nassert_eq!(user.name, \"Bon\");\n```\n\n`#[derive(Builder)]` on a struct generates builder API that is fully compatible with placing `#[builder]` on the `new()` method with a signature similar to the struct's fields (more details on the [Compatibility](https://bon-rs.com/guide/basics/compatibility#switching-between-derive-builder-and-builder-on-the-new-method) page).\n\n### Other Methods\n\nAll other methods generate `{method_name}()/call()` methods.\n\n```rust\nuse bon::bon;\n\nstruct Greeter {\n    name: String,\n}\n\n#[bon]\nimpl Greeter {\n    #[builder]\n    fn greet(\u0026self, target: \u0026str, prefix: Option\u003c\u0026str\u003e) -\u003e String {\n        let prefix = prefix.unwrap_or(\"INFO\");\n        let name = \u0026self.name;\n\n        format!(\"[{prefix}] {name} says hello to {target}\")\n    }\n}\n\nlet greeter = Greeter { name: \"Bon\".to_owned() };\n\nlet greeting = greeter\n    .greet()\n    .target(\"the world\")\n    // `prefix` is optional, omitting it is fine\n    .call();\n\nassert_eq!(greeting, \"[INFO] Bon says hello to the world\");\n```\n\nMethods with or without `self` are both supported.\n\n## No Panics Possible\n\nBuilders generated by `bon`'s macros use the typestate pattern to ensure all required parameters are filled, and the same setters aren't called repeatedly to prevent unintentional overwrites. If something is wrong, a compile error will be created.\n\n| ⭐ Don't forget to give our repo a [star on Github ⭐](https://github.com/elastio/bon)! |\n| --------------------------------------------------------------------------------------- |\n\n## What's Next?\n\nWhat you've seen above is the first page of the 📖 Guide Book. If you want to learn more, jump to the [Basics](https://bon-rs.com/guide/basics) section. And remember: knowledge is power 🐱!\n\nFeel free to jump to code and use the `#[builder]` and `#[derive(Builder)]` once you've seen enough docs to get started.\n\nThe [🔍 API Reference](https://bon-rs.com/reference/builder) will help you navigate the attributes once you feel comfortable with the basics of `bon`. Both `#[derive(Builder)]` on structs and `#[builder]` on functions/methods have almost identical attributes API, so the documentation for them is common.\n\n## Installation\n\nAdd `bon` to your `Cargo.toml`.\n\n\u003c!-- If you change this, make sure to update `scripts/sync-version.sh` --\u003e\n\n```toml\n[dependencies]\nbon = \"3.9\"\n```\n\nYou can opt out of `std` and `alloc` cargo features with `default-features = false` for `no_std` environments.\n\nSee [alternatives](https://bon-rs.com/guide/alternatives) for comparison.\n\n## Getting Help\n\nIf you can't figure something out, consult the docs and maybe use the `🔍 Search` bar on our [docs website](https://bon-rs.com). You may also create an issue or a discussion on the [Github repository](https://github.com/elastio/bon) for help or write us a message on [Discord](https://bon-rs.com/discord).\n\n## Socials\n\n\u003ctable\u003e\n\u003ctbody\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003ca href=\"https://bon-rs.com/discord\"\u003eDiscord\u003c/a\u003e\u003c/td\u003e\n        \u003ctd\u003eHere you can leave feedback, ask questions, report bugs, etc.\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\n## Acknowledgments\n\nThis project was heavily inspired by such awesome crates as [`buildstructor`](https://docs.rs/buildstructor), [`typed-builder`](https://docs.rs/typed-builder) and [`derive_builder`](https://docs.rs/derive_builder). This crate was designed with many lessons learned from them.\n\n## Supporters\n\n\u003cdiv align=\"center\"\u003e\n\nBig thanks to `bon`'s current financial supporters ❤️\n\n**Julius Lungys**\\\n\u003csup\u003e\u003ccode\u003e$1/month\u003c/code\u003e\u003c/sup\u003e\n\n\u003c/div\u003e\n\n## Past Supporters\n\n\u003cdiv align=\"center\"\u003e\n\nBig thanks to `bon`'s past financial supporters ❤️\n\n[![](https://github.com/user-attachments/assets/b0acb844-4b91-461c-95ae-90a601296500)](https://kindness.ai)\\\n[**Kindness**](https://kindness.ai)\n\n**Ethan Skowronski**\n\n\u003c/div\u003e\n\n## License\n\n\u003csup\u003e\nLicensed under either of \u003ca href=\"https://github.com/elastio/bon/blob/master/LICENSE-APACHE\"\u003eApache License, Version\n2.0\u003c/a\u003e or \u003ca href=\"https://github.com/elastio/bon/blob/master/LICENSE-MIT\"\u003eMIT license\u003c/a\u003e at your option.\n\u003c/sup\u003e\n\n\u003cbr\u003e\n\n\u003csub\u003e\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be\ndual licensed as above, without any additional terms or conditions.\n\u003c/sub\u003e\n\n\u003c!-- #endregion overview --\u003e\n","funding_links":["https://patreon.com/Veetaha","https://ko-fi.com/Veetaha","https://opencollective.com/bon-rs"],"categories":["Rust","Libraries"],"sub_categories":["Macro"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felastio%2Fbon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felastio%2Fbon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felastio%2Fbon/lists"}