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
///Main module that contains all useful elements like Person struct, parse func etc
pub mod person_module {
    use std::fs::File;
    use std::io::prelude::*;
    use thiserror::Error;

    ///My error type using thiserror to handle error situations
    #[derive(Error, Debug)]
    pub enum MyError {
        ///Global error of parsing
        #[error("An error occurred: {0}")]
        PSPError(String),
        ///Error for the situation when the field from input string is incorrect
        #[error("An error occurred incorrect field: {0}")]
        IncorrectField(String),
        ///File error
        #[error("An error occurred incorrect file: {0}")]
        IncorrectFile(String),
    }

    ///A struct to contain the information about a person such as name, age,city and zip
    pub struct Person {
        ///name of the person
        pub name: String,
        ///age of the person
        pub age: u32,
        ///city where the person lives
        pub city: String,
        ///zip
        pub zip: u32,
    }
    impl std::fmt::Display for Person {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            let mut norm_zip: String = self.zip.to_string();
            if norm_zip.len() != 5 {
                while norm_zip.len() < 5 {
                    norm_zip = String::from("0").to_string() + &norm_zip.to_string();
                }
                if norm_zip.len() > 5 {
                    norm_zip = String::from("00000");
                }
            }
            write!(f, "{} {} {}{}", self.name, self.age, self.city, norm_zip)
        }
    }

    impl Person {
        ///A function to reduce Person object to normal view
        pub fn normalize(&mut self) -> &mut Self {
            if !self.name.is_empty() {
                let s = self.name.chars().next().unwrap().to_ascii_uppercase();
                self.name.make_ascii_lowercase();
                self.name.remove(0);
                self.name.insert(0, s);
            }
            if !self.city.is_empty() {
                let s = self.city.chars().next().unwrap().to_ascii_uppercase();
                self.city.make_ascii_lowercase();
                self.city.remove(0);
                self.city.insert(0, s);
            }
            if self.age > 999 {
                self.age = 0;
            }
            let mut modified_city = String::new();
            let mut must_be_high = true; // починаємо з великої літери

            for s in self.city.chars() {
                if must_be_high {
                    modified_city.push_str(&s.to_uppercase().to_string());
                    must_be_high = false;
                } else {
                    modified_city.push(s);
                }

                if s == ' ' || s == '-' {
                    must_be_high = true;
                }
            }

            self.city = modified_city;
            if self.zip > 99999 {
                self.zip = 0;
            }
            self
        }
    }
    ///Main function to parse String object into the Person object(with normalization)
    pub fn parse(string: &str) -> Result<Person, MyError> {
        let mut tname = String::from("");
        let mut tage = String::from("");
        let mut tcity = String::from("");
        let mut tzip = String::from("");

        let mut has_name = false;
        let mut has_age = false;
        let mut has_city = false;
        let mut has_zip = false;
        let mut city_gaps = 0;
        let mut city_hyphens = 0;

        for s in String::from(string).chars() {
            if !has_name {
                if s.is_ascii_alphabetic() {
                    has_name = true;
                }
            } else if has_name && !has_age {
                if s.is_ascii_digit() {
                    has_age = true;
                }
            } else if has_age && !has_city && s.is_ascii_alphabetic() {
                has_city = true;
            } else if has_city && !has_zip && s.is_ascii_digit() {
                has_zip = true;
            } else if has_city && !has_zip {
                if s == '-' {
                    city_hyphens += 1;
                } else if s == ' ' {
                    city_gaps += 1;
                }
            }
        }

        if !has_age {
            return Err(MyError::IncorrectField("age".to_string()));
        } else if !has_city
            || city_gaps > 1
            || city_hyphens > 1
            || (city_gaps == 1 && city_hyphens == 1)
        {
            return Err(MyError::IncorrectField("city".to_string()));
        } else if !has_name {
            return Err(MyError::IncorrectField("name".to_string()));
        } else if !has_zip {
            return Err(MyError::IncorrectField("zip".to_string()));
        }

        let mut must_be_name = true;
        let mut must_be_age = false;
        let mut must_be_city = false;
        let mut must_be_zip = false;

        for s in String::from(string).chars() {
            if !s.is_ascii_digit()
                && !s.is_ascii_alphabetic()
                && !(must_be_city && (s == ' ' || s == '-'))
            {
            } else if must_be_name {
                if s.is_ascii_alphabetic() {
                    tname.push(s);
                } else {
                    if s.is_ascii_digit() {
                        tage.push(s);
                    }
                    must_be_name = false;
                    must_be_age = true;
                }
            } else if must_be_age {
                if s.is_ascii_digit() {
                    tage.push(s);
                } else {
                    if s.is_ascii_alphabetic() {
                        tcity.push(s);
                    }
                    must_be_age = false;
                    must_be_city = true;
                }
            } else if must_be_city {
                if s.is_ascii_alphabetic() || ((s == '-' || s == ' ') && tcity.len() > 2) {
                    tcity.push(s);
                } else {
                    must_be_city = false;
                    must_be_zip = true;
                    if s.is_ascii_digit() {
                        tzip.push(s);
                    }
                }
            } else if must_be_zip && s.is_ascii_digit() {
                tzip.push(s);
            }
        }

        if tage.is_empty() {
            return Err(MyError::IncorrectField("age".to_string()));
        } else if tcity.is_empty() {
            return Err(MyError::IncorrectField("city".to_string()));
        } else if tname.is_empty() {
            return Err(MyError::IncorrectField("name".to_string()));
        } else if tzip.is_empty() {
            return Err(MyError::IncorrectField("zip".to_string()));
        }

        let unnorm_age = tage.parse::<u32>();
        let unnorm_zip = tzip.parse::<u32>();
        let norm_age: u32 = match unnorm_age {
            Ok(value) => value,
            Err(_) => {
                let err = MyError::PSPError("Invalid age parsing".to_string());
                return Err(err);
            }
        };

        let norm_zip: u32 = match unnorm_zip {
            Ok(value) => value,
            Err(_) => {
                let err = MyError::PSPError("Invalid zip parsing".to_string());
                return Err(err);
            }
        };
        let mut person = Person {
            name: tname,
            age: norm_age,
            city: tcity,
            zip: norm_zip,
        };
        person.normalize();
        Ok(person)
    }

    ///Function to parse content and write it to the file
    pub fn write_to_file(file_path: &str, content: &str) -> Result<(), MyError> {
        let mut file = match File::create(file_path) {
            Ok(file) => file,
            Err(_) => return Err(MyError::IncorrectFile("CreateError".to_string())),
        };
        let person = parse(content);

        match person {
            Ok(_) => {}
            Err(_) => return Err(MyError::PSPError("ParseToFile".to_string())),
        }
        let str_person = format!("{}", person.unwrap());

        match file.write_all(str_person.as_bytes()) {
            Ok(_) => Ok(()),
            Err(_) => Err(MyError::IncorrectFile("WriteError".to_string())),
        }
    }
}