Refactoring

This commit is contained in:
2026-06-06 12:29:10 -04:00
parent 422b773153
commit ce36feecfe
2 changed files with 23 additions and 45 deletions
+6 -5
View File
@@ -1,6 +1,7 @@
pub mod parser; pub mod parser;
fn main() { #[tokio::main]
async fn main() {
println!("clean_file"); println!("clean_file");
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
@@ -13,10 +14,10 @@ fn main() {
println!("Filepath: {filepath:?}"); println!("Filepath: {filepath:?}");
let prsr = parser::NumberParser{filepath: filepath.clone()}; let prsr = parser::NumberParser{filepath: filepath.clone()};
match prsr.file_dump() { match prsr.file_dump().await {
Ok(numbers) => { Ok(contacts) => {
prsr.print_values(&numbers); prsr.print_phone_numbers(&contacts);
prsr.save_file(numbers); prsr.save_file(contacts).await;
} }
Err(err) => { Err(err) => {
eprintln!("Error: {err:?}"); eprintln!("Error: {err:?}");
+17 -40
View File
@@ -1,6 +1,6 @@
use std::io::{BufRead}; use std::io::{BufRead};
pub const LETTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const LETTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct NumberParser { pub struct NumberParser {
@@ -8,11 +8,11 @@ pub struct NumberParser {
} }
impl NumberParser { impl NumberParser {
pub fn file_dump(&self) -> Result<Vec<textsender_models::contact::Contact>, std::io::Error> { pub async fn file_dump(&self) -> Result<Vec<textsender_models::contact::Contact>, std::io::Error> {
println!("Dumping files"); println!("Dumping files");
let mut objs: Vec<textsender_models::contact::Contact> = Vec::new(); let mut objs: Vec<textsender_models::contact::Contact> = Vec::new();
match self.read_lines(&self.filepath) { match self.read_lines(&self.filepath).await {
Ok(lines) => { Ok(lines) => {
for line in lines { for line in lines {
let contains_letters = line.chars().any(|c| LETTERS.contains(c)); let contains_letters = line.chars().any(|c| LETTERS.contains(c));
@@ -29,8 +29,8 @@ impl NumberParser {
} }
} }
let updated_contacts = self.update_contacts(&objs); let updated_contacts = self.update_contacts(&objs).await;
let removed_duplicates = self.remove_dups(&updated_contacts); let removed_duplicates = self.remove_dups(&updated_contacts).await;
Ok(removed_duplicates) Ok(removed_duplicates)
} }
@@ -40,9 +40,7 @@ impl NumberParser {
} }
} }
pub fn save_file(&self, numbers: Vec<textsender_models::contact::Contact>) { pub async fn save_file(&self, numbers: Vec<textsender_models::contact::Contact>) {
// filename := "numbers.json"
let filename = String::from("numbers.json"); let filename = String::from("numbers.json");
match serde_json::to_string(&numbers) { match serde_json::to_string(&numbers) {
@@ -60,34 +58,19 @@ impl NumberParser {
eprintln!("Error: {err:?}"); 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<textsender_models::contact::Contact>) { pub fn print_phone_numbers(&self, contacts: &Vec<textsender_models::contact::Contact>) {
println!("Printing numbers"); println!("Printing phone numbers");
for number in numbers { for contact in contacts {
println!("Number: {number:?}"); println!("Phone Number: {:?}", contact.phone_number);
} }
println!("Total numbers: {:?}", numbers.len()); println!("Total phone numbers: {:?}", contacts.len());
} }
fn read_lines(&self, filename: &String) -> Result<Vec<String>, std::io::Error> { async fn read_lines(&self, filename: &String) -> Result<Vec<String>, std::io::Error> {
match std::fs::File::open(filename) { match std::fs::File::open(filename) {
Ok(file) => { Ok(file) => {
let reader = std::io::BufReader::new(file); let reader = std::io::BufReader::new(file);
@@ -99,7 +82,7 @@ impl NumberParser {
} }
} }
fn remove_dups(&self, contacts: &Vec<textsender_models::contact::Contact>) -> Vec<textsender_models::contact::Contact> { async fn remove_dups(&self, contacts: &Vec<textsender_models::contact::Contact>) -> Vec<textsender_models::contact::Contact> {
let mut unique_contacts: Vec<textsender_models::contact::Contact> = Vec::new(); let mut unique_contacts: Vec<textsender_models::contact::Contact> = Vec::new();
let mut unique_items = std::collections::HashSet::new(); let mut unique_items = std::collections::HashSet::new();
@@ -119,7 +102,7 @@ impl NumberParser {
} }
fn update_contacts(&self, contacts: &Vec<textsender_models::contact::Contact>) -> Vec<textsender_models::contact::Contact> { async fn update_contacts(&self, contacts: &Vec<textsender_models::contact::Contact>) -> Vec<textsender_models::contact::Contact> {
let mut updated: Vec<textsender_models::contact::Contact> = Vec::new(); let mut updated: Vec<textsender_models::contact::Contact> = Vec::new();
for ct in contacts { for ct in contacts {
@@ -129,15 +112,11 @@ impl NumberParser {
continue; continue;
} }
// pars := prsr.removeSomeData(parsed) let pars = self.remove_some_data(&phone_number).await;
let pars = self.remove_some_data(&phone_number);
// parsed = pars
println!("Parsed: {pars:?}"); // println!("Parsed: {pars:?}");
// updatedParsed := prsr.addPrefix(parsed)
let updated_parsed = self.add_prefix(&pars); let updated_parsed = self.add_prefix(&pars);
// updated = append(updated, contact.Contact{PhoneNumber: updatedParsed})
let contact = textsender_models::contact::Contact { let contact = textsender_models::contact::Contact {
phone_number: updated_parsed, phone_number: updated_parsed,
..Default::default() ..Default::default()
@@ -166,7 +145,7 @@ impl NumberParser {
parsed parsed
} }
fn remove_some_data(&self, unparsed: &String) -> String { async fn remove_some_data(&self, unparsed: &String) -> String {
let mut parsed: String = String::new(); let mut parsed: String = String::new();
for (_, ch) in unparsed.chars().enumerate() { for (_, ch) in unparsed.chars().enumerate() {
@@ -179,8 +158,6 @@ impl NumberParser {
} }
} }
println!("Parsed: {parsed:?}");
parsed parsed
} }
} }