Skip to content

Commit

Permalink
refactor: remove manual implementation of fs::read_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
lavafroth authored and Shinyzenith committed Feb 24, 2024
1 parent 30f25b5 commit 4c4e995
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 29 deletions.
12 changes: 2 additions & 10 deletions swhkd/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use itertools::Itertools;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::fs;
use std::{
fmt,
path::{Path, PathBuf},
Expand Down Expand Up @@ -75,13 +74,6 @@ pub struct Config {
pub imports: Vec<PathBuf>,
}

pub fn load_file_contents(path: &Path) -> Result<String, Error> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}

impl Config {
pub fn get_imports(contents: &str) -> Result<Vec<PathBuf>, Error> {
let mut imports = Vec::new();
Expand All @@ -96,7 +88,7 @@ impl Config {
}

pub fn new(path: &Path) -> Result<Self, Error> {
let contents = load_file_contents(path)?;
let contents = fs::read_to_string(path)?;
let imports = Self::get_imports(&contents)?;
Ok(Config { path: path.to_path_buf(), contents, imports })
}
Expand Down
21 changes: 2 additions & 19 deletions swhkd/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod test_config {
use crate::config::{
extract_curly_brace, load, load_file_contents, parse_contents, Error, Hotkey, Modifier,
ParseError, Prefix,
extract_curly_brace, load, parse_contents, Error, Hotkey, Modifier, ParseError, Prefix,
};
use std::fs;
use std::io::Write;
Expand Down Expand Up @@ -99,22 +98,6 @@ mod test_config {
Ok(())
}

#[test]
fn test_nonexistent_file() {
let path = PathBuf::from(r"This File Doesn't Exist");

let result = load_file_contents(&path);

assert!(result.is_err());

match result.unwrap_err() {
Error::ConfigNotFound => {}
_ => {
panic!("Error type for nonexistent file is wrong.");
}
}
}

#[test]
fn test_existing_file() -> std::io::Result<()> {
let setup = TestPath::new("/tmp/swhkd-test-file1");
Expand All @@ -129,7 +112,7 @@ q
bspc node -q",
)?;

let result = load_file_contents(&setup.path());
let result = fs::read_to_string(&setup.path());
assert!(result.is_ok());
Ok(())
}
Expand Down

0 comments on commit 4c4e995

Please sign in to comment.