Migrate #4
+6
-5
@@ -1,6 +1,7 @@
|
||||
pub mod parser;
|
||||
|
||||
fn main() {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
println!("clean_file");
|
||||
|
||||
let args: Vec<String> = 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:?}");
|
||||
|
||||
+17
-40
@@ -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<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");
|
||||
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) => {
|
||||
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<textsender_models::contact::Contact>) {
|
||||
|
||||
// filename := "numbers.json"
|
||||
pub async fn save_file(&self, numbers: Vec<textsender_models::contact::Contact>) {
|
||||
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<textsender_models::contact::Contact>) {
|
||||
println!("Printing numbers");
|
||||
pub fn print_phone_numbers(&self, contacts: &Vec<textsender_models::contact::Contact>) {
|
||||
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<Vec<String>, std::io::Error> {
|
||||
async fn read_lines(&self, filename: &String) -> Result<Vec<String>, 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<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_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();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user