{"id":15597070,"url":"https://github.com/alorel/fancy_constructor-rs","last_synced_at":"2025-09-21T01:35:14.806Z","repository":{"id":192698186,"uuid":"687221203","full_name":"Alorel/fancy_constructor-rs","owner":"Alorel","description":"Derive a highly configurable constructor for your struct","archived":false,"fork":false,"pushed_at":"2025-08-18T17:52:53.000Z","size":52,"stargazers_count":1,"open_issues_count":4,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-19T20:52:46.346Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Alorel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["Alorel"],"custom":["https://paypal.me/alorel"]}},"created_at":"2023-09-04T23:00:56.000Z","updated_at":"2025-05-11T16:58:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"9148047b-c90b-4bd4-a31c-31523f21809c","html_url":"https://github.com/Alorel/fancy_constructor-rs","commit_stats":{"total_commits":19,"total_committers":3,"mean_commits":6.333333333333333,"dds":0.4736842105263158,"last_synced_commit":"0abdf15a9b597cb1b5bbde976e5ac2f80d2597c8"},"previous_names":["alorel/fancy_constructor-rs"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/Alorel/fancy_constructor-rs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alorel%2Ffancy_constructor-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alorel%2Ffancy_constructor-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alorel%2Ffancy_constructor-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alorel%2Ffancy_constructor-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Alorel","download_url":"https://codeload.github.com/Alorel/fancy_constructor-rs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alorel%2Ffancy_constructor-rs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276183677,"owners_count":25599223,"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","status":"online","status_checked_at":"2025-09-20T02:00:10.207Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-10-03T01:20:23.208Z","updated_at":"2025-09-21T01:35:14.793Z","avatar_url":"https://github.com/Alorel.png","language":"Rust","funding_links":["https://github.com/sponsors/Alorel","https://paypal.me/alorel"],"categories":[],"sub_categories":[],"readme":"\u003c!-- cargo-rdme start --\u003e\n\nDerive a highly configurable constructor for your struct\n\n[![MASTER CI status](https://github.com/Alorel/fancy_constructor-rs/actions/workflows/test.yml/badge.svg)](https://github.com/Alorel/fancy_constructor-rs/actions/workflows/test.yml?query=branch%3Amaster)\n[![crates.io badge](https://img.shields.io/crates/v/fancy_constructor)](https://crates.io/crates/fancy_constructor)\n[![dependencies badge](https://img.shields.io/librariesio/release/cargo/fancy_constructor)](https://libraries.io/cargo/fancy_constructor)\n[![Coverage Status](https://coveralls.io/repos/github/Alorel/fancy_constructor-rs/badge.png)](https://coveralls.io/github/Alorel/fancy_constructor-rs)\n\n# Examples\n\n\u003cdetails\u003e\u003csummary\u003eBasic\u003c/summary\u003e\n\n```rust\nuse fancy_constructor::new;\n#[derive(new, PartialEq, Eq, Debug)]\nstruct MyStruct {\n  foo: String,\n  bar: u8,\n}\n\nlet a = MyStruct::new(\"#[derive(new)]\".into(), 55);\nlet b = MyStruct { foo: \"#[derive(new)]\".into(), bar: 55 };\nassert_eq!(a, b);\n```\n\nOutputs:\n```rust\nimpl MyStruct {\n  pub fn new(foo: String, bar: u8) -\u003e Self {\n    Self { foo, bar }\n  }\n}\n````\n\n\u003c/details\u003e\n\u003cdetails\u003e\u003csummary\u003eOptions showcase\u003c/summary\u003e\n\n```rust\n#[derive(new, PartialEq, Eq, Debug)]\n#[new(vis(pub(crate)), name(construct), comment(\"Foo\"), bounds(T: Clone))]\nstruct MyStruct\u003cT\u003e {\n  #[new(into)]\n  a: T,\n\n  #[new(val(\"Bar\".into()))]\n  b: String,\n\n  #[new(clone)]\n  c: Arc\u003cWhatever\u003e,\n\n  #[new(default)]\n  d: Vec\u003cu8\u003e,\n}\n\nlet we = Arc::new(Whatever::default());\nlet a = MyStruct::\u003cString\u003e::construct(\"A\", \u0026we);\nlet b = MyStruct {a: \"A\".into(), b: \"Bar\".into(), c: we, d: vec![]};\nassert_eq!(a, b);\n```\n\nOutputs:\n\n```rust\nimpl\u003cT\u003e MyStruct\u003cT\u003e {\n  /// Foo\n  pub(crate) fn construct(a: impl Into\u003cT\u003e, c: \u0026Arc\u003cWhatever\u003e) -\u003e Self where T: Clone {\n    Self {\n      a: a.into(),\n      b: \"Bar\".into(),\n      c: c.clone(),\n      d: Default::default(),\n    }\n  }\n}\n````\n\n\u003c/details\u003e\n\u003cdetails\u003e\u003csummary\u003ePrivate const fn\u003c/summary\u003e\n\n```rust\n#[derive(new, PartialEq, Eq, Debug)]\n#[new(const_fn, vis())]\nstruct Foo(u8);\n\nconst FOO: Foo = Foo::new(128);\nassert_eq!(FOO, Foo(128));\n```\n\nOutputs:\n\n```rust\nimpl Foo {\n  const fn new(f1: u8) -\u003e Self {\n    Self(f1)\n  }\n}\n````\n\n\u003c/details\u003e\n\u003cdetails\u003e\u003csummary\u003eComputed values\u003c/summary\u003e\n\n```rust\n#[derive(new)]\nstruct Foo {\n  is_bar: bool,\n  #[new(val(if is_bar { 100 } else { 5 }))]\n  barness_level: u8,\n}\n\nassert_eq!(Foo::new(true).barness_level, 100);\nassert_eq!(Foo::new(false).barness_level, 5);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eCustom constructor args\u003c/summary\u003e\n\n```rust\n#[derive(new)]\n#[new(args(input_string: \u0026str))]\nstruct Foo {\n  #[new(val(input_string.to_lowercase()))]\n  pub lowercase: String,\n\n  #[new(val(input_string.to_uppercase()))]\n  pub uppercase: String,\n}\n\nlet foo = Foo::new(\"Foo\");\nassert_eq!(foo.lowercase.as_str(), \"foo\");\nassert_eq!(foo.uppercase.as_str(), \"FOO\");\n```\n\n\u003c/details\u003e\n\u003cdetails\u003e\u003csummary\u003eRenaming constructor args\u003c/summary\u003e\n\n```rust\n#[derive(new)]\nstruct MyNewtype(#[new(name(my_value))] u8);\n```\n\nOutputs:\n\n```rust\nimpl MyNewtype {\n  pub fn new(my_value: u8) -\u003e Self {\n    Self(my_value)\n  }\n}\n````\n\n\u003c/details\u003e\n\u003cdetails\u003e\u003csummary\u003eEnums\u003c/summary\u003e\n\n```rust\n#[derive(new, Eq, PartialEq, Debug)]\nenum MyEnum {\n  #[new]\n  Foo { #[new(into)] bar: u8 },\n  Qux,\n}\n\nassert_eq!(MyEnum::new(5), MyEnum::Foo { bar: 5 });\n```\n\nOutputs:\n\n```rust\nimpl MyEnum {\n  pub fn new(bar: Into\u003cu8\u003e) -\u003e Self {\n    Self::Foo { bar: bar.into() }\n  }\n}\n````\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eDeriving the Default trait\u003c/summary\u003e\n\nThe `Default` trait can be derived if the constructor ends up with no arguments:\n\n```rust\n#[derive(new, PartialEq, Eq, Debug)]\n#[new(default, name(construct))]\nstruct Foo {\n  #[new(val(u8::MAX))]\n  bar: u8,\n}\n\nassert_eq!(Foo::construct(), Foo::default());\n```\n\nOutputs:\n\n```rust\nimpl Default for Foo {\n  fn default() -\u003e Self {\n    Foo::construct()\n  }\n}\n````\n\nAttempting to use the option when the constructor has arguments will result in a compile error:\n\n```rust\n#[derive(new)]\n#[new(default)]\nstruct Foo {\n  bar: u8,\n}\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eInvalid inputs\u003c/summary\u003e\n\n```rust\n#[derive(fancy_constructor::new)]\nenum Foo {\n  Bar, // no variants marked with `#[new]`\n}\n```\n\n```rust\n#[derive(fancy_constructor::new)]\nenum Foo {\n  #[new] Bar, // multiple variants marked with `#[new]`\n  #[new] Qux,\n}\n```\n\n```rust\n#[derive(fancy_constructor::new)]\nunion Foo { // Unions not supported\n  bar: u8,\n  qux: u8,\n}\n```\n\n\u003c/details\u003e\n\n\u003c!-- cargo-rdme end --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falorel%2Ffancy_constructor-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falorel%2Ffancy_constructor-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falorel%2Ffancy_constructor-rs/lists"}