Skip to content

Commit

Permalink
refactor(fs): move fs related code out of main
Browse files Browse the repository at this point in the history
Signed-off-by: Christina Sørensen <christina@cafkafk.com>
  • Loading branch information
cafkafk committed Sep 22, 2023
1 parent 6c9b8a7 commit 26b5736
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
66 changes: 66 additions & 0 deletions src/fs/dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::fs::File;
use std::io::Write;
use std::path::Path;

/// Dumps a set of strings into individual files within a specified directory.
///
/// This function creates a directory (if it doesn't exist) and writes each
/// string from the provided `output_strings` vector to a new file within that
/// directory. The filename is derived from the content of the string,
/// specifically from the second line which is expected to contain an "args"
/// field.
///
/// # Parameters
///
/// - `dump_dir`: The path to the directory where the files should be dumped.
/// - `output_strings`: A vector of strings where each string represents the content to be written
/// to a new file in the dump directory.
///
/// # Panics
///
/// This function will panic if:
/// - It fails to create the dump directory.
/// - It fails to create a file within the dump directory.
/// - It fails to write to a file.
///
/// # Examples
///
/// ```rust
/// use dir::dump_dir;
///
/// let contents = vec![
/// String::from("Some content with\nargs = \"tests/itest\"\nMore content"),
/// String::from("Another content with\nargs = \"sample_arg\"\nEven more content"),
/// ];
///
/// dump_dir("./dumps", contents);
/// ```
pub fn dump_dir(dump_dir: &str, output_strings: Vec<String>) {
// Create the dump directory if it doesn't exist
let dump_path = Path::new(dump_dir);
if !dump_path.exists() {
std::fs::create_dir(dump_path)
.unwrap_or_else(|_| panic!("Failed to create dump directory: {:?}", dump_path));
}

// Write each string in output_strings to a new file in the dump directory
for content in &output_strings {
// Extract the arguments part from the content
let args_line = content.lines().nth(1).unwrap_or("");
let args = args_line
.split("args = \"")
.nth(1)
.unwrap_or("")
.trim_end_matches('\"')
.replace([' ', '/'], "_") // This is to handle the "tests/itest" in ARGS
.replace(&['<', '>', ':', '"', '/', '\\', '|', '?', '*'][..], "_"); // Sanitize for Windows

let file_path = dump_path.join(format!("ptest_{}.toml", args));
let mut file =
File::create(&file_path).expect(&format!("Failed to create file at {:?}", file_path));
let mut file = File::create(&file_path)
.unwrap_or_else(|_| panic!("Failed to create file at {:?}", file_path));
file.write_all(content.as_bytes())
.expect("Failed to write to file");
}
}
2 changes: 2 additions & 0 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod dir;
pub use self::dir::dump_dir;
34 changes: 4 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::fs::{self, File};
use std::io::{BufReader, Write};
use std::path::Path;
use std::io::BufReader;

mod cli;
mod data;
mod fs;
mod math;
mod parser;
mod utils;
Expand Down Expand Up @@ -129,34 +128,9 @@ fn main() -> std::io::Result<()> {

println!("{output_strings:#?}");

// Create the dump directory if it doesn't exist
let binding = config.dump_dir.unwrap();
let dump_path = Path::new(&binding);
if !dump_path.exists() {
fs::create_dir(dump_path)
.unwrap_or_else(|_| panic!("Failed to create dump directory: {:?}", dump_path));
}
let dump_dir = config.dump_dir.unwrap();

// Write each string in output_strings to a new file in the dump directory
for content in &output_strings {
// Extract the arguments part from the content
let args_line = content.lines().nth(1).unwrap_or("");
let args = args_line
.split("args = \"")
.nth(1)
.unwrap_or("")
.trim_end_matches('\"')
.replace([' ', '/'], "_") // This is to handle the "tests/itest" in ARGS
.replace(&['<', '>', ':', '"', '/', '\\', '|', '?', '*'][..], "_"); // Sanitize for Windows

let file_path = dump_path.join(format!("ptest_{}.toml", args));
let mut file =
File::create(&file_path).expect(&format!("Failed to create file at {:?}", file_path));
let mut file = File::create(&file_path)
.unwrap_or_else(|_| panic!("Failed to create file at {:?}", file_path));
file.write_all(content.as_bytes())
.expect("Failed to write to file");
}
crate::fs::dump_dir(&dump_dir, output_strings);

Ok(())
}

0 comments on commit 26b5736

Please sign in to comment.