1use std::iter::once;
2
3use crate::Unreal;
4use crate::choose;
5use crate::data::address::{
6 COUNTRY, COUNTRY_ABR, STREET_NAME, STREET_PREFIX, STREET_SUFFIX, US_STATE, US_STATE_INITIALS,
7};
8use itertools::Itertools;
9use rand::Rng;
10use rand::RngCore;
11use rand::distributions::uniform::SampleRange;
12
13impl<R: RngCore> Unreal<R> {
15 pub fn address(&mut self) -> String {
18 format!(
19 "{}, {}, {} {}",
20 self.street(),
21 self.city(),
22 self.us_state(),
23 self.zip()
24 )
25 }
26
27 pub fn street(&mut self) -> String {
30 once(self.street_number().as_str())
31 .chain(Some(self.street_prefix()).filter(|_| self.r#gen()))
32 .chain(once(self.street_name()))
33 .chain(once(self.street_suffix()))
34 .join(" ")
35 }
36
37 pub fn street_number(&mut self) -> String {
40 self.numbers(0..=99_999, 3)
41 }
42
43 choose! {
44 pub fn street_prefix(&mut self) from STREET_PREFIX;
46 pub fn street_name(&mut self) from STREET_NAME;
48 pub fn street_suffix(&mut self) from STREET_SUFFIX;
50 pub fn us_state(&mut self) from US_STATE;
52 pub fn us_state_initials(&mut self) from US_STATE_INITIALS;
54 pub fn country(&mut self) from COUNTRY;
56 pub fn country_abr(&mut self) from COUNTRY_ABR;
58 }
59
60 pub fn city(&mut self) -> String {
62 self.choose([
63 (|this: &mut Self| format!("{}{}", this.first_name(), this.street_suffix()))
64 as fn(&mut Self) -> String,
65 (|this: &mut Self| format!("{}{}", this.last_name(), this.street_suffix()))
66 as fn(&mut Self) -> String,
67 (|this: &mut Self| format!("{} {}", this.street_prefix(), this.last_name()))
68 as fn(&mut Self) -> String,
69 ])(self)
70 }
71
72 pub fn zip(&mut self) -> String {
74 self.digits(5)
75 }
76
77 pub fn latitude(&mut self) -> f32 {
79 self.gen_range(-90. ..=90.)
80 }
81
82 pub fn latitude_in_range(&mut self, range: impl SampleRange<f32>) -> f32 {
84 self.gen_range(range).clamp(-90., 90.)
85 }
86
87 pub fn longitude(&mut self) -> f32 {
89 self.gen_range(-180. ..=180.)
90 }
91
92 pub fn longitude_in_range(&mut self, range: impl SampleRange<f32>) -> f32 {
94 self.gen_range(range).clamp(-180., 180.)
95 }
96}