use std::path::Path;
pub fn load_file_as_string<P: AsRef<Path>>(path: P) -> String {
let path = path.as_ref();
std::fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file at '{path:?}'."))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::save::save_string_to_file;
use crate::test_utils::get_temp_dir_path;
use tempfile::tempdir;
#[test]
fn test_save_load_file_string() {
let temp_dir = tempdir().unwrap();
let temp_dir_path = get_temp_dir_path(&temp_dir);
let file_path = temp_dir_path.join("test_file.txt");
let file_paths: Vec<Box<dyn AsRef<Path>>> = vec![
Box::new(file_path.to_str().unwrap()), Box::new(file_path.to_str().unwrap().to_string()), Box::new(file_path.as_path()), Box::new(file_path.clone()), ];
for file_path in file_paths {
let file_path = file_path.as_ref();
let content = "Hello, world!";
save_string_to_file(content, file_path);
let loaded_content = load_file_as_string(file_path);
assert_eq!(loaded_content, content);
}
}
}