Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove manual implementation of fs::read_to_string #238

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading