Skip to main content

mockd/
generator.rs

1//!
2//! Provides 1 function to generate a string based on input string.
3//!
4//! # Tags
5//!
6//! The input string can contain the following tags:
7//! * contact.email
8//! * hacker.abbreviation
9//! * hacker.adjective
10//! * hacker.noun
11//! * hacker.verb
12//! * hacker.ingverb
13//! * person.first
14//! * person.last
15//!
16//! # Letters and Numbers
17//!
18//! Hashtags (#) are replaced with numbers and question marks are
19//! replaced with letters.
20//!
21//! # Examples
22//!
23//! ```rust
24//! use mockd::generator;
25//!
26//!     let data = generator::generate("{person.first} {person.last} {contact.email} #?#?#?".to_string()); // data: Watson Connelly baileeprosacco@smitham.biz 6d0e0a
27//!     // More details about this later
28//! ```
29//!
30//! # Feature
31//!
32//! Requires the "generator" feature.
33//!
34
35use crate::contact;
36use crate::hacker;
37use crate::person::data as person_data;
38
39use crate::misc;
40
41///
42/// Provides 1 function to generate a string based on input string.
43///
44/// # Tags
45///
46/// The input string can contain the following tags:
47/// * contact.email
48/// * hacker.abbreviation
49/// * hacker.adjective
50/// * hacker.noun
51/// * hacker.verb
52/// * hacker.ingverb
53/// * person.first
54/// * person.last
55///
56/// # Letters and Numbers
57///
58/// Hashtags (#) are replaced with numbers and question marks are
59/// replaced with letters.
60///
61/// # Examples
62///
63/// ```rust
64/// use mockd::generator;
65///
66///     let data = generator::generate("{person.first} {person.last} {contact.email} #?#?#?".to_string()); // data: Watson Connelly baileeprosacco@smitham.biz 6d0e0a
67///     // More details about this later
68/// ```
69///
70/// # Feature
71///
72/// Requires the "generator" feature.
73///
74pub fn generate(data: String) -> String {
75    let mut d_validate_left = data.matches('{').count();
76    let mut d_validate_right = data.matches('}').count();
77
78    let mut m_data = data; // .clone();
79
80    while d_validate_left > 0 && d_validate_right > 0 && d_validate_left == d_validate_right {
81        let place_of_left = m_data.find('{').unwrap();
82        let place_of_right = m_data.find('}').unwrap();
83
84        let tag = &m_data[place_of_left + 1..place_of_right];
85        let text = resolve_tag(tag);
86        let tag_formatted = format!("{}{}{}", "{", tag, "}");
87        m_data = m_data.replace(&tag_formatted[..], &text[..]);
88
89        d_validate_left = m_data.matches('{').count();
90        d_validate_right = m_data.matches('}').count();
91    }
92
93    m_data = misc::replace_with_numbers(m_data);
94    m_data = misc::replace_with_letter(m_data);
95
96    m_data
97}
98
99fn resolve_tag(tag: &str) -> String {
100    match tag {
101        "contact.email" => contact::email(),
102        "hacker.abbreviation" => hacker::abbreviation(),
103        "hacker.adjective" => hacker::adjective(),
104        "hacker.noun" => hacker::noun(),
105        "hacker.verb" => hacker::verb(),
106        "hacker.ingverb" => hacker::ingverb(),
107        "person.first" => misc::random_data(person_data::FIRST).to_string(),
108        "person.last" => misc::random_data(person_data::LAST).to_string(),
109
110        _ => "".to_string(),
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use crate::generator;
117    use crate::testify::exec_mes;
118
119    #[test]
120    fn generate() {
121        exec_mes("beer::name", || {
122            generator::generate("{person.first} {person.last} {contact.email} #?#?#?".to_string())
123        });
124    }
125}