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
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
const READ_ERR: &str = "something went wrong reading the file";
const WRITE_ERR: &str = "something went wrong writing the file";
const NOT_FOUND: &str = "file not found";
pub fn read_file_into_line_vec(filename: &str) -> Vec<String> {
let mut target: Vec<String> = vec![];
for line in open_file_as_string(filename).split("\n") {
target.push(String::from(line));
}
target
}
pub fn read_file_into_char_vec(filename: &str) -> Vec<String> {
let mut target: Vec<String> = vec![];
for line in open_file_as_string(filename).split("") {
target.push(String::from(line));
}
target
}
pub fn open_file_as_string(filename: &str) -> String {
let mut file = File::open(filename).expect(NOT_FOUND);
let mut contents = String::new();
file.read_to_string(&mut contents).expect(READ_ERR);
contents
}
pub fn write_file(source: String, target: &str) {
let mut file = File::create(target).expect(NOT_FOUND);
file.write_all(source.as_bytes()).expect(WRITE_ERR);
}
pub fn is_dup(dup_map: &mut HashMap<String, bool>, id: &str) -> bool {
match dup_map.get(id) {
Some(_) => true,
None => {
dup_map.insert(String::from(id), true);
false
}
}
}
pub fn store_into_map(
store_map: &mut HashMap<String, Vec<String>>,
key: &str,
value: String,
) -> bool {
let mut vals: Vec<String> = vec![];
let exists = match store_map.get(key) {
Some(values) => {
vals = values.to_vec();
true
}
None => false,
};
let mut new_values: Vec<String> = vec![];
if exists == false {
vals.push(value);
}
for val in vals {
new_values.push(val.to_owned());
}
store_map.insert(String::from(key), new_values);
exists
}
pub fn sub(source: &String, pattern: &str, replacement: &str) -> String {
String::from(str::replace(&source, pattern, replacement))
}
pub fn create_duplicate_map() -> HashMap<String, bool> {
let dup_map: HashMap<String, bool> = HashMap::new();
dup_map
}
pub fn create_store_map() -> HashMap<String, Vec<String>> {
let store_map: HashMap<String, Vec<String>> = HashMap::new();
store_map
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_stacks_lines_into_vec() {
let string_vec: Vec<String> = read_file_into_line_vec("./fixtures/test/test.csv");
assert_eq!("hello,", string_vec[0]);
assert_eq!("world,", string_vec[1]);
assert_eq!("foo,", string_vec[2]);
assert_eq!("bar", string_vec[3]);
}
#[test]
fn it_stacks_chars_into_vec() {
let string_vec: Vec<String> = read_file_into_char_vec("./fixtures/test/test.csv");
assert_eq!("", string_vec[0]);
assert_eq!("h", string_vec[1]);
assert_eq!("e", string_vec[2]);
}
#[test]
fn it_opens_file_as_a_string() {
let string_file: String = open_file_as_string("./fixtures/test/test.csv");
assert_eq!("hello,\nworld,\nfoo,\nbar\n", string_file);
}
#[test]
fn it_replaces_string_contents() {
let string: String = "hello\nworld\nfoo\nbar".to_string();
assert_eq!(String::from("helloworldfoobar"), sub(&string, "\n", ""));
}
#[test]
fn it_can_create_a_duplicate_entry_if_id_not_found_and_find_duplicates() {
let mut dups = create_duplicate_map();
assert!(!is_dup(&mut dups, "90"));
assert!(is_dup(&mut dups, "90"))
}
#[test]
fn it_can_store_into_map() {
let mut store = create_store_map();
assert!(!store_into_map(
&mut store,
"1",
"1,er,gh,45,epp".to_string()
));
assert!(store_into_map(
&mut store,
"1",
"1,as,vb,45,abb".to_string()
));
}
#[test]
fn it_reads_and_writes_a_large_file() {
let file = open_file_as_string("fixtures/test/test.csv");
write_file(file, "fixtures/results/test.csv");
assert!(true);
}
}