Skip to content

Commit 4d6fffc

Browse files
committed
refactor: remove manual implementation of fs::read_to_string
1 parent 30f25b5 commit 4d6fffc

File tree

2 files changed

+4
-29
lines changed

2 files changed

+4
-29
lines changed

Diff for: swhkd/src/config.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use itertools::Itertools;
22
use std::collections::HashMap;
3-
use std::fs::File;
4-
use std::io::Read;
3+
use std::fs;
54
use std::{
65
fmt,
76
path::{Path, PathBuf},
@@ -75,13 +74,6 @@ pub struct Config {
7574
pub imports: Vec<PathBuf>,
7675
}
7776

78-
pub fn load_file_contents(path: &Path) -> Result<String, Error> {
79-
let mut file = File::open(path)?;
80-
let mut contents = String::new();
81-
file.read_to_string(&mut contents)?;
82-
Ok(contents)
83-
}
84-
8577
impl Config {
8678
pub fn get_imports(contents: &str) -> Result<Vec<PathBuf>, Error> {
8779
let mut imports = Vec::new();
@@ -96,7 +88,7 @@ impl Config {
9688
}
9789

9890
pub fn new(path: &Path) -> Result<Self, Error> {
99-
let contents = load_file_contents(path)?;
91+
let contents = fs::read_to_string(path)?;
10092
let imports = Self::get_imports(&contents)?;
10193
Ok(Config { path: path.to_path_buf(), contents, imports })
10294
}

Diff for: swhkd/src/tests.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod test_config {
22
use crate::config::{
3-
extract_curly_brace, load, load_file_contents, parse_contents, Error, Hotkey, Modifier,
4-
ParseError, Prefix,
3+
extract_curly_brace, load, parse_contents, Error, Hotkey, Modifier, ParseError, Prefix,
54
};
65
use std::fs;
76
use std::io::Write;
@@ -99,22 +98,6 @@ mod test_config {
9998
Ok(())
10099
}
101100

102-
#[test]
103-
fn test_nonexistent_file() {
104-
let path = PathBuf::from(r"This File Doesn't Exist");
105-
106-
let result = load_file_contents(&path);
107-
108-
assert!(result.is_err());
109-
110-
match result.unwrap_err() {
111-
Error::ConfigNotFound => {}
112-
_ => {
113-
panic!("Error type for nonexistent file is wrong.");
114-
}
115-
}
116-
}
117-
118101
#[test]
119102
fn test_existing_file() -> std::io::Result<()> {
120103
let setup = TestPath::new("/tmp/swhkd-test-file1");
@@ -129,7 +112,7 @@ q
129112
bspc node -q",
130113
)?;
131114

132-
let result = load_file_contents(&setup.path());
115+
let result = fs::read_to_string(&setup.path());
133116
assert!(result.is_ok());
134117
Ok(())
135118
}

0 commit comments

Comments
 (0)