diff --git a/Cargo.toml b/Cargo.toml index 2ab6e65..32cac34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,19 +1,5 @@ -[package] -name = "fake" -version = "2.1.0" -authors = ["cksac "] -description = "An easy to use library for generating fake data like name, number, address, lorem, dates, etc." -keywords = ["faker", "data", "generator", "random"] -license = "MIT/Apache-2.0" -readme = "README.md" -repository = "https://github.com/cksac/fake-rs" -homepage = "https://github.com/cksac/fake-rs" -edition = "2018" - -[features] -wasm-bindgen = ["rand/wasm-bindgen"] - -[dependencies] -rand = "0.7" -chrono = { version = "0.4", optional = true } -http = { version = "0.1", optional = true } \ No newline at end of file +[workspace] +members = [ + "fake", + "dummy_derive", +] \ No newline at end of file diff --git a/dummy_derive/Cargo.toml b/dummy_derive/Cargo.toml new file mode 100644 index 0000000..97ee045 --- /dev/null +++ b/dummy_derive/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "dummy" +version = "0.3.0" +authors = ["cksac "] +description = "Macros implementation of #[derive(Dummy)]" +keywords = ["faker", "data", "random"] +license = "MIT/Apache-2.0" +readme = "README.md" +repository = "https://github.com/cksac/faker-rs/dummy_derive" +homepage = "https://github.com/cksac/faker-rs/dummy_derive" +edition = "2018" + +[lib] +proc-macro = true + +[dependencies] +syn = "0.15.26" +quote = "0.6" +darling = "0.9.0" + +[dev-dependencies] +fake = { version = "2.0", path = "../fake", features=['derive']} +rand="0.7" \ No newline at end of file diff --git a/dummy_derive/LICENSE-APACHE b/dummy_derive/LICENSE-APACHE new file mode 100644 index 0000000..8d13b5c --- /dev/null +++ b/dummy_derive/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright (c) 2018 cksac + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/dummy_derive/LICENSE-MIT b/dummy_derive/LICENSE-MIT new file mode 100644 index 0000000..52a3c53 --- /dev/null +++ b/dummy_derive/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2018 cksac + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/dummy_derive/README.md b/dummy_derive/README.md new file mode 100644 index 0000000..0cb7cd1 --- /dev/null +++ b/dummy_derive/README.md @@ -0,0 +1,62 @@ +# Dummy +[![Latest Version](https://img.shields.io/crates/v/dummy.svg)](https://crates.io/crates/dummy) + +This crate provide derive(Dummy) macros. use it via `fake` crate with derive feature + +## Installation +```toml +[dependencies] +fake = { version = "2.0", features=['derive']} +``` + +## Usage + +```rust +use fake::faker::boolean::en::*; +use fake::faker::company::en::*; +use fake::faker::name::en::*; +use fake::Dummy; +use fake::{Fake, Faker}; + +#[derive(Debug, Dummy)] +pub struct Order { + #[dummy(faker = "1000..")] + order_id: usize, + + #[dummy(faker = "Name()")] + customer: String, + + #[dummy(faker = "(Faker, 3..5)")] + items: Vec, + + #[dummy(faker = "Boolean(70)")] + paid: bool, +} + +#[derive(Debug, Dummy)] +pub struct Item { + #[dummy(faker = "1..100")] + product_id: usize, + + qty: u8, + + #[dummy(faker = "CompanyName()")] + company: String, +} + +fn main() { + let order: Order = Faker.fake(); + println!("{:#?}", order); +} +``` + +## License + +This project is licensed under either of + + * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or + http://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([LICENSE-MIT](LICENSE-MIT) or + http://opensource.org/licenses/MIT) + +at your option. diff --git a/dummy_derive/examples/derive.rs b/dummy_derive/examples/derive.rs new file mode 100644 index 0000000..aad439d --- /dev/null +++ b/dummy_derive/examples/derive.rs @@ -0,0 +1,36 @@ +use fake::faker::boolean::en::*; +use fake::faker::company::en::*; +use fake::faker::name::en::*; +use fake::Dummy; +use fake::{Fake, Faker}; + +#[derive(Debug, Dummy)] +pub struct Order { + #[dummy(faker = "1000..")] + order_id: usize, + + #[dummy(faker = "Name()")] + customer: String, + + #[dummy(faker = "(Faker, 3..5)")] + items: Vec, + + #[dummy(faker = "Boolean(70)")] + paid: bool, +} + +#[derive(Debug, Dummy)] +pub struct Item { + #[dummy(faker = "1..100")] + product_id: usize, + + qty: u8, + + #[dummy(faker = "CompanyName()")] + company: String, +} + +fn main() { + let order: Order = Faker.fake(); + println!("{:#?}", order); +} diff --git a/dummy_derive/src/lib.rs b/dummy_derive/src/lib.rs new file mode 100644 index 0000000..535ffde --- /dev/null +++ b/dummy_derive/src/lib.rs @@ -0,0 +1,69 @@ +extern crate proc_macro; +#[macro_use] +extern crate quote; +#[macro_use] +extern crate darling; + +use syn::{Ident, Type}; + +use darling::{ast, util, FromDeriveInput}; +use proc_macro::TokenStream; + +#[derive(Debug, FromField)] +#[darling(attributes(dummy))] +struct DummyField { + ident: Option, + ty: Type, + #[darling(default)] + faker: Option, +} + +#[derive(Debug, FromDeriveInput)] +#[darling(attributes(dummy), supports(struct_named))] +struct Dummy { + ident: Ident, + data: ast::Data, +} + +#[proc_macro_derive(Dummy, attributes(dummy))] +pub fn hello_world(input: TokenStream) -> TokenStream { + let parsed = syn::parse(input).unwrap(); + let receiver = Dummy::from_derive_input(&parsed).unwrap(); + + let struct_name = &receiver.ident; + let expanded = match receiver.data { + darling::ast::Data::Struct(darling::ast::Fields { ref fields, .. }) => { + let struct_fields: Vec<_> = fields.iter().map(|f| f.ident.as_ref().unwrap()).collect();; + + let let_statements: Vec<_> = fields + .iter() + .map(|f| { + let field_name = f.ident.as_ref().unwrap(); + let field_ty = &f.ty; + let faker = if let Some(ref expr) = f.faker { + syn::parse_str::(expr).unwrap() + } else { + syn::parse_str::(&"fake::Faker").unwrap() + }; + quote! { + let #field_name: #field_ty = (#faker).fake_with_rng(rng); + } + }) + .collect();; + + let impl_dummy = quote! { + impl fake::Dummy for #struct_name { + fn dummy_with_rng(_: &fake::Faker, rng: &mut R) -> Self { + #(#let_statements)* + #struct_name { + #(#struct_fields),* + } + } + } + }; + impl_dummy + } + _ => panic!("Dummy custom derive is not implemented for union or enum type."), + }; + expanded.into() +} diff --git a/fake/Cargo.toml b/fake/Cargo.toml new file mode 100644 index 0000000..60dcd96 --- /dev/null +++ b/fake/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "fake" +version = "2.1.0" +authors = ["cksac "] +description = "An easy to use library for generating fake data like name, number, address, lorem, dates, etc." +keywords = ["faker", "data", "generator", "random"] +license = "MIT/Apache-2.0" +readme = "README.md" +repository = "https://github.com/cksac/fake-rs/fake" +homepage = "https://github.com/cksac/fake-rs/fake" +edition = "2018" + +[dependencies] +dummy = { version = "0.3", optional = true, path = "../dummy_derive" } +rand = "0.7" +chrono = { version = "0.4", optional = true } +http = { version = "0.1", optional = true } + +[features] +wasm-bindgen = ["rand/wasm-bindgen"] + +# Provide derive(Dummy) macros. +derive = ["dummy"] \ No newline at end of file diff --git a/fake/LICENSE-APACHE b/fake/LICENSE-APACHE new file mode 100644 index 0000000..e3621bf --- /dev/null +++ b/fake/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright (c) 2016 cksac + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/fake/LICENSE-MIT b/fake/LICENSE-MIT new file mode 100644 index 0000000..a337a47 --- /dev/null +++ b/fake/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2016 cksac + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/fake/README.md b/fake/README.md new file mode 100644 index 0000000..b49157a --- /dev/null +++ b/fake/README.md @@ -0,0 +1,211 @@ +# Fake + +[![Build Status](https://travis-ci.org/cksac/fake-rs.svg?branch=master)](https://travis-ci.org/cksac/fake-rs) +[![Latest Version](https://img.shields.io/crates/v/fake.svg)](https://crates.io/crates/fake) + +A Rust library for generating fake data. + +## Installation + +Default: +```toml +[dependencies] +fake = "*" +``` +If you want the date and time features from chrono: +```toml +[dependencies.fake] +version = "*" +features = ["chrono"] +``` + +If you want Http faker features: +```toml +[dependencies.fake] +version = "*" +features = ["http"] +``` + +## Usage + +```rust +use fake::{Fake, Faker}; +use rand::rngs::StdRng; +use rand::SeedableRng; + +fn main() { + // using `Faker` to generate default fake value of given type + let tuple = Faker.fake::<(u8, u32, f32)>(); + println!("tuple {:?}", tuple); + println!("String {:?}", Faker.fake::()); + + // types U can used to generate fake value T, if `T: Dummy` + println!("String {:?}", (8..20).fake::()); + println!("u32 {:?}", (8..20).fake::()); + + // using `faker` module with locales + use fake::faker::name::raw::*; + use fake::locales::*; + + let name: String = Name(EN).fake(); + println!("name {:?}", name); + + let name: String = Name(ZH_TW).fake(); + println!("name {:?}", name); + + // using convenient function without providing locale + use fake::faker::lorem::en::*; + let words: Vec = Words(3..5).fake(); + println!("words {:?}", words); + + // using macro to generate nested collection + let name_vec = fake::vec![String as Name(EN); 4, 3..5, 2]; + println!("random nested vec {:?}", name_vec); + + // fixed seed rng + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, + ]; + let ref mut r = StdRng::from_seed(seed); + for _ in 0..5 { + let v: usize = Faker.fake_with_rng(r); + println!("value from fixed seed {}", v); + } +} +``` + +# Fakers with locale +## Lorem + +```rust +Word(); +Words(count: Range); +Sentence(count: Range); +Sentences(count: Range); +Paragraph(count: Range); +Paragraphs(count: Range); +``` + +## Name + +```rust +FirstName(); +LastName(); +Title(); +Suffix(); +Name(); +NameWithTitle(); +``` + +## Number + +```rust +Digit(); +NumberWithFormat(fmt: &'static str); +``` + +## Boolean + +```rust +Boolean(ratio: u8); +``` + +## Internet + +```rust +FreeEmailProvider(); +DomainSuffix(); +FreeEmail(); +SafeEmail(); +Username(); +Password(len_range: Range); +IPv4(); +IPv6(); +IP(); +Color(); +UserAgent(); +``` + +## HTTP +```rust +RfcStatusCode(); +ValidStatusCode(); +``` + +## Company + +```rust +CompanySuffix(); +CompanyName(); +Buzzword(); +BuzzwordMiddle(); +BuzzwordTail(); +CatchPhase(); +BsVerb(); +BsAdj(); +BsNoun(); +Bs(); +Profession(); +Industry(); +``` + +## Address + +```rust +CityPrefix(); +CitySuffix(); +CityName(); +CountryName(); +CountryCode(); +StreetSuffix(); +StreetName(); +TimeZone(); +StateName(); +StateAbbr(); +SecondaryAddressType(); +SecondaryAddress(); +ZipCode(); +PostCode(); +BuildingNumber(); +Latitude(); +Longitude(); +``` + +## Phone Number + +```rust +PhoneNumber(); +CellNumber(); +``` + +## Date/Time + +```rust +Time(); +Date(); +DateTime(); +Duration(); +DateTimeBefore(dt: DateTime); +DateTimeAfter(dt: DateTime); +DateTimeBetween(start: DateTime, end: DateTime); +``` + +## Filesystem +```rust +FilePath(); +FileName(); +FileExtension(); +DirPath(); +``` + +# LICENSE + +This project is licensed under either of + + * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or + http://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([LICENSE-MIT](LICENSE-MIT) or + http://opensource.org/licenses/MIT) + +at your option. \ No newline at end of file diff --git a/examples/basic.rs b/fake/examples/basic.rs similarity index 84% rename from examples/basic.rs rename to fake/examples/basic.rs index cf8e8e9..c1688d8 100644 --- a/examples/basic.rs +++ b/fake/examples/basic.rs @@ -1,8 +1,23 @@ -use fake::{Fake, Faker, ResultFaker, StringFaker}; +use fake::{Dummy, Fake, Faker, ResultFaker, StringFaker}; use std::pin::Pin; use std::rc::Rc; +#[derive(Debug, Dummy)] +pub struct Foo { + #[dummy(faker = "1000..2000")] + order_id: usize, + customer: String, + paid: bool, +} + +fn dummy_derive() { + let f: Foo = Faker.fake(); + println!("{:?}", f); +} + fn main() { + dummy_derive(); + // option println!("Option {:?}", Faker.fake::>()); println!("Option {:?}", (1..10).fake::>()); diff --git a/examples/collections.rs b/fake/examples/collections.rs similarity index 100% rename from examples/collections.rs rename to fake/examples/collections.rs diff --git a/examples/fakers.rs b/fake/examples/fakers.rs similarity index 100% rename from examples/fakers.rs rename to fake/examples/fakers.rs diff --git a/examples/primitives.rs b/fake/examples/primitives.rs similarity index 100% rename from examples/primitives.rs rename to fake/examples/primitives.rs diff --git a/examples/usage.rs b/fake/examples/usage.rs similarity index 100% rename from examples/usage.rs rename to fake/examples/usage.rs diff --git a/src/faker/impls/address.rs b/fake/src/faker/impls/address.rs similarity index 100% rename from src/faker/impls/address.rs rename to fake/src/faker/impls/address.rs diff --git a/src/faker/impls/boolean.rs b/fake/src/faker/impls/boolean.rs similarity index 100% rename from src/faker/impls/boolean.rs rename to fake/src/faker/impls/boolean.rs diff --git a/src/faker/impls/chrono.rs b/fake/src/faker/impls/chrono.rs similarity index 100% rename from src/faker/impls/chrono.rs rename to fake/src/faker/impls/chrono.rs diff --git a/src/faker/impls/company.rs b/fake/src/faker/impls/company.rs similarity index 100% rename from src/faker/impls/company.rs rename to fake/src/faker/impls/company.rs diff --git a/src/faker/impls/currency.rs b/fake/src/faker/impls/currency.rs similarity index 100% rename from src/faker/impls/currency.rs rename to fake/src/faker/impls/currency.rs diff --git a/src/faker/impls/filesystem.rs b/fake/src/faker/impls/filesystem.rs similarity index 100% rename from src/faker/impls/filesystem.rs rename to fake/src/faker/impls/filesystem.rs diff --git a/src/faker/impls/http.rs b/fake/src/faker/impls/http.rs similarity index 100% rename from src/faker/impls/http.rs rename to fake/src/faker/impls/http.rs diff --git a/src/faker/impls/internet.rs b/fake/src/faker/impls/internet.rs similarity index 100% rename from src/faker/impls/internet.rs rename to fake/src/faker/impls/internet.rs diff --git a/src/faker/impls/job.rs b/fake/src/faker/impls/job.rs similarity index 100% rename from src/faker/impls/job.rs rename to fake/src/faker/impls/job.rs diff --git a/src/faker/impls/lorem.rs b/fake/src/faker/impls/lorem.rs similarity index 100% rename from src/faker/impls/lorem.rs rename to fake/src/faker/impls/lorem.rs diff --git a/src/faker/impls/mod.rs b/fake/src/faker/impls/mod.rs similarity index 100% rename from src/faker/impls/mod.rs rename to fake/src/faker/impls/mod.rs diff --git a/src/faker/impls/name.rs b/fake/src/faker/impls/name.rs similarity index 100% rename from src/faker/impls/name.rs rename to fake/src/faker/impls/name.rs diff --git a/src/faker/impls/number.rs b/fake/src/faker/impls/number.rs similarity index 100% rename from src/faker/impls/number.rs rename to fake/src/faker/impls/number.rs diff --git a/src/faker/impls/phone_number.rs b/fake/src/faker/impls/phone_number.rs similarity index 100% rename from src/faker/impls/phone_number.rs rename to fake/src/faker/impls/phone_number.rs diff --git a/src/faker/mod.rs b/fake/src/faker/mod.rs similarity index 100% rename from src/faker/mod.rs rename to fake/src/faker/mod.rs diff --git a/src/impls/chrono/mod.rs b/fake/src/impls/chrono/mod.rs similarity index 100% rename from src/impls/chrono/mod.rs rename to fake/src/impls/chrono/mod.rs diff --git a/src/impls/http/mod.rs b/fake/src/impls/http/mod.rs similarity index 100% rename from src/impls/http/mod.rs rename to fake/src/impls/http/mod.rs diff --git a/src/impls/mod.rs b/fake/src/impls/mod.rs similarity index 100% rename from src/impls/mod.rs rename to fake/src/impls/mod.rs diff --git a/src/impls/std/array.rs b/fake/src/impls/std/array.rs similarity index 100% rename from src/impls/std/array.rs rename to fake/src/impls/std/array.rs diff --git a/src/impls/std/collections/binary_heap.rs b/fake/src/impls/std/collections/binary_heap.rs similarity index 100% rename from src/impls/std/collections/binary_heap.rs rename to fake/src/impls/std/collections/binary_heap.rs diff --git a/src/impls/std/collections/btree_map.rs b/fake/src/impls/std/collections/btree_map.rs similarity index 100% rename from src/impls/std/collections/btree_map.rs rename to fake/src/impls/std/collections/btree_map.rs diff --git a/src/impls/std/collections/btree_set.rs b/fake/src/impls/std/collections/btree_set.rs similarity index 100% rename from src/impls/std/collections/btree_set.rs rename to fake/src/impls/std/collections/btree_set.rs diff --git a/src/impls/std/collections/hash_map.rs b/fake/src/impls/std/collections/hash_map.rs similarity index 100% rename from src/impls/std/collections/hash_map.rs rename to fake/src/impls/std/collections/hash_map.rs diff --git a/src/impls/std/collections/hash_set.rs b/fake/src/impls/std/collections/hash_set.rs similarity index 100% rename from src/impls/std/collections/hash_set.rs rename to fake/src/impls/std/collections/hash_set.rs diff --git a/src/impls/std/collections/linked_list.rs b/fake/src/impls/std/collections/linked_list.rs similarity index 100% rename from src/impls/std/collections/linked_list.rs rename to fake/src/impls/std/collections/linked_list.rs diff --git a/src/impls/std/collections/mod.rs b/fake/src/impls/std/collections/mod.rs similarity index 100% rename from src/impls/std/collections/mod.rs rename to fake/src/impls/std/collections/mod.rs diff --git a/src/impls/std/collections/vec.rs b/fake/src/impls/std/collections/vec.rs similarity index 100% rename from src/impls/std/collections/vec.rs rename to fake/src/impls/std/collections/vec.rs diff --git a/src/impls/std/collections/vec_deque.rs b/fake/src/impls/std/collections/vec_deque.rs similarity index 100% rename from src/impls/std/collections/vec_deque.rs rename to fake/src/impls/std/collections/vec_deque.rs diff --git a/src/impls/std/container.rs b/fake/src/impls/std/container.rs similarity index 100% rename from src/impls/std/container.rs rename to fake/src/impls/std/container.rs diff --git a/src/impls/std/mod.rs b/fake/src/impls/std/mod.rs similarity index 100% rename from src/impls/std/mod.rs rename to fake/src/impls/std/mod.rs diff --git a/src/impls/std/net.rs b/fake/src/impls/std/net.rs similarity index 100% rename from src/impls/std/net.rs rename to fake/src/impls/std/net.rs diff --git a/src/impls/std/option.rs b/fake/src/impls/std/option.rs similarity index 100% rename from src/impls/std/option.rs rename to fake/src/impls/std/option.rs diff --git a/src/impls/std/path.rs b/fake/src/impls/std/path.rs similarity index 100% rename from src/impls/std/path.rs rename to fake/src/impls/std/path.rs diff --git a/src/impls/std/primitives.rs b/fake/src/impls/std/primitives.rs similarity index 100% rename from src/impls/std/primitives.rs rename to fake/src/impls/std/primitives.rs diff --git a/src/impls/std/result.rs b/fake/src/impls/std/result.rs similarity index 100% rename from src/impls/std/result.rs rename to fake/src/impls/std/result.rs diff --git a/src/impls/std/string.rs b/fake/src/impls/std/string.rs similarity index 100% rename from src/impls/std/string.rs rename to fake/src/impls/std/string.rs diff --git a/src/impls/std/tuple.rs b/fake/src/impls/std/tuple.rs similarity index 100% rename from src/impls/std/tuple.rs rename to fake/src/impls/std/tuple.rs diff --git a/src/lib.rs b/fake/src/lib.rs similarity index 95% rename from src/lib.rs rename to fake/src/lib.rs index 7988804..8ea56eb 100644 --- a/src/lib.rs +++ b/fake/src/lib.rs @@ -57,3 +57,6 @@ pub use impls::std::string::StringFaker; pub mod faker; pub mod locales; + +#[cfg(feature = "derive")] +pub use dummy::Dummy; diff --git a/src/locales/en.rs b/fake/src/locales/en.rs similarity index 100% rename from src/locales/en.rs rename to fake/src/locales/en.rs diff --git a/src/locales/mod.rs b/fake/src/locales/mod.rs similarity index 100% rename from src/locales/mod.rs rename to fake/src/locales/mod.rs diff --git a/src/locales/zh_tw.rs b/fake/src/locales/zh_tw.rs similarity index 100% rename from src/locales/zh_tw.rs rename to fake/src/locales/zh_tw.rs