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;

/// Structure of Novel text file
pub struct TextFile {
    chapter: Chapter,
}

/// Implementation for novel text structure
impl<'file_handling> TextFile {
    /// Constructor
    ///
    /// # Fail
    ///
    /// * Path format is invalid
    /// * A file designated by a path is not exists
    ///  
    /// # Example
    ///
    /// ```no_run
    /// use naromat::entities::text_file::TextFile;
    ///
    /// TextFile::new("./path/to/source/file.txt").unwrap();
    /// ```
    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()),
        })
    }

    /// Format file text and save
    ///
    /// Return true if save was succeed
    ///
    /// Return false if save was failed
    ///
    /// # Example
    ///
    /// ```no_run
    /// use naromat::entities::text_file::TextFile;
    ///
    /// let text = TextFile::new("./path/to/source/file.txt").unwrap();
    /// text.format_and_save("./path/to/save.txt");
    /// ```
    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() {
        // given
        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();

        // when
        source_file.format_and_save(target_file_path.as_str()).unwrap();

        // then
        let is_target_text_is_same_to_reference = diff(target_file_path.as_str(), reference_file_path);
        // teardown
        fs::remove_file(target_file_path).unwrap();
        // assert
        assert!(is_target_text_is_same_to_reference);
    }

    #[test]
    fn return_error_when_target_file_already_exists() {
        // given
        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();

        // expect
        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
        assert!(is_return_error);
        assert!(is_target_file_not_modified);
    }

    #[test]
    fn return_error_when_source_file_does_not_exists() {
        // given
        let source_file_path = "./resources/test/entities/file/not/exists.txt";

        // expect
        let is_return_error = TextFile::new(source_file_path).is_err();

        // assert
        assert!(is_return_error);
    }

    #[test]
    fn return_error_when_source_file_path_is_invalid() {
        // given
        let source_file_path = "\\not.valid>/pat h@";

        // expect
        let is_return_error = TextFile::new(source_file_path).is_err();

        // assert
        assert!(is_return_error);
    }
}