From 3ac24f5ec9831bfc93154050a24b5e623eba6177 Mon Sep 17 00:00:00 2001 From: specCon18 Date: Thu, 30 May 2024 17:50:45 -0400 Subject: [PATCH] added toml support --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4be46e..ce6dd56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -293,7 +293,7 @@ checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "yunodo" -version = "0.3.0" +version = "0.5.0" dependencies = [ "clap", "ring", diff --git a/Cargo.toml b/Cargo.toml index a219e89..165b9c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "yunodo" -version = "0.3.0" +version = "0.5.0" edition = "2021" [dependencies] diff --git a/src/main.rs b/src/main.rs index 106c4b0..1e03e3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ use clap::Parser; -use std::{io, fs, path::PathBuf}; +use std::{io, fs, path::PathBuf,collections::HashMap}; //TODO: implement mod file_tree :ODOT// #[derive(Parser)] #[command(name = "YUNODO")] -#[command(version = "0.4.0")] +#[command(version = "0.5.0")] #[command(about = "parse file tree for //TODO: comments", long_about = "parses a directory of files for substrings of //TODO: and outputs all instances in a parsable format")] struct Cli { /// Sets a custom config file @@ -78,8 +78,9 @@ fn main() { eprintln!("Error: {}", err); } } - // out_as_md_table(output_csv_item); - out_as_json_object(output_csv_item); + out_as_md_table(output_csv_item.clone()); + out_as_json_object(output_csv_item.clone()); + out_as_toml_file(output_csv_item.clone()); } } @@ -132,13 +133,13 @@ fn out_as_md_table(input_csv:String){ println!("{}",line); } } -fn split_csv_rows(vec: &mut Vec<&str>) -> Vec { +fn split_csv(vec: &mut Vec<&str>,split:usize) -> Vec { let mut rows: Vec = Vec::new(); if !vec.is_empty() { - let mut vec2 = vec.split_off(4); + let mut vec2 = vec.split_off(split); let row = vec.join(","); rows.push(row); - let mut remaining_rows = split_csv_rows(&mut vec2); + let mut remaining_rows = split_csv(&mut vec2,split); rows.append(&mut remaining_rows); } rows @@ -165,7 +166,7 @@ fn out_as_json_object(input_csv:String){ let depth_counter = 0; let mut split_input: Vec<&str> = input_csv.split(',').collect(); - let rows:Vec = split_csv_rows(&mut split_input); + let rows:Vec = split_csv(&mut split_input,4); let mut output:Vec = Vec::new(); output.push(object_open_char); @@ -189,3 +190,43 @@ fn out_as_json_object(input_csv:String){ println!("{}",line) } } +fn out_as_toml_file(input_csv: String) { + // Split input CSV into rows + let mut split_input: Vec<&str> = input_csv.split(',').collect(); + + // Remove any empty elements + split_input.retain(|&x| x.trim() != ""); + + // Define data structures to store todos and headers + let mut todos: HashMap> = HashMap::new(); + + // Parse each row of the CSV + while !split_input.is_empty() { + let path = split_input.remove(0).trim().to_string(); + let file = split_input.remove(0).trim().to_string(); + let line = split_input.remove(0).trim().to_string(); + let comment = split_input.remove(0).trim().to_string(); + let header = format!("{}{}", path, file); + + // Store todo item under the header + todos.entry(header.clone()).or_insert(Vec::new()).push((line.clone(), comment.clone())); + } + + // Write todos to TOML file + let mut toml_output = String::new(); + for (header, todo_list) in todos { + // Write header + toml_output.push_str(&format!("[{}]\n", header)); + + // Write todo items under this header + for (i, (line, comment)) in todo_list.clone().into_iter().enumerate() { + toml_output.push_str(&format!("[[todo]]\n")); + toml_output.push_str(&format!("line = {}\n", line)); + toml_output.push_str(&format!("comment = \"{}\"\n", comment)); + if i < todo_list.len() - 1 { + toml_output.push_str("\n"); // Separate todo items with a newline + } + } + } + println!("{}", toml_output); +}