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
use super::{Error, PathBuf, File, Read, Write};
pub fn load_csv(csv_file: PathBuf) -> Result<String, Error> {
let file = read(csv_file)?;
Ok(file)
}
pub fn write_csv(csv_data: &str, filename: &str) -> Result<(), Error>
{
write(csv_data, filename)?;
Ok(())
}
fn read(path: PathBuf) -> Result<String, Error> {
let mut buffer = String::new();
let mut file = open(path)?;
file.read_to_string(&mut buffer)?;
if buffer.is_empty() {
return Err("input file missing")?
}
Ok(buffer)
}
fn open(path: PathBuf) -> Result<File, Error> {
let file = File::open(path)?;
Ok(file)
}
fn write(data: &str, filename: &str) -> Result<(), Error> {
let mut buffer = File::create(filename)?;
buffer.write_all(data.as_bytes())?;
Ok(())
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
use super::{load_csv, write_csv};
#[test]
fn test_valid_load_csv(){
let filename = PathBuf::from("./input/challenge.csv");
let csv_data = load_csv(filename);
assert!(csv_data.is_ok());
}
#[test]
#[ignore]
fn test_invalid_load_csv(){
let filename = PathBuf::from("./input/other.csv");
let csv_data = load_csv(filename);
assert!(csv_data.is_err());
}
#[test]
fn test_valid_write_csv(){
let filename = PathBuf::from("./input/challenge.csv");
let modified_data = r"a,b,c,d,e\nf,g,h,i,j";
let output_file = write_csv(&modified_data, "output/test.csv");
assert!(output_file.is_ok());
}
}