From ce36feecfe7cfc200380012ac30214522663b147 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 6 Jun 2026 12:29:10 -0400 Subject: [PATCH] Refactoring --- src/main.rs | 11 ++++----- src/parser/mod.rs | 57 ++++++++++++++--------------------------------- 2 files changed, 23 insertions(+), 45 deletions(-) diff --git a/src/main.rs b/src/main.rs index a5b23d5..ef78318 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ pub mod parser; -fn main() { +#[tokio::main] +async fn main() { println!("clean_file"); let args: Vec = std::env::args().collect(); @@ -13,10 +14,10 @@ fn main() { println!("Filepath: {filepath:?}"); let prsr = parser::NumberParser{filepath: filepath.clone()}; - match prsr.file_dump() { - Ok(numbers) => { - prsr.print_values(&numbers); - prsr.save_file(numbers); + match prsr.file_dump().await { + Ok(contacts) => { + prsr.print_phone_numbers(&contacts); + prsr.save_file(contacts).await; } Err(err) => { eprintln!("Error: {err:?}"); diff --git a/src/parser/mod.rs b/src/parser/mod.rs index fcbd3c0..4e87029 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,6 +1,6 @@ use std::io::{BufRead}; -pub const LETTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; +const LETTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; #[derive(Clone, Debug, Default)] pub struct NumberParser { @@ -8,11 +8,11 @@ pub struct NumberParser { } impl NumberParser { - pub fn file_dump(&self) -> Result, std::io::Error> { + pub async fn file_dump(&self) -> Result, std::io::Error> { println!("Dumping files"); let mut objs: Vec = Vec::new(); - match self.read_lines(&self.filepath) { + match self.read_lines(&self.filepath).await { Ok(lines) => { for line in lines { let contains_letters = line.chars().any(|c| LETTERS.contains(c)); @@ -29,8 +29,8 @@ impl NumberParser { } } - let updated_contacts = self.update_contacts(&objs); - let removed_duplicates = self.remove_dups(&updated_contacts); + let updated_contacts = self.update_contacts(&objs).await; + let removed_duplicates = self.remove_dups(&updated_contacts).await; Ok(removed_duplicates) } @@ -40,9 +40,7 @@ impl NumberParser { } } - pub fn save_file(&self, numbers: Vec) { - - // filename := "numbers.json" + pub async fn save_file(&self, numbers: Vec) { let filename = String::from("numbers.json"); match serde_json::to_string(&numbers) { @@ -60,34 +58,19 @@ impl NumberParser { eprintln!("Error: {err:?}"); } }; - - // Create json and save it to the filesystem - /* - jsonData, err := json.MarshalIndent(&vals, "", " ") - if err != nil { - log.Println("Error marshaling JSON:", err) - return - } - - err = os.WriteFile(filename, jsonData, 0644) - if err != nil { - log.Println("Error writing file:", err) - return - } - */ } - pub fn print_values(&self, numbers: &Vec) { - println!("Printing numbers"); + pub fn print_phone_numbers(&self, contacts: &Vec) { + println!("Printing phone numbers"); - for number in numbers { - println!("Number: {number:?}"); + for contact in contacts { + println!("Phone Number: {:?}", contact.phone_number); } - println!("Total numbers: {:?}", numbers.len()); + println!("Total phone numbers: {:?}", contacts.len()); } - fn read_lines(&self, filename: &String) -> Result, std::io::Error> { + async fn read_lines(&self, filename: &String) -> Result, std::io::Error> { match std::fs::File::open(filename) { Ok(file) => { let reader = std::io::BufReader::new(file); @@ -99,7 +82,7 @@ impl NumberParser { } } - fn remove_dups(&self, contacts: &Vec) -> Vec { + async fn remove_dups(&self, contacts: &Vec) -> Vec { let mut unique_contacts: Vec = Vec::new(); let mut unique_items = std::collections::HashSet::new(); @@ -119,7 +102,7 @@ impl NumberParser { } - fn update_contacts(&self, contacts: &Vec) -> Vec { + async fn update_contacts(&self, contacts: &Vec) -> Vec { let mut updated: Vec = Vec::new(); for ct in contacts { @@ -129,15 +112,11 @@ impl NumberParser { continue; } - // pars := prsr.removeSomeData(parsed) - let pars = self.remove_some_data(&phone_number); - // parsed = pars + let pars = self.remove_some_data(&phone_number).await; - println!("Parsed: {pars:?}"); + // println!("Parsed: {pars:?}"); - // updatedParsed := prsr.addPrefix(parsed) let updated_parsed = self.add_prefix(&pars); - // updated = append(updated, contact.Contact{PhoneNumber: updatedParsed}) let contact = textsender_models::contact::Contact { phone_number: updated_parsed, ..Default::default() @@ -166,7 +145,7 @@ impl NumberParser { parsed } - fn remove_some_data(&self, unparsed: &String) -> String { + async fn remove_some_data(&self, unparsed: &String) -> String { let mut parsed: String = String::new(); for (_, ch) in unparsed.chars().enumerate() { @@ -179,8 +158,6 @@ impl NumberParser { } } - println!("Parsed: {parsed:?}"); - parsed } }