Skip to main content

faker_rust/default/
name.rs

1//! Name generator - generates random names
2
3use crate::base::sample;
4use crate::config::FakerConfig;
5use crate::locale::{fetch_locale_with_context, sample_with_resolve};
6
7/// Generate a random full name
8pub fn name() -> String {
9    format!("{} {}", first_name(), last_name())
10}
11
12/// Generate a random first name
13pub fn first_name() -> String {
14    fetch_locale_with_context("name.first_name", "en", Some("name"))
15        .map(|v| sample_with_resolve(&v, Some("name")))
16        .unwrap_or_else(|| sample(FALLBACK_FIRST_NAMES).to_string())
17}
18
19/// Generate a random last name
20pub fn last_name() -> String {
21    fetch_locale_with_context("name.last_name", "en", Some("name"))
22        .map(|v| sample_with_resolve(&v, Some("name")))
23        .unwrap_or_else(|| sample(FALLBACK_LAST_NAMES).to_string())
24}
25
26/// Generate a random name prefix (Mr., Mrs., Ms., Dr., etc.)
27pub fn prefix() -> String {
28    fetch_locale_with_context("name.prefix", "en", Some("name"))
29        .map(|v| sample(&v))
30        .unwrap_or_else(|| sample(FALLBACK_PREFIXES).to_string())
31}
32
33/// Generate a random name suffix (Jr., Sr., I, II, III, IV, etc.)
34pub fn suffix() -> String {
35    fetch_locale_with_context("name.suffix", "en", Some("name"))
36        .map(|v| sample(&v))
37        .unwrap_or_else(|| sample(FALLBACK_SUFFIXES).to_string())
38}
39
40/// Generate random initials
41pub fn initials(num: usize) -> String {
42    let config = FakerConfig::current();
43    (0..num)
44        .map(|_| config.rand_char(&crate::base::U_LETTERS))
45        .collect()
46}
47
48/// Generate a random name with middle name
49pub fn name_with_middle() -> String {
50    format!("{} {} {}", first_name(), first_name(), last_name())
51}
52
53/// Generate a random male first name
54pub fn male_first_name() -> String {
55    fetch_locale_with_context("name.male_first_name", "en", Some("name"))
56        .map(|v| sample(&v))
57        .unwrap_or_else(|| sample(FALLBACK_MALE_FIRST_NAMES).to_string())
58}
59
60/// Generate a random female first name
61pub fn female_first_name() -> String {
62    fetch_locale_with_context("name.female_first_name", "en", Some("name"))
63        .map(|v| sample(&v))
64        .unwrap_or_else(|| sample(FALLBACK_FEMALE_FIRST_NAMES).to_string())
65}
66
67/// Generate a random gender-neutral first name
68pub fn neutral_first_name() -> String {
69    fetch_locale_with_context("name.neutral_first_name", "en", Some("name"))
70        .map(|v| sample(&v))
71        .unwrap_or_else(|| sample(FALLBACK_NEUTRAL_FIRST_NAMES).to_string())
72}
73
74// Static fallback data - used when locale files are not available
75// Note: Locale loading infrastructure is in place but needs refinement
76const FALLBACK_FIRST_NAMES: &[&str] = &[
77    "James",
78    "Mary",
79    "Robert",
80    "Patricia",
81    "John",
82    "Jennifer",
83    "Michael",
84    "Linda",
85    "David",
86    "Elizabeth",
87    "William",
88    "Barbara",
89    "Richard",
90    "Susan",
91    "Joseph",
92    "Jessica",
93    "Thomas",
94    "Sarah",
95    "Charles",
96    "Karen",
97    "Christopher",
98    "Nancy",
99    "Daniel",
100    "Lisa",
101    "Matthew",
102    "Betty",
103    "Anthony",
104    "Margaret",
105    "Mark",
106    "Sandra",
107    "Donald",
108    "Ashley",
109];
110
111const FALLBACK_MALE_FIRST_NAMES: &[&str] = &[
112    "James",
113    "John",
114    "Robert",
115    "Michael",
116    "William",
117    "David",
118    "Richard",
119    "Joseph",
120    "Thomas",
121    "Charles",
122    "Christopher",
123    "Daniel",
124    "Matthew",
125    "Anthony",
126    "Mark",
127    "Donald",
128    "Steven",
129    "Paul",
130    "Andrew",
131    "Joshua",
132    "Kenneth",
133    "Kevin",
134    "Brian",
135    "George",
136];
137
138const FALLBACK_FEMALE_FIRST_NAMES: &[&str] = &[
139    "Mary",
140    "Patricia",
141    "Jennifer",
142    "Linda",
143    "Barbara",
144    "Elizabeth",
145    "Susan",
146    "Jessica",
147    "Sarah",
148    "Karen",
149    "Nancy",
150    "Lisa",
151    "Betty",
152    "Margaret",
153    "Sandra",
154    "Ashley",
155];
156
157const FALLBACK_NEUTRAL_FIRST_NAMES: &[&str] = &[
158    "Alex", "Jordan", "Taylor", "Morgan", "Casey", "Riley", "Jamie", "Avery",
159];
160
161const FALLBACK_LAST_NAMES: &[&str] = &[
162    "Smith",
163    "Johnson",
164    "Williams",
165    "Brown",
166    "Jones",
167    "Garcia",
168    "Miller",
169    "Davis",
170    "Rodriguez",
171    "Martinez",
172    "Hernandez",
173    "Lopez",
174    "Gonzalez",
175    "Wilson",
176    "Anderson",
177    "Thomas",
178    "Taylor",
179    "Moore",
180    "Jackson",
181    "Martin",
182    "Lee",
183    "Perez",
184    "Thompson",
185    "White",
186];
187
188const FALLBACK_PREFIXES: &[&str] = &["Mr.", "Mrs.", "Ms.", "Miss", "Dr.", "Prof."];
189
190const FALLBACK_SUFFIXES: &[&str] = &["Jr.", "Sr.", "I", "II", "III", "IV", "V"];