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
use crate::entities::chapter::Chapter;
use crate::errors::TextFileOutputError;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
pub struct TextFile {
chapter: Chapter,
}
impl<'file_handling> TextFile {
pub fn new(path_from: &str) -> Result<Self, Box<dyn std::error::Error + 'static>> {
let text = fs::read_to_string(path_from)?;
Ok(Self {
chapter: Chapter::new(text.as_str()),
})
}
pub fn format_and_save(self, path_to: &'file_handling str) -> Result<(), TextFileOutputError> {
Self::touch_file(path_to).and_then(|file| self.save_file(file))
}
fn touch_file(path_to: &'file_handling str) -> Result<File, TextFileOutputError> {
if Path::new(path_to).exists() {
return Err(TextFileOutputError::AlreadyExists(path_to));
}
match File::create(path_to) {
Ok(file) => Ok(file),
Err(cause) => Err(TextFileOutputError::CannotCreate(cause)),
}
}
fn save_file(self, mut file: File) -> Result<(), TextFileOutputError<'file_handling>> {
let result = match writeln!(file, "{}", self.chapter.get()) {
Ok(_) => Ok(()),
Err(cause) => return Err(TextFileOutputError::CannotWrite(cause)),
};
match file.flush() {
Ok(_) => result,
Err(cause) => Err(TextFileOutputError::CannotFlush(cause)),
}
}
}
#[cfg(test)]
mod tests {
use super::TextFile;
use file_diff::diff;
use std::{fs, time};
#[test]
fn can_save_formatted_text() {
let source_file_path = "./resources/test/entities/file/source.txt";
let reference_file_path = "./resources/test/entities/file/reference.txt";
let now = time::Instant::now();
let target_file_path = format!(
"./resources/test/entities/file/target-{}.txt",
now.elapsed().as_millis()
);
let source_file = TextFile::new(source_file_path).unwrap();
source_file.format_and_save(target_file_path.as_str()).unwrap();
let is_target_text_is_same_to_reference = diff(target_file_path.as_str(), reference_file_path);
fs::remove_file(target_file_path).unwrap();
assert!(is_target_text_is_same_to_reference);
}
#[test]
fn return_error_when_target_file_already_exists() {
let source_file_path = "./resources/test/entities/file/source.txt";
let target_file_path = "./resources/test/entities/file/already_exists.txt";
let source_file = TextFile::new(source_file_path).unwrap();
let is_return_error = source_file.format_and_save(target_file_path).is_err();
let is_target_file_not_modified = fs::metadata(target_file_path).unwrap().len() == 0;
assert!(is_return_error);
assert!(is_target_file_not_modified);
}
#[test]
fn return_error_when_source_file_does_not_exists() {
let source_file_path = "./resources/test/entities/file/not/exists.txt";
let is_return_error = TextFile::new(source_file_path).is_err();
assert!(is_return_error);
}
#[test]
fn return_error_when_source_file_path_is_invalid() {
let source_file_path = "\\not.valid>/pat h@";
let is_return_error = TextFile::new(source_file_path).is_err();
assert!(is_return_error);
}
}