-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_write.rs
145 lines (139 loc) · 3.94 KB
/
read_write.rs
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
use memmap::Mmap;
use same_file::Handle;
use std::{
fs::File,
io::{BufRead, BufReader, Error, ErrorKind, Write},
path::Path,
};
/// Reads a file
///
/// # Arguments
///
/// * `file_path` - A string slice that holds the path to the file
///
/// # Returns
///
/// A vector of strings that holds the lines of the file
///
/// # Examples
///
/// ```
/// use file_system::read_file_lines;
///
/// let file_path = "tests/test_read.txt";
/// let lines = read_file_lines(file_path);
/// assert_eq!(lines, vec!["Reading from test_read.txt", ":)"]);
/// ```
pub fn read_file_lines(file_path: &str) -> Vec<String> {
let file = File::open(file_path).unwrap();
let file_buffered = BufReader::new(file);
file_buffered.lines().map(|line| line.unwrap()).collect()
}
/// Writes to a file
///
/// # Arguments
///
/// * `file_path` - A string slice that holds the path to the file
/// * `content` - A string slice that holds the content to write to the file
///
/// # Returns
///
/// A vector of strings that holds the lines of the file
///
/// # Examples
///
/// ```ignore
/// use file_system::write_to_file;
/// use std::{fs::remove_file, path::Path};
///
/// let file_path = "tests/test_write.txt";
/// let content = "Writing to test_write.txt";
/// let lines = write_to_file(file_path, content);
/// assert_eq!(lines, vec!["Writing to test_write.txt"]);
///
/// remove_file(file_path).unwrap();
/// assert!(!Path::new(file_path).exists());
/// ```
pub fn write_to_file(file_path: &str, content: &str) -> Vec<String> {
let mut file = File::create(file_path).unwrap();
write!(file, "{}", content).unwrap();
read_file_lines(file_path)
}
/// Writes to a file and reads from a different file
///
/// # Arguments
///
/// * `file_path` - A string slice that holds the path to the file
/// * `content` - A string slice that holds the content to write to the file
///
/// # Returns
///
/// A vector of strings that holds the lines of the file
///
/// # Errors
///
/// Returns an error if the file path is the same
///
/// # Examples
///
/// ```ignore
/// use file_system::avoid_writing_reading_same_file;
/// use std::{fs::{remove_file, File}, path::Path};
///
/// let file_path = "test_avoid.txt";
/// File::create(file_path).unwrap();
///
/// let content = "Writing to test_avoid.txt";
/// let lines = avoid_writing_reading_same_file(file_path, content).unwrap();
/// assert_eq!(lines, vec!["Writing to test_avoid.txt"]);
///
/// remove_file(file_path).unwrap();
/// ```
pub fn avoid_writing_reading_same_file(
file_path: &str,
content: &str,
) -> Result<Vec<String>, Error> {
let path = Path::new(file_path);
let stdout_handle = Handle::stdout().unwrap();
let handle = Handle::from_path(path).unwrap();
if stdout_handle == handle {
return Err(Error::new(
ErrorKind::Other,
"Writing to and reading from the same file",
));
} else {
let mut file = File::create(file_path).unwrap();
write!(file, "{}", content).unwrap();
Ok(read_file_lines(file_path))
}
}
/// Accesses a file randomly using memory map
///
/// # Arguments
///
/// * `file_path` - A string slice that holds the path to the file
/// * `indexes` - A slice of unsigned integers that holds the indexes to access the file
///
/// # Returns
///
/// A vector of unsigned integers that holds the bytes of the file
///
/// # Examples
///
/// ```
/// use file_system::access_file_randomly_using_memory_map;
///
/// let file_path = "tests/test_memory_map.txt";
/// let indexes = vec![0, 1, 2, 19];
/// let lines = access_file_randomly_using_memory_map(file_path, &indexes);
/// assert!(lines.is_ok());
/// ```
pub fn access_file_randomly_using_memory_map(
file_path: &str,
indexes: &[usize],
) -> Result<(Vec<u8>, Mmap), Box<dyn std::error::Error>> {
let file = File::open(file_path).unwrap();
let map = unsafe { Mmap::map(&file)? };
let random_bytes: Vec<u8> = indexes.iter().map(|&index| map[index]).collect();
Ok((random_bytes, map))
}