1use crate::base::{bothify, sample};
4use crate::locale::{fetch_locale_with_context, sample_with_resolve};
5
6pub fn city() -> String {
8 fetch_locale_with_context("address.city", "en", Some("address"))
9 .map(|v| sample_with_resolve(&v, Some("address")))
10 .unwrap_or_else(|| sample(FALLBACK_CITIES).to_string())
11}
12
13pub fn street_name() -> String {
15 let prefix = fetch_locale_with_context("address.street_prefix", "en", Some("address"))
16 .map(|v| sample_with_resolve(&v, Some("address")))
17 .unwrap_or_else(|| sample(FALLBACK_STREET_PREFIXES).to_string());
18 let suffix = fetch_locale_with_context("address.street_suffix", "en", Some("address"))
19 .map(|v| sample_with_resolve(&v, Some("address")))
20 .unwrap_or_else(|| sample(FALLBACK_STREET_SUFFIXES).to_string());
21 format!("{} {}", prefix, suffix)
22}
23
24pub fn street_address() -> String {
26 let num = fetch_locale_with_context("address.street_number", "en", Some("address"))
27 .map(|v| sample_with_resolve(&v, Some("address")))
28 .unwrap_or_else(|| sample(FALLBACK_STREET_NUMBERS).to_string());
29 format!("{} {}", num, street_name())
30}
31
32pub fn secondary_address() -> String {
34 let pattern = fetch_locale_with_context("address.secondary_address", "en", Some("address"))
35 .map(|v| sample(&v))
36 .unwrap_or_else(|| "Apt. ###".to_string());
37 bothify(&pattern)
38}
39
40pub fn zip_code() -> String {
42 bothify("#####")
43}
44
45pub fn zip_code_with_extension() -> String {
47 bothify("#####-####")
48}
49
50pub fn country() -> String {
52 fetch_locale_with_context("address.country", "en", Some("address"))
53 .map(|v| sample(&v))
54 .unwrap_or_else(|| sample(FALLBACK_COUNTRIES).to_string())
55}
56
57pub fn country_code() -> String {
59 sample(FALLBACK_COUNTRY_CODES).to_string()
60}
61
62pub fn full_address() -> String {
64 let pattern = crate::config::FakerConfig::current().rand_range(0, 4);
65
66 match pattern {
67 0 => format!("{}, {}", street_address(), city_state_zip()),
68 1 => format!("{}\n{}", street_address(), city_state_zip()),
69 2 => format!("{}, {}", secondary_address(), city_state_zip()),
70 _ => format!(
71 "{}\n{}, {}",
72 street_address(),
73 secondary_address(),
74 city_state_zip()
75 ),
76 }
77}
78
79fn city_state_zip() -> String {
80 let city_val = city();
81 let state = fetch_locale_with_context("address.state", "en", Some("address"))
82 .map(|v| sample(&v))
83 .unwrap_or_else(|| "CA".to_string());
84 format!("{}, {} {}", city_val, state, zip_code())
85}
86
87pub fn time_zone() -> String {
89 sample(FALLBACK_TIME_ZONES).to_string()
90}
91
92pub fn latitude() -> String {
94 let config = crate::config::FakerConfig::current();
95 let lat = config.rand_range(0, 180_000_000) as f64 - 90_000_000.0;
96 format!("{:.6}", lat / 1_000_000.0)
97}
98
99pub fn longitude() -> String {
101 let config = crate::config::FakerConfig::current();
102 let lon = config.rand_range(0, 360_000_000) as f64 - 180_000_000.0;
103 format!("{:.6}", lon / 1_000_000.0)
104}
105
106const FALLBACK_CITIES: &[&str] = &[
108 "New York",
109 "Los Angeles",
110 "Chicago",
111 "Houston",
112 "Phoenix",
113 "Philadelphia",
114 "San Antonio",
115 "San Diego",
116 "Dallas",
117 "San Jose",
118 "Austin",
119 "Jacksonville",
120];
121
122const FALLBACK_STREET_PREFIXES: &[&str] = &[
123 "Main",
124 "Oak",
125 "Pine",
126 "Maple",
127 "Cedar",
128 "Elm",
129 "Washington",
130 "Lake",
131 "Hill",
132 "Park",
133 "Forest",
134 "River",
135];
136
137const FALLBACK_STREET_SUFFIXES: &[&str] = &[
138 "Street",
139 "Avenue",
140 "Road",
141 "Boulevard",
142 "Drive",
143 "Lane",
144 "Way",
145 "Court",
146];
147
148const FALLBACK_STREET_NUMBERS: &[&str] = &[
149 "1", "2", "3", "4", "5", "10", "11", "12", "13", "14", "15", "20", "21", "22", "30", "40",
150 "50", "100", "200", "300", "400", "500",
151];
152
153const FALLBACK_COUNTRIES: &[&str] = &[
154 "United States",
155 "United Kingdom",
156 "Canada",
157 "Australia",
158 "Germany",
159 "France",
160 "Japan",
161 "China",
162 "India",
163 "Brazil",
164];
165
166const FALLBACK_COUNTRY_CODES: &[&str] =
167 &["US", "CA", "GB", "DE", "FR", "JP", "CN", "IN", "BR", "AU"];
168
169const FALLBACK_TIME_ZONES: &[&str] = &[
170 "America/New_York",
171 "America/Chicago",
172 "America/Denver",
173 "America/Los_Angeles",
174 "Europe/London",
175 "Europe/Paris",
176 "Asia/Tokyo",
177 "Asia/Shanghai",
178];