Skip to content

Commit 9fa9719

Browse files
committed
minor changes
1 parent 6343ba1 commit 9fa9719

File tree

2 files changed

+28
-49
lines changed

2 files changed

+28
-49
lines changed

src/fs_manager/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ fn create_server_properties() -> io::Result<()> {
4141
let path = Path::new(consts::file_paths::PROPERTIES);
4242
let content = consts::file_contents::server_properties();
4343

44-
utils::create_file(path, &content)
44+
utils::create_file(path, Some(&content))
4545
}
4646

4747
/// Creates the 'eula.txt' file if it does not already exist.
4848
fn create_eula() -> io::Result<()> {
4949
let path = Path::new(consts::file_paths::EULA);
5050
let content = consts::file_contents::eula();
5151

52-
utils::create_file(path, &content)
52+
utils::create_file(path, Some(&content))
5353
}
5454

5555
/// Check if the 'eula.txt' has been agreed to.
@@ -69,47 +69,47 @@ fn check_eula() -> io::Result<bool> {
6969
}
7070

7171
pub fn create_other_files() {
72-
match utils::create_file_nn(Path::new(consts::file_paths::BANNED_IP)) {
72+
match utils::create_file(Path::new(consts::file_paths::BANNED_IP), None) {
7373
Ok(_) => (),
7474
Err(e) => error!(
7575
"Failed to create the file {} as error:{}",
7676
consts::file_paths::BANNED_IP,
7777
e
7878
),
7979
}
80-
match utils::create_file_nn(Path::new(consts::file_paths::BANNED_PLAYERS)) {
80+
match utils::create_file(Path::new(consts::file_paths::BANNED_PLAYERS), None) {
8181
Ok(_) => (),
8282
Err(e) => error!(
8383
"Failed to create the file {} as error:{}",
8484
consts::file_paths::BANNED_PLAYERS,
8585
e
8686
),
8787
}
88-
match utils::create_file_nn(Path::new(consts::file_paths::OPERATORS)) {
88+
match utils::create_file(Path::new(consts::file_paths::OPERATORS), None) {
8989
Ok(_) => (),
9090
Err(e) => error!(
9191
"Failed to create the file {} as error:{}",
9292
consts::file_paths::OPERATORS,
9393
e
9494
),
9595
}
96-
match utils::create_file_nn(Path::new(consts::file_paths::SESSION)) {
96+
match utils::create_file(Path::new(consts::file_paths::SESSION), None) {
9797
Ok(_) => (),
9898
Err(e) => error!(
9999
"Failed to create the file {} as error:{}",
100100
consts::file_paths::SESSION,
101101
e
102102
),
103103
}
104-
match utils::create_file_nn(Path::new(consts::file_paths::USERCACHE)) {
104+
match utils::create_file(Path::new(consts::file_paths::USERCACHE), None) {
105105
Ok(_) => (),
106106
Err(e) => error!(
107107
"Failed to create the file {} as error:{}",
108108
consts::file_paths::USERCACHE,
109109
e
110110
),
111111
}
112-
match utils::create_file_nn(Path::new(consts::file_paths::WHITELIST)) {
112+
match utils::create_file(Path::new(consts::file_paths::WHITELIST), None) {
113113
Ok(_) => (),
114114
Err(e) => error!(
115115
"Failed to create the file {} as error:{}",

src/fs_manager/utils.rs

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use log::{debug, info};
2-
use std::fs::metadata;
1+
use log::info;
32
use std::{
43
fs::{self, File, OpenOptions},
54
io::{self, Write},
@@ -8,63 +7,43 @@ use std::{
87

98
/// Creates a file if it does not already exist.
109
/// If the file already exists, it doesn't modify its content.
11-
pub fn create_file(path: &Path, content: &str) -> io::Result<()> {
10+
pub fn create_file(path: &Path, content: Option<&str>) -> io::Result<()> {
1211
match OpenOptions::new().write(true).create_new(true).open(path) {
13-
Ok(mut file) => {
14-
info!("File '{}' created.", path.to_string_lossy());
15-
file.write_all(content.as_bytes())?;
16-
Ok(())
17-
}
12+
Ok(mut file) => content.map_or_else(
13+
|| {
14+
info!("Created an empty file: '{}'", path.display());
15+
Ok(())
16+
},
17+
|s| {
18+
file.write_all(s.as_bytes())
19+
.inspect(|_| info!("File '{}' created.", path.display()))
20+
},
21+
),
1822
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
19-
info!(
20-
"File '{}' already exists. Not altering it.",
21-
path.to_string_lossy()
22-
);
23-
Ok(())
24-
}
25-
Err(e) => Err(e),
26-
}
27-
}
28-
///Create_file with no content...
29-
pub fn create_file_nn(path: &Path) -> io::Result<()> {
30-
// Verify if the file does not already exist.
31-
if metadata(path).is_ok() {
32-
info!(
33-
"File '{}' already exists. Not altering it.",
34-
path.to_string_lossy()
35-
);
36-
return Ok(());
37-
}
38-
39-
// Create the file.
40-
match std::fs::File::create(path) {
41-
Ok(_) => {
42-
info!("File '{}' created.", path.to_string_lossy());
23+
info!("File '{}' already exists. Not altering it.", path.display());
4324
Ok(())
4425
}
4526
Err(e) => Err(e),
4627
}
4728
}
4829

49-
/// Creates a file given its path.
30+
/// Creates a directory given its path.
5031
pub fn create_dir(path: &Path) -> io::Result<()> {
5132
if path.exists() {
5233
info!(
5334
"Directory '{}' already exists. Not altering it.",
54-
path.to_string_lossy()
35+
path.display()
5536
);
5637
return Ok(());
5738
}
58-
fs::create_dir(path)?;
59-
info!("Created directory: '{}'", path.to_string_lossy());
60-
Ok(())
39+
fs::create_dir(path).inspect(|_| info!("Created directory: '{}'", path.display()))
6140
}
6241

6342
/// Opens an already existing file and overwrites all content with `content`.
6443
pub fn overwrite_file(path: &Path, content: &str) -> std::io::Result<()> {
6544
let mut file = File::create(path)?;
66-
debug!("Overwrote file '{}'", path.to_string_lossy());
6745
file.write_all(content.as_bytes())
46+
.inspect(|_| info!("Overwrote file: '{}'", path.display()))
6847
}
6948

7049
#[cfg(test)]
@@ -107,18 +86,18 @@ mod tests {
10786
// Test 1: Create a new file
10887
let file_path = temp_dir.path().join("test1.txt");
10988
let content = "Hello, World!";
110-
create_file(&file_path, content)?;
89+
create_file(&file_path, Some(content))?;
11190
assert!(file_path.exists());
11291
assert_eq!(fs::read_to_string(&file_path)?, content);
11392

11493
// Test 2: Attempt to create an existing file (should not modify)
11594
let existing_content = fs::read_to_string(&file_path)?;
116-
create_file(&file_path, "New content")?;
95+
create_file(&file_path, Some("New content"))?;
11796
assert_eq!(fs::read_to_string(&file_path)?, existing_content);
11897

11998
// Test 3: Create file with empty content
12099
let empty_file_path = temp_dir.path().join("empty.txt");
121-
create_file(&empty_file_path, "")?;
100+
create_file(&empty_file_path, Some(""))?;
122101
assert!(empty_file_path.exists());
123102
assert_eq!(fs::read_to_string(&empty_file_path)?, "");
124103

0 commit comments

Comments
 (0)