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
use chrono::Datelike;
pub use ipaddress::*;

use super::dependencies::*;
use super::{Datetime, File, Text};


#[derive(Debug)]
pub enum StockType {
    URL(String),
    Image(Vec<u8>),
}

/// Methods collection for generating data related to the internet
pub struct Internet;

impl Internet {
    /// Get a random HTTP content type
    ///
    /// return example: Content-Type: application/json
    ///
    /// # Arguments
    /// * `mime_type` - MimeType enum
    pub fn content_type(mime_type: Option<MimeType>) -> String {
        format!("Content-Type: {}", File::mime_type(mime_type))
    }

    /// Get a random HTTP status message
    ///
    /// return example: 200 OK
    pub fn http_status_message() -> &'static str {
        get_random_element(HTTP_STATUS_MSGS.iter())
    }

    /// Get a random HTTP status message
    ///
    /// return example: 200
    pub fn http_status_code() -> &'static str {
        get_random_element(HTTP_STATUS_CODES.iter())
    }

    /// Get a random HTTP status message
    ///
    /// return example: POST
    pub fn http_method() -> &'static str {
        get_random_element(HTTP_METHODS.iter())
    }

    /// Generate random port
    /// 
    /// return example: 8000
    ///
    /// # Arguments
    /// * `port_range` - PortRange enum
    pub fn port(port_range: Option<PortRange>) -> u16 {
        let range = validate_enum(port_range, Some(PortRange::ALL));
        randint(range.0, range.1)
    }

    /// Get a random v4 IPAddress struct
    ///
    /// return example: IPAddress
    pub fn ip_v4_object() -> IPAddress {
        ipv4::from_u32(randint(0, 4294967295), 32).expect("Cant create v4 IPAddress!")
    }

    /// Get a random v4 IP address
    ///
    /// return example: 127.0.0.1
    pub fn ip_v4() -> String {
        Self::ip_v4_object().to_s()
    }

    /// Get a random v4 IP address with port
    ///
    /// return example: 127.0.0.1:666
    ///
    /// # Arguments
    /// * `port_range` - PortRange enum
    pub fn ip_v4_with_port(port_range: Option<PortRange>) -> String {
        format!("{}:{}", Self::ip_v4(), Self::port(port_range))
    }

    /// Get a random v6 IPAddress struct
    ///
    /// return example: IPAddress
    pub fn ip_v6_object() -> IPAddress {
        ipv6::from_int(randbigint(u128::MIN, u128::MAX), 32).expect("Cant create v6 IPAddress!")
    }

    /// Get a random v6 IP address
    ///
    /// return example: 0000:0000:0000:0000:0000:0000:0000:0001
    pub fn ip_v6() -> String {
        Self::ip_v6_object().to_s()
    }

    /// Get a random mac address
    ///
    /// return example: 00:16:3e:25:e7:f1
    pub fn mac() -> String {
        vec![
            0x00,
            0x16,
            0x3E,
            randint(0x00, 0x7F),
            randint(0x00, 0xFF),
            randint(0x00, 0xFF),
        ].iter().map(|i| format!("{:02x}", i))
            .join(":")
    }

    /// Get a random emoji shortcut code
    ///
    /// return example: :smirk:
    pub fn emoji() -> &'static str {
        get_random_element(EMOJI.iter())
    }

    /// Generate random stock image (JPG/JPEG) hosted on Unsplash
    ///
    /// return example: StockType::URL("<https://source.unsplash.com/666x666?test>")
    ///
    /// # Arguments
    /// * `width` - Width of the image
    /// * `height` - Height of the image
    /// * `keywords` - List of search keywords
    /// * `to_image` - Return image as vec of u8
    pub fn stock_image(width: u32, height: u32, keywords: Vec<&str>, to_image: bool) -> StockType {
        let keywords_str = keywords.join(",");
        let url = format!("https://source.unsplash.com/{width}x{height}?{keywords_str}");

        match to_image {
            true => {
                let r = reqwest::blocking::get(url).expect("Cant fetch images from unsplash!")
                    .bytes().expect("Cant get output!");
                
                StockType::Image(r.to_vec())
            },
            false => StockType::URL(url),
        }
    }

    /// Generate a list of hashtags
    ///
    /// return example: vec!\['#some', '#random', '#things'\]
    ///
    /// # Arguments
    /// * `quantity` - The quantity of hashtags
    pub fn hashtags(quantity: i32) -> Vec<String> {
        let locale = Text(Locale::EN);
        (0..quantity).map(|_| format!("#{}", locale.word())).collect()
    }

    /// Generates random top level domain
    ///
    /// return example: com
    ///
    /// # Arguments
    /// * `tld_type` - TLDType provide hostname domain
    pub fn top_level_domain(tld_type: Option<TLDType>) -> &'static str {
        let tld = validate_enum(tld_type, None);
        get_random_element(TLD.get(tld).expect("Cant get TLD type!").iter())
    }

    /// Generates random top level domain | *An allias for .top_level_domain()*
    ///
    /// return example: com
    ///
    /// # Arguments
    /// * `tld_type` - TLDType provide hostname domain
    pub fn tld(tld_type: Option<TLDType>) -> &'static str {
        Self::top_level_domain(tld_type)
    }

    /// Get a random user agent
    ///
    /// return example: Gecko/20100101 Firefox/15.0.1
    pub fn user_agent() -> &'static str {
        get_random_element(USER_AGENTS.iter())
    }

    /// Get a random user agent
    ///
    /// return example: 1.1.1.1
    pub fn public_dns() -> &'static str {
        get_random_element(PUBLIC_DNS.iter())
    }

    /// Generate a random slug of given parts count
    /// 
    /// return example: some-slug-here
    pub fn slug(parts_count: Option<usize>) -> String {
        let parts = parts_count.unwrap_or_else(|| randint(2, 12));

        if parts > 12 {
            panic!("Slug's parts count must be <= 12");
        }

        if parts < 2 {
            panic!("Slug must contain more than 2 parts");
        }

        Text(Locale::EN).words(parts).iter().join("-")
    }

    /// Generate a random hostname without scheme
    ///
    /// return example: sub.domain
    ///
    /// # Arguments
    /// * `tld_type` - TLDType provide hostname domain
    /// * `subdomains` - vec of subdomains
    pub fn hostname(tld_type: Option<TLDType>, subdomains: Option<Vec<&str>>) -> String {
        let tld = Self::top_level_domain(tld_type);
        let host = get_random_element(USERNAMES.iter());

        let mut url = format!("{host}{tld}");

        if let Some(v) = subdomains {
            url = format!("{}.{url}", get_random_element(v.iter()));
        }

        url
    }

    /// Generate random URL
    ///
    /// return example: <https://sub.domain.com:8000/>
    ///
    /// # Arguments
    /// * `scheme` - URLScheme provide url scheme
    /// * `port_range` - PortRange enum
    /// * `tld_type` - TLDType provide hostname domain
    /// * `subdomains` - vec of subdomains
    pub fn url(scheme: Option<URLScheme>, port_range: Option<PortRange>, tld_type: Option<TLDType>, subdomains: Option<Vec<&str>>) -> String {
        let hostname = Self::hostname(tld_type, subdomains);
        let scheme = validate_enum(scheme, None);
        let mut url = format!("{scheme}://{hostname}");

        if port_range.is_some() {
            url = format!("{url}:{}", Self::port(port_range));
        }

        format!("{url}/")
    }

    /// Generate a random URI
    ///
    /// return example: <https://sub.domain.com:8000/2013/6/6/?some-things&test-test>
    ///
    /// # Arguments
    /// * `scheme` - URLScheme for url prefix
    /// * `port_range` - PortRange enum for port
    /// * `tld_type` - TLDType provide hostname domain
    /// * `subdomains` - vec of subdomains
    /// * `query_params_count` - Query params count
    pub fn uri(scheme: Option<URLScheme>, port_range: Option<PortRange>, tld_type: Option<TLDType>, subdomains: Option<Vec<&str>>, query_params_count: Option<usize>) -> String {
        let directory = Datetime::date(2010, chrono::Local::now().year()).format("%Y/%m/%d");

        let mut url = format!("{}{directory}/{}", Self::url(scheme, port_range, tld_type, subdomains), Self::slug(None));

        if query_params_count.is_some() {
            url = format!("{url}?{}", Self::query_string(query_params_count));
        }

        url
    }

    /// Generate arbitrary query string of given length
    ///
    /// return example: some-things&test-test
    ///
    /// # Arguments
    /// * `length` - Query params count
    pub fn query_string(length: Option<usize>) -> String {
        let formated = Self::query_parameters(length).iter().map(|p| {
            format!("{}={}", p.0, p.1)
        }).join("&");
    
        urlencoding::encode(&formated).into_owned()
    }

    /// Generate arbitrary query parameters as a set
    ///
    /// return example: vec![('some', 'things'), ('test', 'test')]
    ///
    /// # Arguments
    /// * `length` - Query params count
    pub fn query_parameters(length: Option<usize>) -> Vec<(&'static str, &'static String)> {
        let text = &Text(Locale::RU);
        let length = length.unwrap_or_else(|| randint(1, 10));

        if length > 32 {
            panic!("Length should be less than 32!")
        }

        let mut unique_words: Vec<&'static str> = vec![];
        while unique_words.len() < length {
            let word = text.word();
            if !unique_words.contains(&word) {
                unique_words.push(word)
            }
        }

        unique_words.into_iter().zip(text.words(length)).collect()
    }
}