{"id":13822409,"url":"https://github.com/TaKO8Ki/beaver","last_synced_at":"2025-05-16T17:30:47.339Z","repository":{"id":53391887,"uuid":"284887249","full_name":"TaKO8Ki/beaver","owner":"TaKO8Ki","description":"A library for setting up Rust objects inspired by factory_bot.","archived":false,"fork":false,"pushed_at":"2021-03-31T02:55:04.000Z","size":188,"stargazers_count":42,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T01:08:30.246Z","etag":null,"topics":["factory","rust","test"],"latest_commit_sha":null,"homepage":"https://docs.rs/beaver","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/TaKO8Ki.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"TaKO8Ki"}},"created_at":"2020-08-04T05:37:44.000Z","updated_at":"2025-02-07T10:39:15.000Z","dependencies_parsed_at":"2022-09-05T01:00:22.299Z","dependency_job_id":null,"html_url":"https://github.com/TaKO8Ki/beaver","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TaKO8Ki%2Fbeaver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TaKO8Ki%2Fbeaver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TaKO8Ki%2Fbeaver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TaKO8Ki%2Fbeaver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TaKO8Ki","download_url":"https://codeload.github.com/TaKO8Ki/beaver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254576378,"owners_count":22094357,"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":["factory","rust","test"],"created_at":"2024-08-04T08:01:59.299Z","updated_at":"2025-05-16T17:30:47.079Z","avatar_url":"https://github.com/TaKO8Ki.png","language":"Rust","funding_links":["https://github.com/sponsors/TaKO8Ki"],"categories":["Rust"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n ![logo](./resources/logo.png)\n\n beaver is a library for setting up Rust objects inspired by [factory_bot](https://github.com/thoughtbot/factory_bot).\n\n [![github workflow status](https://img.shields.io/github/workflow/status/TaKO8Ki/beaver/CI/master)](https://github.com/TaKO8Ki/beaver/actions) [![crates](https://img.shields.io/crates/v/beaver.svg?logo=rust)](https://crates.io/crates/beaver) [![docs](https://img.shields.io/badge/docs-beaver-8da0cb?labelColor=555555\u0026logo=rust)](https://docs.rs/beaver)\n\n [Usage](#Usage) | [Examples](examples) | [Docs](https://docs.rs/beaver)\n\n\u003c/div\u003e\n\n## Dependencies\n\n```toml\n[dependencies]\nbeaver = \"1\"\nserde = { version = \"1.0\", features = [\"derive\"] }\n```\n\nIf you want to use [chrono](https://docs.rs/chrono/) for your struct fields, `Cargo.toml` would look like this. \n\n```toml\n[dependencies]\nbeaver = \"1\"\nserde = { version = \"1.0\", features = [\"derive\"] }\n# you need `serde` feature.\nchrono = { version = \"0.4\", features = [\"serde\"] }\n```\n\n## Usage\n\n### Quickstart\n\n```rust\nuse serde::{Deserialize, Serialize};\n\n// `Post` needs both of `Serialize` and `Deserialize`.\n#[derive(Serialize, Deserialize, Debug)]\nstruct Post {\n    id: u16,\n    title: String,\n    approved: bool,\n}\n\nbeaver::define! {\n    PostFactory (Post) {\n        id -\u003e |n| n,\n        title -\u003e |n| format!(\"post-{}\", n),\n        approved -\u003e |_| false,\n    }\n}\n\nfn main() {\n    let post_factory = PostFactory::new();\n    let post1 = post_factory.build(|_| {});\n    let post2 = post_factory.build(|_| {});\n    println!(\"{:?}\", post1);\n    println!(\"{:?}\", post2);\n}\n```\n\n### Define a factory\n\n```rust\nbeaver::define! {\n    // [factory name] (struct)\n    PostFactory (Post) {\n        // `n` is a sequence number.\n        id -\u003e |n| n,\n        title -\u003e |n| format!(\"{}\", n),\n        approved -\u003e |_| false,\n    }\n}\n```\n\nThis `define!` macro defines a struct, `PostFactory` as a factory.\nIf you want to use factories outside modules, you need to make both of factories and structs public. For more information, please see this [example](examples/public_factory.rs).\n\n### Build structs\n\n```rust\n// initialize a factory.\nlet post_factory = PostFactory::new();\n\n// build a `Post`.\npost_factory.build(|_| {});\n\n// build a vector of some `Posts`.\npost_factory.build_list(3, |_| {});\n\n// override attributes of a factory.\npost_factory.build(|post| {\n    post.id = 1024;\n    post.title = \"foo bar\".to_string()\n});\n```\n\n## Examples\n\n- [Public factory](#public-factory)\n- [Sub factory vector](#sub-factory-vector)\n- [Others](#others)\n\n### [Public factory](examples/public_factory.rs)\n\n```rust\nuse chrono::NaiveDateTime;\nuse serde::{Deserialize, Serialize};\n\n// `Post` needs to be public.\n#[derive(Serialize, Deserialize, Debug)]\npub struct Post {\n    id: u16,\n    title: String,\n    approved: bool,\n    created_at: NaiveDateTime,\n}\n\nmod factory {\n    use crate::Post;\n    use chrono::NaiveDate;\n\n    beaver::define! {\n        // `PostFactory` needs to be public.\n        pub PostFactory (Post) {\n            id -\u003e |n| n,\n            title -\u003e |n| format!(\"post-{}\", n),\n            approved -\u003e |_| false,\n            created_at -\u003e |_| NaiveDate::from_ymd(2020, 1, 1).and_hms(0, 0, 0),\n        }\n    }\n}\n\nfn main() {\n    use factory::PostFactory;\n\n    let post_factory = PostFactory::new();\n    let post1 = post_factory.build(|_| {});\n    let post2 = post_factory.build(|_| {});\n    println!(\"{:?}\\n{:?}\", post1, post2);\n}\n```\n\nOutput:\n\n```sh\nPost { id: 1, title: \"post-1\", approved: false, created_at: 2020-01-01T00:00:00 }\nPost { id: 2, title: \"post-2\", approved: false, created_at: 2020-01-01T00:00:00 }\n```\n\n### [Sub factory vector](examples/sub_factory_vector.rs)\n\n```rust\nuse serde::{Deserialize, Serialize};\n\n#[derive(Serialize, Deserialize, Debug)]\npub struct Post {\n    id: u16,\n    title: String,\n    approved: bool,\n    tags: Vec\u003cTag\u003e,\n}\n\n#[derive(Serialize, Deserialize, Debug)]\npub struct Tag {\n    id: u16,\n    name: String,\n}\n\nmod factory {\n    use crate::Post;\n    use crate::Tag;\n\n    beaver::define! {\n        pub PostFactory (Post) {\n            id -\u003e |n| n,\n            title -\u003e |n| format!(\"post-{}\", n),\n            approved -\u003e |_| true,\n            // use `build_list`\n            tags -\u003e |n| TagFactory::build_list(3, n),\n        }\n    }\n\n    beaver::define! {\n        TagFactory (Tag) {\n            id -\u003e |n| beaver::sequence(100, n),\n            name -\u003e |n| format!(\"tag-{}\", n),\n        }\n    }\n}\n\nfn main() {\n    use factory::PostFactory;\n\n    let post_factory = PostFactory::new();\n    let post1 = post_factory.build(|_| {});\n    let post2 = post_factory.build(|_| {});\n    println!(\"{:?}\\n{:?}\", post1, post2);\n\n    let posts = post_factory.build_list(3, |_| {});\n    for post in posts {\n        println!(\"{:?}\", post);\n    }\n}\n```\n\nOutput:\n\n```sh\nPost { id: 1, title: \"post-1\", approved: true, tags: [Tag { id: 1, name: \"tag-1\" }, Tag { id: 2, name: \"tag-2\" }, Tag { id: 3, name: \"tag-3\" }] }\nPost { id: 2, title: \"post-2\", approved: true, tags: [Tag { id: 4, name: \"tag-4\" }, Tag { id: 5, name: \"tag-5\" }, Tag { id: 6, name: \"tag-6\" }] }\nPost { id: 3, title: \"post-3\", approved: true, tags: [Tag { id: 7, name: \"tag-7\" }, Tag { id: 8, name: \"tag-8\" }, Tag { id: 9, name: \"tag-9\" }] }\nPost { id: 4, title: \"post-4\", approved: true, tags: [Tag { id: 10, name: \"tag-10\" }, Tag { id: 11, name: \"tag-11\" }, Tag { id: 12, name: \"tag-12\" }] }\nPost { id: 5, title: \"post-5\", approved: true, tags: [Tag { id: 13, name: \"tag-13\" }, Tag { id: 14, name: \"tag-14\" }, Tag { id: 15, name: \"tag-15\" }] }\n```\n### Others\n\n- [Simple factory](examples/simple_factory.rs)\n- [Sub factory](examples/sub_factory.rs)\n\n## Contribution\n\nContributions, issues and pull requests are welcome!\n\n## License\n\nLicensed under MIT license ([LICENSE](LICENSE)).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTaKO8Ki%2Fbeaver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTaKO8Ki%2Fbeaver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTaKO8Ki%2Fbeaver/lists"}