From 5542c19ddf4fdab20c3a51825a68af2faf410041 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 22 Aug 2023 20:24:29 -0400 Subject: [PATCH 1/9] Removing c++ files --- .gitmodules | 3 - 3rdparty/vcpkg | 1 - CMakeLists.txt | 27 ----- clean_file.sln | 31 ----- clean_file.vcxproj | 115 ------------------- include/number_parser.hpp | 232 -------------------------------------- src/main.cpp | 41 ------- 7 files changed, 450 deletions(-) delete mode 160000 3rdparty/vcpkg delete mode 100644 CMakeLists.txt delete mode 100644 clean_file.sln delete mode 100644 clean_file.vcxproj delete mode 100644 include/number_parser.hpp delete mode 100644 src/main.cpp diff --git a/.gitmodules b/.gitmodules index 90e3bc2..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "3rdparty/vcpkg"] - path = 3rdparty/vcpkg - url = https://github.com/microsoft/vcpkg diff --git a/3rdparty/vcpkg b/3rdparty/vcpkg deleted file mode 160000 index f14984a..0000000 --- a/3rdparty/vcpkg +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f14984af3738e69f197bf0e647a8dca12de92996 diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 74563ea..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ -cmake_minimum_required(VERSION 3.18.4) - -if(NOT DEFINED VCPKG_ROOT_PATH) - set(VCPKG_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/vcpkg) -endif() - -if(NOT DEFINED CMAKE_TOOLCHAIN_FILE) - set(TOOLCHAIN_PATH ${VCPKG_ROOT_PATH}/scripts/buildsystems/vcpkg.cmake) - - set(CMAKE_TOOLCHAIN_FILE - "${TOOLCHAIN_PATH}" - CACHE STRING "Vcpkg toolchain file") - - message("Using default toolchain file") -endif() - - -project(clean_files CXX) - - -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) - -find_package(nlohmann_json CONFIG REQUIRED) - - -add_executable(clean_files src/main.cpp) -target_link_libraries(clean_files PRIVATE nlohmann_json nlohmann_json::nlohmann_json) diff --git a/clean_file.sln b/clean_file.sln deleted file mode 100644 index d28a507..0000000 --- a/clean_file.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.2.32630.192 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "clean_file", "clean_file.vcxproj", "{27781E3A-B63A-4158-A915-7A4A9DA6D187}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {27781E3A-B63A-4158-A915-7A4A9DA6D187}.Debug|x64.ActiveCfg = Debug|x64 - {27781E3A-B63A-4158-A915-7A4A9DA6D187}.Debug|x64.Build.0 = Debug|x64 - {27781E3A-B63A-4158-A915-7A4A9DA6D187}.Debug|x86.ActiveCfg = Debug|Win32 - {27781E3A-B63A-4158-A915-7A4A9DA6D187}.Debug|x86.Build.0 = Debug|Win32 - {27781E3A-B63A-4158-A915-7A4A9DA6D187}.Release|x64.ActiveCfg = Release|x64 - {27781E3A-B63A-4158-A915-7A4A9DA6D187}.Release|x64.Build.0 = Release|x64 - {27781E3A-B63A-4158-A915-7A4A9DA6D187}.Release|x86.ActiveCfg = Release|Win32 - {27781E3A-B63A-4158-A915-7A4A9DA6D187}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {FD15EEA8-C66D-4B6F-A98D-227633B840D6} - EndGlobalSection -EndGlobal diff --git a/clean_file.vcxproj b/clean_file.vcxproj deleted file mode 100644 index 928ca22..0000000 --- a/clean_file.vcxproj +++ /dev/null @@ -1,115 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 17.0 - {27781E3A-B63A-4158-A915-7A4A9DA6D187} - Win32Proj - - - - Application - true - v143 - - - Application - false - v143 - - - Application - true - v143 - - - Application - false - v143 - - - - - - - - - - - - - - - - - - - - - true - - - true - - - $(ProjectDir)inculde;$(ProjectDir);$(IncludePath) - - - $(ProjectDir)inculde;$(ProjectDir);$(IncludePath) - - - - WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) - MultiThreadedDebugDLL - Level3 - ProgramDatabase - Disabled - - - MachineX86 - true - Windows - - - - - WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) - MultiThreadedDLL - Level3 - ProgramDatabase - - - MachineX86 - true - Windows - true - true - - - - - - - - - - - - \ No newline at end of file diff --git a/include/number_parser.hpp b/include/number_parser.hpp deleted file mode 100644 index 0315e5d..0000000 --- a/include/number_parser.hpp +++ /dev/null @@ -1,232 +0,0 @@ -#ifndef NUMBER_PARSER_H_ -#define NUMBER_PARSER_H_ - -#include -#include -#include -#include -#include -#include - -#include "nlohmann/json.hpp" - - -namespace parser -{ - -class some_object -{ -public: - some_object() = default; - some_object(std::string value) : - value(value) - { - } - - ~some_object() = default; - - std::string value; -private: -}; - - -template -class number_parser -{ -public: - number_parser(const std::string &filepath) : - filepath(filepath) - { } - - template> - Con file_dump() - { - Con container; - - std::fstream file(this->filepath.c_str(), std::ios::in); - - if (!file.is_open()) - { - std::cout << "File is not openned...\n"; - std::cout << "Openning file: " << this->filepath << "\n"; - file.open(filepath.c_str()); - } - - std::string line; - int i = 0; - - while (std::getline(file, line)) - { - if (line.size() > 1) - { - container.push_back(line); - } - } - - file.close(); - - return container; - } - - template> - Con update_values(const Con &values) - { - Con container; - std::vector remove_chars; - remove_chars.reserve(4); - remove_chars.push_back(')'); - remove_chars.push_back('('); - remove_chars.push_back(32); - remove_chars.push_back('-'); - - auto prefix = "+1"; - std::string parsed; - - for (const auto &item : values) - { - parsed.assign(item.value); - - if (parsed.size() < 2) - { - std::cout << parsed << " is an invalid number. Continuing with the next number\n"; - continue; - } - - auto pars = remove_some_data(parsed, remove_chars); - parsed.clear(); - parsed.assign(pars); - - add_prefix(parsed); - - container.push_back(parsed); - parsed.clear(); - } - - return container; - } - - template> - void remove_duplicates(Con &container) - { - std::set tmpContainer; - - auto act = [&](Obj& ob) - { - tmpContainer.emplace(ob.value); - }; - - std::for_each(container.begin(), container.end(), act); - container.clear(); - - auto actReplace = [&](std::string value) - { - Obj o(value); - container.emplace_back(o); - }; - - std::for_each(tmpContainer.begin(), tmpContainer.end(), actReplace); - } - - template> - void save_file(const Con container) - { - std::set my_set; - - const std::string filepath("my_file.json");; - - for (const auto &s : container) - { - my_set.insert(s.value); - } - - - nlohmann::json j(my_set); - - std::cout << "Saving data to file: " << filepath << "\n"; - - std::fstream output(filepath.c_str(), std::ios::out); - output << j.dump(4); - - output.close(); - - std::cout << "The file has been saved\n"; - } - - template> - void print_values(const Con container) - { - if (!(container.size() > 0)) - { - std::cout << "Empty container\n"; - return; - } - - std::cout << "Printing values\n"; - - for (const auto &item : container) - { - std::cout << "Value: " << item.value << "\n"; - } - } -private: - std::string remove_some_data(const std::string &unparsed, const std::vector &remove_chars) - { - std::string parsed; - - std::cout << "Unparsed: " << unparsed << "\n"; - - for (auto i = 0; i < unparsed.size(); ++i) - { - char c = unparsed.at(i); - - if (std::isdigit(c)) - { - parsed.append(std::to_string(c-48)); - } - } - - std::cout << "Parsed: " << parsed << "\n"; - - return parsed; - } - - void add_prefix(std::string &parsed) - { - auto last_char = parsed.at(parsed.size() - 1); - auto first_char = parsed.at(0); - auto second_char = parsed.at(1); - - - if (first_char == '1') - { - parsed.insert(0, "+"); - } - else if (first_char != '+') - { - if (second_char != '1') - { - parsed.insert(0, "+1"); - } - else - { - parsed.insert(0, "+1"); - } - } - } - - int add_prefix_if_not_found(std::string &parsed, const std::string &prefixes) - { - auto prefix_size = prefixes.size(); - auto parsed_beginning = parsed.substr(0, prefix_size); - - return 0; - } - - - std::string filepath; -}; - -} - - -#endif diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index f3edfde..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "number_parser.hpp" - -using std::cout; -using std::string; -using std::vector; -using std::fstream; -using std::stringstream; - - -int main(int argc, char **argv) -{ - cout << "clean_files\n"; - - if (argc < 2) - { - cout << "Provide path to file\n"; - - return -1; - } - - const string filepath(argv[1]); - parser::number_parser file_parser(filepath); - auto raw_values = file_parser.file_dump(); - - auto parsed_values = file_parser.update_values(raw_values); - file_parser.remove_duplicates(parsed_values); - - file_parser.print_values(parsed_values); - file_parser.save_file(parsed_values); - - - return 0; -} From 39629cb58c3c3b74b042685cd9dedec3310e18e4 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 22 Aug 2023 20:27:12 -0400 Subject: [PATCH 2/9] Added rust project Wanted to try converting a simple program I've made in C++ over to rust --- Cargo.toml | 8 ++++++++ src/main.rs | 3 +++ 2 files changed, 11 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..77bc6a3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "clean_file" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From a475c94628b1e9ff626290cb52e1b253c45f8e61 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 22 Aug 2023 20:34:17 -0400 Subject: [PATCH 3/9] Updated gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target From c1484ab49de7e11211fc2392a7ee74b3d4b32928 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 22 Aug 2023 21:20:11 -0400 Subject: [PATCH 4/9] Started to move things over Moved over some objects and function declarations. Migrating the implementation will be the fun part --- src/main.rs | 30 +++++++++++++++++++++++++++++- src/parser.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/parser.rs diff --git a/src/main.rs b/src/main.rs index e7a11a9..41289c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,31 @@ +use std::env; + +mod parser; + fn main() { - println!("Hello, world!"); + let args: Vec = env::args().collect(); + + if args.len() < 2 { + println!("Provide path to file"); + return; + } + + let filepath: &String = args.get(1).unwrap(); + + println!("Filepath is: {:?}", filepath); + + let myparser = parser::parser::NumberParser + { + filepath: filepath.clone() + }; + + myparser.prompt(); + + let rawvalues = myparser.filedump(); + + let parsed_values = myparser.updatevalues(rawvalues); + myparser.removedups(&parsed_values); + + myparser.print_values(&parsed_values); + myparser.save_file(parsed_values); } diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..ed4eed6 --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,39 @@ +pub mod parser { + pub struct SomeObject { + pub value: String + } + + pub struct NumberParser { + pub filepath: String + } + + impl NumberParser { + pub fn prompt(&self) { + println!("Hello!") + } + + pub fn filedump(&self) -> Vec { + let objs: Vec = Vec::new(); + + return objs; + } + + pub fn updatevalues(&self, vals: Vec) -> Vec { + let updated: Vec = Vec::new(); + return updated; + } + + pub fn removedups(&self, vals: &Vec) { + println!("Hi"); + } + pub fn print_values(&self, vals: &Vec) { + for o in vals { + println!("Value: {}", o.value); + } + } + + pub fn save_file(&self, vals: Vec) { + println!("Hi"); + } + } +} From 387c29f5a6bd616c9b29307c075f0f97d9ab49f8 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Wed, 23 Aug 2023 21:23:58 -0400 Subject: [PATCH 5/9] Updated function to retrieve text file content --- src/parser.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index ed4eed6..670ef8d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,8 +1,13 @@ + pub mod parser { + use std::fs::File; + use std::io::{self, BufRead, BufReader}; + pub struct SomeObject { pub value: String } + pub struct NumberParser { pub filepath: String } @@ -13,13 +18,26 @@ pub mod parser { } pub fn filedump(&self) -> Vec { - let objs: Vec = Vec::new(); + let mut objs: Vec = Vec::new(); + + if let Ok(lines) = self.read_lines(&self.filepath) { + for line in lines { + if let Ok(phonenumber) = line { + let myobj: SomeObject = SomeObject { + value: phonenumber + }; + + objs.push(myobj); + } + } + } return objs; } pub fn updatevalues(&self, vals: Vec) -> Vec { - let updated: Vec = Vec::new(); + let mut updated: Vec = vals; + return updated; } @@ -35,5 +53,11 @@ pub mod parser { pub fn save_file(&self, vals: Vec) { println!("Hi"); } + + + fn read_lines(&self, filename: &str) -> std::io::Result>> { + let file = std::fs::File::open(filename)?; + Ok(std::io::BufReader::new(file).lines()) + } } } From 8441a05a9817335071de931e2725b70de8ffce90 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Aug 2023 23:35:33 -0400 Subject: [PATCH 6/9] Functionality implemented --- Cargo.toml | 5 ++ src/main.rs | 16 ++++-- src/parser.rs | 136 +++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 147 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 77bc6a3..14a23db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = { version = "1.0.188", features = ["derive"] } + + +# serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" diff --git a/src/main.rs b/src/main.rs index 41289c6..fa91611 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,11 +21,19 @@ fn main() { myparser.prompt(); + println!("\nDumping files"); let rawvalues = myparser.filedump(); + // myparser.print_values(&rawvalues); - let parsed_values = myparser.updatevalues(rawvalues); - myparser.removedups(&parsed_values); + println!("\nUpdating values"); + let parsed_values = myparser.updatevalues(&rawvalues); - myparser.print_values(&parsed_values); - myparser.save_file(parsed_values); + println!("\nRemoving duplicates"); + let newupdated = myparser.removedups(&parsed_values); + + println!("\nPrinting values"); + myparser.print_values(&newupdated); + + println!("\nSaving file"); + let _result = myparser.save_file(parsed_values); } diff --git a/src/parser.rs b/src/parser.rs index 670ef8d..827ff3b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,7 +2,15 @@ pub mod parser { use std::fs::File; use std::io::{self, BufRead, BufReader}; + use std::collections::HashSet; + // use serde::{Serialize, Deserialize}; + use serde::Serialize; + use serde::Deserialize; + // use std::fs::File; + use std::io::prelude::*; + + #[derive(Clone, Deserialize, Serialize)] pub struct SomeObject { pub value: String } @@ -27,7 +35,11 @@ pub mod parser { value: phonenumber }; - objs.push(myobj); + + if !myobj.value.is_empty() { + // println!("{}", myobj.value); + objs.push(myobj); + } } } } @@ -35,14 +47,73 @@ pub mod parser { return objs; } - pub fn updatevalues(&self, vals: Vec) -> Vec { - let mut updated: Vec = vals; + pub fn updatevalues(&self, vals: &Vec) -> Vec { + let mut updated: Vec = Vec::new(); + + let mut remove_chars: Vec = Vec::new(); + remove_chars.push(')'); + remove_chars.push('('); + remove_chars.push(' '); + remove_chars.push('-'); + + let mut i: i32 = 0; + + // let prefix: String = String::from("+1"); + + for a in vals { + println!("Iter {}", i); + + let mut parsed: String = a.value.clone(); + if a.value.len() < 2 { + println!("{} is an invalid number", a.value); + continue; + } + + let mut pars = self.remove_some_data(&parsed, &remove_chars); + parsed.clear(); + parsed = pars; + + println!("{}", parsed.clone()); + + let updated_parsed = self.add_prefix(parsed); + + let myobj: SomeObject = SomeObject { + value: updated_parsed.clone() + }; + + updated.push(myobj); + i = i + 1; + } + return updated; } - pub fn removedups(&self, vals: &Vec) { + pub fn removedups(&self, mut vals: &Vec) -> Vec { println!("Hi"); + // let mut tmp: Vec = Vec::new(); + let mut tmp: HashSet = HashSet::new(); + //v.retain(|e| set.insert(e.clone())); + let mut mm: Vec = Vec::new(); + mm = vals.clone(); + mm.retain(|e| tmp.insert(e.value.clone())); + + // vals.clear(); + // vals.clear(); + let mut updated: Vec = Vec::new(); + + for t in tmp { + let so: SomeObject = SomeObject { + value: t.clone() + }; + + updated.push(so); + } + + // for item in vals { + // tmp.push(item.value.clone()); + // } + return updated } pub fn print_values(&self, vals: &Vec) { for o in vals { @@ -50,8 +121,13 @@ pub mod parser { } } - pub fn save_file(&self, vals: Vec) { - println!("Hi"); + pub fn save_file(&self, vals: Vec) -> std::io::Result<()> { + let filename: String = String::from("numbers.json"); + + let json = serde_json::to_string(&vals).unwrap(); + let mut file = File::create(filename)?; + file.write_all(json.as_bytes())?; + Ok(()) } @@ -59,5 +135,53 @@ pub mod parser { let file = std::fs::File::open(filename)?; Ok(std::io::BufReader::new(file).lines()) } + + fn remove_some_data(&self, unparsed: &String, remove_chars: &Vec) -> String { + println!("Unparsed: {}", unparsed); + + let mut parsed: String = String::new(); + + for i in 0..unparsed.len() { + let mychar: char = unparsed.chars().nth(i).unwrap(); + + if mychar.is_numeric(){ + parsed.push(mychar); + } + } + + println!("Parsed: {}", parsed); + + return parsed; + } + + fn add_prefix(&self, raw: String) -> String { + let mut mychars = raw.chars().clone(); + println!("add_prefix"); + let first_char: char = mychars.nth(0).clone().unwrap(); + let second_char: char = mychars.nth(1).clone().unwrap(); + println!("add_prefix"); + + let last_char_index = raw.len() - 1; + println!("Size {} last index {}", raw.len(), last_char_index); + + // let last_char: char = mychars.nth(last_char_index).clone().unwrap(); + println!("add_prefix"); + + let mut parsed: String = raw.clone(); + + if first_char == '1' { + parsed.insert(0, '+'); + } else if first_char != '+' { + if second_char != '1' { + parsed.insert(0, '+'); + parsed.insert(1, '1'); + } else { + parsed.insert(0, '+'); + parsed.insert(1, '1'); + } + } + + return parsed + } } } From 109826d1e34ba6297a49ced47f61cdbace2fdb6b Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Aug 2023 23:41:42 -0400 Subject: [PATCH 7/9] Cleanup --- src/main.rs | 2 -- src/parser.rs | 53 ++++++++++++--------------------------------------- 2 files changed, 12 insertions(+), 43 deletions(-) diff --git a/src/main.rs b/src/main.rs index fa91611..fc488cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,8 +19,6 @@ fn main() { filepath: filepath.clone() }; - myparser.prompt(); - println!("\nDumping files"); let rawvalues = myparser.filedump(); // myparser.print_values(&rawvalues); diff --git a/src/parser.rs b/src/parser.rs index 827ff3b..73d1a6d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,15 +1,13 @@ pub mod parser { - use std::fs::File; - use std::io::{self, BufRead, BufReader}; use std::collections::HashSet; - - // use serde::{Serialize, Deserialize}; - use serde::Serialize; - use serde::Deserialize; - // use std::fs::File; + use std::fs::File; + use std::io::{BufRead, BufReader}; use std::io::prelude::*; + use serde::Deserialize; + use serde::Serialize; + #[derive(Clone, Deserialize, Serialize)] pub struct SomeObject { pub value: String @@ -21,10 +19,6 @@ pub mod parser { } impl NumberParser { - pub fn prompt(&self) { - println!("Hello!") - } - pub fn filedump(&self) -> Vec { let mut objs: Vec = Vec::new(); @@ -35,9 +29,7 @@ pub mod parser { value: phonenumber }; - if !myobj.value.is_empty() { - // println!("{}", myobj.value); objs.push(myobj); } } @@ -50,16 +42,8 @@ pub mod parser { pub fn updatevalues(&self, vals: &Vec) -> Vec { let mut updated: Vec = Vec::new(); - let mut remove_chars: Vec = Vec::new(); - remove_chars.push(')'); - remove_chars.push('('); - remove_chars.push(' '); - remove_chars.push('-'); - let mut i: i32 = 0; - // let prefix: String = String::from("+1"); - for a in vals { println!("Iter {}", i); @@ -69,7 +53,7 @@ pub mod parser { continue; } - let mut pars = self.remove_some_data(&parsed, &remove_chars); + let pars = self.remove_some_data(&parsed); parsed.clear(); parsed = pars; @@ -89,17 +73,12 @@ pub mod parser { return updated; } - pub fn removedups(&self, mut vals: &Vec) -> Vec { - println!("Hi"); - // let mut tmp: Vec = Vec::new(); + pub fn removedups(&self, vals: &Vec) -> Vec { let mut tmp: HashSet = HashSet::new(); - //v.retain(|e| set.insert(e.clone())); let mut mm: Vec = Vec::new(); - mm = vals.clone(); + mm.clone_from(vals); mm.retain(|e| tmp.insert(e.value.clone())); - // vals.clear(); - // vals.clear(); let mut updated: Vec = Vec::new(); for t in tmp { @@ -110,9 +89,6 @@ pub mod parser { updated.push(so); } - // for item in vals { - // tmp.push(item.value.clone()); - // } return updated } pub fn print_values(&self, vals: &Vec) { @@ -131,12 +107,12 @@ pub mod parser { } - fn read_lines(&self, filename: &str) -> std::io::Result>> { - let file = std::fs::File::open(filename)?; - Ok(std::io::BufReader::new(file).lines()) + fn read_lines(&self, filename: &str) -> std::io::Result>> { + let file = File::open(filename)?; + Ok(BufReader::new(file).lines()) } - fn remove_some_data(&self, unparsed: &String, remove_chars: &Vec) -> String { + fn remove_some_data(&self, unparsed: &String) -> String { println!("Unparsed: {}", unparsed); let mut parsed: String = String::new(); @@ -156,17 +132,12 @@ pub mod parser { fn add_prefix(&self, raw: String) -> String { let mut mychars = raw.chars().clone(); - println!("add_prefix"); let first_char: char = mychars.nth(0).clone().unwrap(); let second_char: char = mychars.nth(1).clone().unwrap(); - println!("add_prefix"); let last_char_index = raw.len() - 1; println!("Size {} last index {}", raw.len(), last_char_index); - // let last_char: char = mychars.nth(last_char_index).clone().unwrap(); - println!("add_prefix"); - let mut parsed: String = raw.clone(); if first_char == '1' { From 1d788b982384adfb47892e18f1e46d72625aaaec Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Aug 2023 23:43:18 -0400 Subject: [PATCH 8/9] More cleanup --- src/main.rs | 1 - src/parser.rs | 8 -------- 2 files changed, 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index fc488cb..0b1b441 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,6 @@ fn main() { println!("\nDumping files"); let rawvalues = myparser.filedump(); - // myparser.print_values(&rawvalues); println!("\nUpdating values"); let parsed_values = myparser.updatevalues(&rawvalues); diff --git a/src/parser.rs b/src/parser.rs index 73d1a6d..ad4f23d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -42,11 +42,7 @@ pub mod parser { pub fn updatevalues(&self, vals: &Vec) -> Vec { let mut updated: Vec = Vec::new(); - let mut i: i32 = 0; - for a in vals { - println!("Iter {}", i); - let mut parsed: String = a.value.clone(); if a.value.len() < 2 { println!("{} is an invalid number", a.value); @@ -66,7 +62,6 @@ pub mod parser { }; updated.push(myobj); - i = i + 1; } @@ -135,9 +130,6 @@ pub mod parser { let first_char: char = mychars.nth(0).clone().unwrap(); let second_char: char = mychars.nth(1).clone().unwrap(); - let last_char_index = raw.len() - 1; - println!("Size {} last index {}", raw.len(), last_char_index); - let mut parsed: String = raw.clone(); if first_char == '1' { From 7e5edc61240cab40b557a1f58e722e5f2705b36b Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 27 Aug 2023 11:45:26 -0400 Subject: [PATCH 9/9] Added tests --- src/parser.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index ad4f23d..381fb10 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -147,4 +147,44 @@ pub mod parser { return parsed } } + + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + fn removing_text() { + let sometext: String = String::from("a1234567890"); + let numberparser = NumberParser { filepath: String::from("")}; + let removetext = numberparser.remove_some_data(&sometext); + let mut result = true; + + for i in 0..removetext.len() { + let c = removetext.chars().nth(i).unwrap(); + + if !c.is_numeric() { + result = false; + break; + } + } + + assert_eq!(result, true); + } + + #[test] + fn adding_prefix() { + let sometext: String = String::from("1234567890"); + let numberparser = NumberParser { filepath: String::from("")}; + let modified = numberparser.add_prefix(sometext); + + let chars = modified.chars().clone(); + let firstcharcheck: char = chars.clone().nth(0).unwrap(); + let secondcharcheck: char = chars.clone().nth(1).unwrap(); + + assert_eq!(firstcharcheck, '+'); + assert_eq!(secondcharcheck, '1'); + } + } } +