1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
//! Seedable, `rand`-compatible generators of fake data. //! //! `faker_rand` provides types that you can use with the popular [`rand`] crate //! in order to generate human-friendly fake data, such as lorem ipsum text, //! first/last names, email addresses, and more. //! //! If `faker_rand` doesn't have what you need out of the box, you can build //! your own generators too. See ["Creating your own //! generators"](#creating-your-own-generators) below. Alternatively, consider //! opening a feature request on GitHub! //! //! # Quick start //! //! This crate provides types, called "generators" in this documentation, that //! you can pass to `rand`'s random data generation functions, like //! [`rand::random`] or [`rand::Rng::gen`]. //! //! ``` //! use rand::Rng; //! use faker_rand::en_us::names::FirstName; //! //! // you can display generators using "{}" //! println!("random first name: {}", rand::random::<FirstName>()); //! println!("random first name: {}", rand::thread_rng().gen::<FirstName>()); //! //! // or, you can use to_string as well //! let name = rand::random::<FirstName>().to_string(); //! println!("random first name: {}", name); //! ``` //! //! Crucially, this crate's generators work with seedable, deterministic PRNGs. //! This means you can get deterministic results automatically, as long as you //! use a deterministic [`rand::Rng`] to generate the data: //! //! ``` //! use rand::{Rng, SeedableRng}; //! use faker_rand::en_us::names::{FirstName, LastName}; //! //! // ChaCha8Rng is a deterministic RNG, assuming you give it a fixed seed //! let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); //! //! // This output is deterministic. Unlike the previous examples (which used a //! // global, randomly-seeded RNG), we will get the same result here each time. //! assert_eq!("Melvin", rng.gen::<FirstName>().to_string()); //! assert_eq!("Jamey", rng.gen::<FirstName>().to_string()); //! assert_eq!("Price", rng.gen::<LastName>().to_string()); //! //! // As a demonstration of the deterministic behavior, let's reset rng back to //! // its initial state. We'll get back the same generated data the second time //! // around. //! let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); //! assert_eq!("Melvin", rng.gen::<FirstName>().to_string()); //! assert_eq!("Jamey", rng.gen::<FirstName>().to_string()); //! assert_eq!("Price", rng.gen::<LastName>().to_string()); //! ``` //! //! # How it works //! //! What this crate calls a "generator" is just a type that wraps [`String`] and //! implements both [`std::fmt::Display`] and //! [`rand::distributions::Distribution`]. //! //! Implementing [`rand::distributions::Distribution`] makes a type compatible //! with [`rand::random`] or [`rand::Rng::gen`]. Implementing //! [`std::fmt::Display`] makes a type easy to print or convert into a //! [`String`]. //! //! Under the hood, almost all of the types exposed by this crate are just //! "newtype" wrappers around [`String`] (e.g. `struct Foo(String)`). It's //! expected that most users of this crate will never pass around instances of //! generators; instead, it's more common to pass a generator type as a type //! parameter to [`rand::Rng::gen`], and then immediately convert the result to //! a [`String`]. //! //! # Creating your own generators //! //! In addition to the base set of generators in this crate, `faker_rand` //! provides a set of macros to make implementing generators quick and easy. The //! two most common patterns for writing generators are to either: //! //! 1. Have the generator randomly choose from a more or less hardcoded list of //! words. //! //! For example, the list of common American first names is fixed in advance, //! and a generator for first names would randomly choose an element from //! that list. //! //! 2. Have the generator call `format!` with a template string and the result //! of sub-generators. //! //! For example, the generator for a full name would want to randomly //! generate a first and a last name, and then put a space between them, //! something like: `format!("{} {}", gen_first_name(), gen_last_name())`. //! //! This crate provides [`faker_impl_from_file`] to support the first pattern, //! and [`faker_impl_from_templates`] to support the second pattern. See the //! documentation for those macros for specifics on how to use them. //! //! ## Advanced generators //! //! Some generators need more advanced behavior than what //! [`faker_impl_from_file`] or [`faker_impl_from_templates`] can provide. //! Sometimes, you need to write your own //! [`Distribution`][`rand::distributions::Distribution`] impl by hand. Here's //! how you can write these advanced generators, while keeping them compatible //! with the macros provided by this crate. //! //! As an example of an advanced generator, let's imagine you want to make a //! generator that will take a sub-generator, and convert the sub-generator's //! result to lowercase. You can't do this with `format!`, so //! [`faker_impl_from_templates`] can't solve this for you. Instead, you'll have //! to write your own code. //! //! The recommended pattern to writing such generators is to create a newtype //! that wraps `String` and `PhantomData<T>`, where `T` will be the //! sub-generator you're wrapping. Then, you can implement your wrapper with //! code along the lines of: //! //! ``` //! use std::fmt; //! use std::marker::PhantomData; //! use rand::Rng; //! use rand::distributions::{Distribution, Standard}; //! //! // Lowercase will take what T generates, and convert it to lowercase. //! struct Lowercase<T>(String, PhantomData<T>); //! //! impl<T: ToString> Distribution<Lowercase<T>> for Standard //! where //! Standard: Distribution<T>, //! { //! fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Lowercase<T> { //! Lowercase(rng.gen::<T>().to_string().to_lowercase(), PhantomData) //! } //! } //! //! impl<T: ToString> fmt::Display for Lowercase<T> { //! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { //! write!(f, "{}", self.0) //! } //! } //! //! use faker_rand::faker_impl_from_templates; //! use faker_rand::en_us::addresses::CityName; //! use faker_rand::en_us::names::FullName; //! //! // As an example, let's use Lowercase to build a generator that takes a //! // city name and a full name, lowercases them, and concatenates the results. //! struct Demo(String); //! faker_impl_from_templates! { //! Demo; //! //! "{} {}", Lowercase<CityName>, Lowercase<FullName>; //! } //! //! use rand::SeedableRng; //! let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); //! //! // you can use Demo as your own custom generator now! //! assert_eq!("cletastad shanie russel dds", rng.gen::<Demo>().to_string()); //! assert_eq!("robinmouth pablo bayer", rng.gen::<Demo>().to_string()); //! assert_eq!("west gregory trevor cronin", rng.gen::<Demo>().to_string()); //! ``` //! //! This pattern is used within this crate to make utility generators like //! [`util::ToAsciiLowercase`] or [`util::CapitalizeFirstLetter`]. If you've //! created a generator that you feel could be useful to others, please consider //! opening a pull request to add it to this crate! /// Create a generator implementation from a file containing a list of words. /// /// The first argument to the macro must be the name of type to create an /// implementation for. Said type must be a newtype whose first member must be a /// [`String`]. The second argument must be a string literal, a path to the file /// containing the list of words. /// /// The macro will generate a /// [`Distribution`][`rand::distributions::Distribution`] and /// [`Display`][`std::fmt::Display`] implementation for the type. /// /// Each line of the given file, whose contents will be loaded using /// [`std::include_str`], will be used as a possible value to return when the /// generator is sampled. /// /// ``` /// use faker_rand::faker_impl_from_file; /// /// // First, declare your newtype wrapper around String. /// struct Demo(String); /// /// // Then, use the macro. data/lorem_words is a path to a file containing /// // example words; you will need to change this path to suit your needs. /// faker_impl_from_file!(Demo, "data/lorem_words"); /// /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// assert_eq!("impedit", rng.gen::<Demo>().to_string()); /// ``` #[macro_export] macro_rules! faker_impl_from_file { ($name: ident, $file: expr) => { impl rand::distributions::Distribution<$name> for rand::distributions::Standard { fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $name { use lazy_static::lazy_static; lazy_static! { static ref VALUES: Vec<String> = include_str!($file).lines().map(String::from).collect(); } $name(VALUES[rng.gen_range(0..VALUES.len())].clone()) } } impl std::fmt::Display for $name { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } }; } /// Create a generator implementation from a set of format strings and /// sub-generators. /// /// The first argument to the macro must be the name of type to create an /// implementation for. Said type must be a newtype whose first member must be a /// [`String`]. The first argument must be followed by a semicolon. /// /// After the first argument, the macro accepts a semicolon-separated sequence /// of template patterns. A template pattern is a comma-separated sequence /// starting with a [`std::format`]-compatible string literal, followed by a /// sequence of generator type names. The `{}`s in the string literal will be /// replaced by sampled data from each of the given generators. /// /// The macro will generate a /// [`Distribution`][`rand::distributions::Distribution`] and /// [`Display`][`std::fmt::Display`] implementation for the type. /// /// If multiple template patterns are given, the created implementation will, on /// each invocation, choose from one of them with equal likelihood. To bias /// generation in favor of one template pattern over another, you can provide /// the same template pattern multiple times. /// /// ``` /// use faker_rand::faker_impl_from_templates; /// /// // First, declare your newtype wrapper around String. /// struct Demo(String); /// /// // Then, invoke the macro. /// // /// // Note well: all commas and semicolons in this example, even trailing /// // semicolons, are strictly required. /// faker_impl_from_templates! { /// // The type we're creating a generator implementation for. /// Demo; /// /// // The template patterns. /// "{}.{}", faker_rand::util::AsciiDigit, faker_rand::lorem::Word; /// "{} ~~~ {}", faker_rand::lorem::Word, faker_rand::util::AsciiDigit; /// } /// /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// assert_eq!("qui ~~~ 5", rng.gen::<Demo>().to_string()); /// assert_eq!("debitis ~~~ 5", rng.gen::<Demo>().to_string()); /// assert_eq!("5.aliquid", rng.gen::<Demo>().to_string()); /// assert_eq!("0.doloribus", rng.gen::<Demo>().to_string()); /// ``` #[macro_export] macro_rules! faker_impl_from_templates { ($name: ident; $($fmt: expr, $($arg:ty),+);+;) => { impl rand::distributions::Distribution<$name> for rand::distributions::Standard { fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $name { use rand::seq::SliceRandom; let funcs: Vec<Box<dyn Fn(&mut R) -> String>> = vec![ $( Box::new(|rng| { format!($fmt, $( rng.gen::<$arg>().to_string(), )*) }), )* ]; $name(funcs.choose(rng).unwrap()(rng)) } } impl std::fmt::Display for $name { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } } } /// Utility generators that can be used as building blocks for larger /// generators. pub mod util { /// Generates an ASCII decimal digit (0-9). /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::util::AsciiDigit; /// assert_eq!("7", rng.gen::<AsciiDigit>().to_string()); /// ``` pub struct AsciiDigit(String); faker_impl_from_file!(AsciiDigit, "data/ascii_digit"); /// Generates an ASCII lowercase letter (a-z). /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::util::AsciiLowercase; /// assert_eq!("s", rng.gen::<AsciiLowercase>().to_string()); /// ``` pub struct AsciiLowercase(String); faker_impl_from_file!(AsciiLowercase, "data/ascii_lowercase"); use rand::distributions::{Distribution, Standard}; use rand::Rng; use std::fmt; use std::marker::PhantomData; /// Wraps a string generator so that its output is all ASCII lowercase /// letters (a-z). /// /// Any character that can't be lowercased to ASCII lowercase is stripped /// from the output. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// // FirstName generates strings whose first letter is capitalized ... /// use faker_rand::en_us::names::FirstName; /// assert_eq!("Melvin", rng.gen::<FirstName>().to_string()); /// /// // ... But with ToAsciiLowercase, it's lowercased. /// use faker_rand::util::ToAsciiLowercase; /// assert_eq!("jamey", rng.gen::<ToAsciiLowercase<FirstName>>().to_string()); /// ``` pub struct ToAsciiLowercase<T>(String, PhantomData<T>); impl<T: ToString> Distribution<ToAsciiLowercase<T>> for Standard where Standard: Distribution<T>, { fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> ToAsciiLowercase<T> { let mut s = deunicode::deunicode(&rng.gen::<T>().to_string()).to_lowercase(); s.retain(|c| c.is_ascii_lowercase()); ToAsciiLowercase(s, std::marker::PhantomData) } } impl<T: ToString> fmt::Display for ToAsciiLowercase<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } /// Wraps a string generator so that the first letter of its output is /// capitalized. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// // Word generates strings that are all lowercase ... /// use faker_rand::lorem::Word; /// assert_eq!("impedit", rng.gen::<Word>().to_string()); /// /// // ... But with CapitalizeFirstLetter, the first letter is capitalized. /// use faker_rand::util::CapitalizeFirstLetter; /// assert_eq!("Totam", rng.gen::<CapitalizeFirstLetter<Word>>().to_string()); /// ``` pub struct CapitalizeFirstLetter<T>(String, PhantomData<T>); impl<T: ToString> Distribution<CapitalizeFirstLetter<T>> for Standard where Standard: Distribution<T>, { fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> CapitalizeFirstLetter<T> { let s = rng.gen::<T>().to_string(); let mut c = s.chars(); CapitalizeFirstLetter( c.next().unwrap().to_uppercase().chain(c).collect(), std::marker::PhantomData, ) } } impl<T: ToString> fmt::Display for CapitalizeFirstLetter<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } } /// Generators for "lorem ipsum" placeholder text. pub mod lorem { use crate::util::CapitalizeFirstLetter; /// Generates a lorem ipsum word. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::lorem::Word; /// assert_eq!("impedit", rng.gen::<Word>().to_string()); /// ``` pub struct Word(String); faker_impl_from_file!(Word, "data/lorem_words"); struct FirstWord(String); faker_impl_from_templates! { FirstWord; "{}", CapitalizeFirstLetter<Word>; } /// Generates a lorem ipsum sentence. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::lorem::Sentence; /// assert_eq!( /// "Cumque debitis unde eum recusandae aut.", /// rng.gen::<Sentence>().to_string() /// ); /// ``` pub struct Sentence(String); faker_impl_from_templates! { Sentence; "{} {} {}.", FirstWord, Word, Word; "{} {} {} {}.", FirstWord, Word, Word, Word; "{} {} {} {} {}.", FirstWord, Word, Word, Word, Word; "{} {} {} {} {} {}.", FirstWord, Word, Word, Word, Word, Word; "{} {} {} {} {} {} {}.", FirstWord, Word, Word, Word, Word, Word, Word; } /// Generates a lorem ipsum paragraph. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::lorem::Paragraph; /// assert_eq!( /// "Debitis unde eum recusandae aut. Aut assumenda cupiditate aliquid voluptas facilis consectetur. Repellendus quae perspiciatis asperiores impedit. Voluptate dolorem in autem et. Consequatur iusto corrupti eum cupiditate.", /// rng.gen::<Paragraph>().to_string() /// ); /// ``` pub struct Paragraph(String); faker_impl_from_templates! { Paragraph; "{} {} {}", Sentence, Sentence, Sentence; "{} {} {} {}", Sentence, Sentence, Sentence, Sentence; "{} {} {} {} {}", Sentence, Sentence, Sentence, Sentence, Sentence; } /// Generates multiple lorem ipsum paragraphs. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::lorem::Paragraphs; /// assert_eq!( /// "Debitis unde eum recusandae aut. Aut assumenda cupiditate aliquid voluptas facilis consectetur. Repellendus quae perspiciatis asperiores impedit. Voluptate dolorem in autem et. Consequatur iusto corrupti eum cupiditate.\nDignissimos sit cupiditate vitae. Ex quidem odio quia nam. Doloribus reiciendis dignissimos in cum ad reprehenderit.\nEt error illum. Animi voluptatem quo temporibus velit consequatur. Ipsa corrupti cupiditate in et.\nSapiente molestiae sed. Ipsa voluptas rerum laborum. Sed natus et eum officiis ut. Ut voluptatem sint consequatur fuga explicabo asperiores. Aliquam vero quia cupiditate exercitationem blanditiis ea.\nMinima incidunt velit provident voluptate odio. Eius sequi unde voluptas qui. Possimus aut optio et. Consequuntur soluta aut dicta eos amet rerum. Eveniet corporis repudiandae aspernatur.\n", /// rng.gen::<Paragraphs>().to_string() /// ); /// ``` pub struct Paragraphs(String); faker_impl_from_templates! { Paragraphs; "{}\n{}\n{}\n", Paragraph, Paragraph, Paragraph; "{}\n{}\n{}\n{}\n", Paragraph, Paragraph, Paragraph, Paragraph; "{}\n{}\n{}\n{}\n{}\n", Paragraph, Paragraph, Paragraph, Paragraph, Paragraph; } } /// Localized generators for English as spoken in the United States (`en-US`). pub mod en_us { /// Generators for the names of individuals (e.g., first, last, or full /// names). pub mod names { /// Generates a first name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::names::FirstName; /// assert_eq!("Melvin", rng.gen::<FirstName>().to_string()); /// ``` pub struct FirstName(String); faker_impl_from_file!(FirstName, "data/en_us/first_names"); /// Generates a last name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::names::LastName; /// assert_eq!("Quitzon", rng.gen::<LastName>().to_string()); /// ``` pub struct LastName(String); faker_impl_from_file!(LastName, "data/en_us/last_names"); /// Generates a name prefix. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::names::NamePrefix; /// assert_eq!("Miss", rng.gen::<NamePrefix>().to_string()); /// ``` pub struct NamePrefix(String); faker_impl_from_file!(NamePrefix, "data/en_us/name_prefixes"); /// Generates a name suffix. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::names::NameSuffix; /// assert_eq!("IV", rng.gen::<NameSuffix>().to_string()); /// ``` pub struct NameSuffix(String); faker_impl_from_file!(NameSuffix, "data/en_us/name_suffixes"); /// Generates a full name, including possibly a prefix, suffix, or both. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::names::FullName; /// assert_eq!("Cleta McClure III", rng.gen::<FullName>().to_string()); /// ``` pub struct FullName(String); faker_impl_from_templates! { FullName; "{} {}", FirstName, LastName; "{} {} {}", NamePrefix, FirstName, LastName; "{} {} {}", FirstName, LastName, NameSuffix; "{} {} {} {}", NamePrefix, FirstName, LastName, NameSuffix; } } /// Generators for postal addresses and their constituent parts (e.g. city /// names, postal codes, etc.). pub mod addresses { use super::names::{FirstName, FullName, LastName}; use crate::util::AsciiDigit; struct CityPrefix(String); faker_impl_from_file!(CityPrefix, "data/en_us/city_prefixes"); struct CitySuffix(String); faker_impl_from_file!(CitySuffix, "data/en_us/city_suffixes"); /// Generates a city name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::addresses::CityName; /// assert_eq!("Cletastad", rng.gen::<CityName>().to_string()); /// ``` pub struct CityName(String); faker_impl_from_templates! { CityName; "{} {}{}", CityPrefix, FirstName, CitySuffix; "{} {}", CityPrefix, FirstName; "{}{}", FirstName, CitySuffix; "{}{}", LastName, CitySuffix; } struct StreetSuffix(String); faker_impl_from_file!(StreetSuffix, "data/en_us/street_suffixes"); /// Generates a street name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::addresses::StreetName; /// assert_eq!("Renner Mission", rng.gen::<StreetName>().to_string()); /// ``` pub struct StreetName(String); faker_impl_from_templates! { StreetName; "{} {}", FirstName, StreetSuffix; "{} {}", LastName, StreetSuffix; } struct BuildingNumber(String); faker_impl_from_templates! { BuildingNumber; "{}{}{}", AsciiDigit, AsciiDigit, AsciiDigit; "{}{}{}{}", AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit; "{}{}{}{}{}", AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit; } /// Generates a street address. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::addresses::StreetAddress; /// assert_eq!("5489 Shanie Springs", rng.gen::<StreetAddress>().to_string()); /// ``` pub struct StreetAddress(String); faker_impl_from_templates! { StreetAddress; "{} {}", BuildingNumber, StreetName; } /// Generates a secondary address (e.g. an apartment number). /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::addresses::SecondaryAddress; /// assert_eq!("Suite 755", rng.gen::<SecondaryAddress>().to_string()); /// ``` pub struct SecondaryAddress(String); faker_impl_from_templates! { SecondaryAddress; "Apt. {}{}{}", AsciiDigit, AsciiDigit, AsciiDigit; "Suite {}{}{}", AsciiDigit, AsciiDigit, AsciiDigit; } /// Generates a first-level administrative division (e.g. one of the 50 /// states). /// /// Currently, other top-level divisions in USA, such as the District of /// Columbia or the unincorporated organized territories (e.g. Puerto /// Rico), are not included in this list. This may be changed in a /// future minor version of this crate. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::addresses::Division; /// assert_eq!("Oklahoma", rng.gen::<Division>().to_string()); /// ``` pub struct Division(String); faker_impl_from_file!(Division, "data/en_us/divisions"); /// Generates an abbreviated first-level division (e.g. the two-letter /// abbreviation for one of the 50 states). /// /// See note in [`Division`] on the inclusion of entities other than the /// 50 states, and how this may change in a minor version of this crate. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::addresses::DivisionAbbreviation; /// assert_eq!("OK", rng.gen::<DivisionAbbreviation>().to_string()); /// ``` pub struct DivisionAbbreviation(String); faker_impl_from_file!(DivisionAbbreviation, "data/en_us/division_abbreviations"); /// Generates a postal code (a.k.a. a ZIP Code). /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::addresses::PostalCode; /// assert_eq!("75548-9960", rng.gen::<PostalCode>().to_string()); /// ``` pub struct PostalCode(String); faker_impl_from_templates! { PostalCode; "{}{}{}{}{}", AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit; "{}{}{}{}{}-{}{}{}{}", AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit; } /// Generates a full postal address. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::addresses::Address; /// assert_eq!( /// "Cleta McClure III\n15364 Marks Passage Apt. 057\nMargaritaborough, MA 91404\n", /// rng.gen::<Address>().to_string() /// ); /// ``` pub struct Address(String); faker_impl_from_templates! { Address; "{}\n{}\n{}, {} {}\n", FullName, StreetAddress, CityName, DivisionAbbreviation, PostalCode; "{}\n{} {}\n{}, {} {}\n", FullName, StreetAddress, SecondaryAddress, CityName, DivisionAbbreviation, PostalCode; } } /// Generators for company names and slogans. pub mod company { use super::names::{FirstName, LastName}; struct CompanySuffix(String); faker_impl_from_file!(CompanySuffix, "data/en_us/company_suffixes"); /// Generates a company name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::company::CompanyName; /// assert_eq!("Konopelski, Price, and Beier", rng.gen::<CompanyName>().to_string()); /// ``` pub struct CompanyName(String); faker_impl_from_templates! { CompanyName; "{} {}", FirstName, CompanySuffix; "{}-{}", LastName, LastName; "{}, {}, and {}", LastName, LastName, LastName; } struct SloganAdjective(String); faker_impl_from_file!(SloganAdjective, "data/en_us/slogan_adjectives"); struct SloganDescriptor(String); faker_impl_from_file!(SloganDescriptor, "data/en_us/slogan_descriptors"); struct SloganNouns(String); faker_impl_from_file!(SloganNouns, "data/en_us/slogan_nouns"); /// Generates a company slogan. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::company::Slogan; /// assert_eq!("Business-focused intermediate applications", rng.gen::<Slogan>().to_string()); /// ``` pub struct Slogan(String); faker_impl_from_templates! { Slogan; "{} {} {}", SloganAdjective, SloganDescriptor, SloganNouns; } } /// Generators for internet domain names, usernames, and emails. pub mod internet { use super::names::{FirstName, LastName}; use crate::util::{AsciiDigit, AsciiLowercase, ToAsciiLowercase}; struct DomainWord(String); faker_impl_from_templates! { DomainWord; "{}", ToAsciiLowercase<LastName>; } struct DomainTLD(String); faker_impl_from_file!(DomainTLD, "data/en_us/domain_tlds"); /// Generates a domain name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::internet::Domain; /// assert_eq!("thiel.name", rng.gen::<Domain>().to_string()); /// ``` pub struct Domain(String); faker_impl_from_templates! { Domain; "{}.{}", DomainWord, DomainTLD; } /// Generates a username. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::internet::Username; /// assert_eq!("odietrich48", rng.gen::<Username>().to_string()); /// ``` pub struct Username(String); faker_impl_from_templates! { Username; "{}{}", AsciiLowercase, ToAsciiLowercase<LastName>; "{}{}{}", AsciiLowercase, ToAsciiLowercase<LastName>, AsciiDigit; "{}{}{}{}", AsciiLowercase, ToAsciiLowercase<LastName>, AsciiDigit, AsciiDigit; "{}{}", ToAsciiLowercase<FirstName>, ToAsciiLowercase<LastName>; } /// Generates an email. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::internet::Email; /// assert_eq!("odietrich48@thompson.net", rng.gen::<Email>().to_string()); /// ``` pub struct Email(String); faker_impl_from_templates! { Email; "{}@{}", Username, Domain; } } /// Generators for phone numbers. pub mod phones { use crate::util::AsciiDigit; /// Generates a phone number. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::en_us::phones::PhoneNumber; /// assert_eq!("(058) 981-5364", rng.gen::<PhoneNumber>().to_string()); /// ``` pub struct PhoneNumber(String); faker_impl_from_templates! { PhoneNumber; "({}{}{}) {}{}{}-{}{}{}{}", AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit; } } } /// Localized generators for French as spoken in France (`fr-FR`). pub mod fr_fr { /// Generators for the names of individuals (e.g., first, last, or full /// names). pub mod names { /// Generates a first name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::names::FirstName; /// assert_eq!("Mahaut", rng.gen::<FirstName>().to_string()); /// ``` pub struct FirstName(String); faker_impl_from_file!(FirstName, "data/fr_fr/first_names"); /// Generates a last name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::names::LastName; /// assert_eq!("GUILLOT", rng.gen::<LastName>().to_string()); /// ``` pub struct LastName(String); faker_impl_from_file!(LastName, "data/fr_fr/last_names"); /// Generates a name prefix. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::names::NamePrefix; /// assert_eq!("Dr", rng.gen::<NamePrefix>().to_string()); /// ``` pub struct NamePrefix(String); faker_impl_from_file!(NamePrefix, "data/fr_fr/name_prefixes"); /// Generates a full name, including possibly a prefix. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::names::FullName; /// assert_eq!("Mlle Gisèle MARTINEZ", rng.gen::<FullName>().to_string()); /// ``` pub struct FullName(String); faker_impl_from_templates! { FullName; "{} {}", FirstName, LastName; "{} {} {}", NamePrefix, FirstName, LastName; } } /// Generators for postal addresses and their constituent parts (e.g. city /// names, postal codes, etc.). pub mod addresses { use super::names::FullName; use crate::util::AsciiDigit; /// Generates a city name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::addresses::CityName; /// assert_eq!("Levallois-Perret", rng.gen::<CityName>().to_string()); /// ``` pub struct CityName(String); faker_impl_from_file!(CityName, "data/fr_fr/city_names"); struct StreetPrefix(String); faker_impl_from_file!(StreetPrefix, "data/fr_fr/street_prefixes"); struct StreetSuffix(String); faker_impl_from_file!(StreetSuffix, "data/fr_fr/street_suffixes"); /// Generates a street name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::addresses::StreetName; /// assert_eq!("Passage de Seine", rng.gen::<StreetName>().to_string()); /// ``` pub struct StreetName(String); faker_impl_from_templates! { StreetName; "{} {}", StreetPrefix, StreetSuffix; } struct BuildingNumber(String); faker_impl_from_templates! { BuildingNumber; "{}", AsciiDigit; "{}{}", AsciiDigit, AsciiDigit; "{}{}{}", AsciiDigit, AsciiDigit, AsciiDigit; } /// Generates a street address. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::addresses::StreetAddress; /// assert_eq!("54 Place de Montmorency", rng.gen::<StreetAddress>().to_string()); /// ``` pub struct StreetAddress(String); faker_impl_from_templates! { StreetAddress; "{} {}", BuildingNumber, StreetName; } /// Generates a secondary address (e.g. an apartment number). /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::addresses::SecondaryAddress; /// assert_eq!("7 étage", rng.gen::<SecondaryAddress>().to_string()); /// ``` pub struct SecondaryAddress(String); faker_impl_from_templates! { SecondaryAddress; "Apt. {}{}{}", AsciiDigit, AsciiDigit, AsciiDigit; "{} étage", AsciiDigit; } /// Generates a first-level administrative division (e.g. one of the /// *régions* of France). /// /// Currently, this will generate only one of the 13 metropolitan /// regions of France. This may be changed in a future minor version of /// this crate. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::addresses::Division; /// assert_eq!("Nouvelle-Aquitaine", rng.gen::<Division>().to_string()); /// ``` pub struct Division(String); faker_impl_from_file!(Division, "data/fr_fr/divisions"); /// Generates a postal code. /// /// No guarantee is made that the first two digits correspond to a /// correct department. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::addresses::PostalCode; /// assert_eq!("05898", rng.gen::<PostalCode>().to_string()); /// ``` pub struct PostalCode(String); faker_impl_from_templates! { PostalCode; "{}{}{}{}{}", AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit; } /// Generates a full postal address. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::addresses::Address; /// assert_eq!( /// "Mlle Lucille MOREAU\nApt. 489\n96 Quai Saint-Jacques\n05764 Saint-Nazaire\nFRANCE\n", /// rng.gen::<Address>().to_string() /// ); /// ``` pub struct Address(String); faker_impl_from_templates! { Address; "{}\n{}\n{} {}\nFRANCE\n", FullName, StreetAddress, PostalCode, CityName; "{}\n{}\n{}\n{} {}\nFRANCE\n", FullName, SecondaryAddress, StreetAddress, PostalCode, CityName; } } /// Generators for company names. pub mod company { use super::names::FirstName; struct CompanySuffix(String); faker_impl_from_file!(CompanySuffix, "data/fr_fr/company_suffixes"); /// Generates a company name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::company::CompanyName; /// assert_eq!("Lucille SARL", rng.gen::<CompanyName>().to_string()); /// ``` pub struct CompanyName(String); faker_impl_from_templates! { CompanyName; "{} {}", FirstName, CompanySuffix; } } /// Generators for internet domain names, usernames, and emails. pub mod internet { use super::names::{FirstName, LastName}; use crate::util::{AsciiDigit, AsciiLowercase, ToAsciiLowercase}; struct DomainWord(String); faker_impl_from_templates! { DomainWord; "{}", ToAsciiLowercase<LastName>; } struct DomainTLD(String); faker_impl_from_file!(DomainTLD, "data/fr_fr/domain_tlds"); /// Generates a domain name. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::internet::Domain; /// assert_eq!("renard.net", rng.gen::<Domain>().to_string()); /// ``` pub struct Domain(String); faker_impl_from_templates! { Domain; "{}.{}", DomainWord, DomainTLD; } /// Generates a username. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::internet::Username; /// assert_eq!("omartinez48", rng.gen::<Username>().to_string()); /// ``` pub struct Username(String); faker_impl_from_templates! { Username; "{}{}", AsciiLowercase, ToAsciiLowercase<LastName>; "{}{}{}", AsciiLowercase, ToAsciiLowercase<LastName>, AsciiDigit; "{}{}{}{}", AsciiLowercase, ToAsciiLowercase<LastName>, AsciiDigit, AsciiDigit; "{}{}", ToAsciiLowercase<FirstName>, ToAsciiLowercase<LastName>; } /// Generates an email. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::internet::Email; /// assert_eq!("omartinez48@poirier.net", rng.gen::<Email>().to_string()); /// ``` pub struct Email(String); faker_impl_from_templates! { Email; "{}@{}", Username, Domain; } } /// Generators for phone numbers. pub mod phones { use crate::util::AsciiDigit; /// Generates a phone number. /// /// ``` /// use rand::{Rng, SeedableRng}; /// let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0); /// /// use faker_rand::fr_fr::phones::PhoneNumber; /// assert_eq!("00 58 98 15 36", rng.gen::<PhoneNumber>().to_string()); /// ``` pub struct PhoneNumber(String); faker_impl_from_templates! { PhoneNumber; "0{} {}{} {}{} {}{} {}{}", AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit, AsciiDigit; } } }