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 std::fs::File;
use std::io::{Read, Write};

pub struct FileHandler;

pub struct FileStruct {
    pub file_path: String,
}

impl FileHandler {
    /// # Panics
    ///
    /// - Will panic if file is not readable
    /// - Will panic if your current user does not have the
    /// required permissions to open the file

    #[must_use]
    pub fn read_file(file_path: &str) -> String {
        let mut file = File::open(file_path).expect("Error while opening file");

        let mut content = String::new();
        file.read_to_string(&mut content)
            .expect("Error while reading from file");

        content
    }

    /// # Panics
    ///
    /// - Will panic if file is not writeable
    /// - Will panic if your current user does not have the
    /// required permissions to open the file

    pub fn write_to_file(file_path: &str, content: &str) {
        let mut file = File::create(file_path).expect("Error while creating or opening file");

        file.write_all(content.as_bytes())
            .expect("Error while writing to file");
    }
}

impl FileStruct {
    /// # Panics
    ///
    /// - Will panic if file is not readable
    /// - Will panic if your current user does not have the
    /// required permissions to open the file

    #[must_use]
    pub fn read(&self) -> String {
        let mut file: File = File::open(&self.file_path).expect("Error while opening file");

        let mut content = String::new();

        file.read_to_string(&mut content)
            .expect("Error while reading file");

        content
    }

    /// # Panics
    ///
    /// - Will panic if file is not writeable
    /// - Will panic if your current user does not have the
    /// required permissions to open the file

    pub fn write(&self, content: &str) {
        let mut file = File::create(&self.file_path).expect("Error while creating or opening file");

        file.write_all(content.as_bytes())
            .expect("Error while writing to file");
    }
}