Rust Build / Check (pull_request) Failing after 26s
Rust Build / Rustfmt (pull_request) Failing after 33s
Rust Build / Test Suite (pull_request) Failing after 55s
Rust Build / Clippy (pull_request) Failing after 41s
Rust Build / build (pull_request) Failing after 51s
187 lines
4.9 KiB
Rust
187 lines
4.9 KiB
Rust
use std::io::{BufRead, BufReader};
|
|
|
|
pub const LETTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct NumberParser {
|
|
pub filepath: String,
|
|
}
|
|
|
|
impl NumberParser {
|
|
pub 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) {
|
|
Ok(lines) => {
|
|
for line in lines {
|
|
let contains_letters = line.chars().any(|c| LETTERS.contains(c));
|
|
if contains_letters {
|
|
continue;
|
|
} else {
|
|
let my_obj = textsender_models::contact::Contact {
|
|
phone_number: line.clone(),
|
|
..Default::default()
|
|
};
|
|
if !my_obj.phone_number.is_empty() {
|
|
objs.push(my_obj);
|
|
}
|
|
}
|
|
}
|
|
|
|
let updated_contacts = self.update_contacts(&objs);
|
|
let removed_duplicates = self.remove_dups(&updated_contacts);
|
|
|
|
Ok(removed_duplicates)
|
|
}
|
|
Err(err) => {
|
|
Err(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn save_file(&self, numbers: Vec<textsender_models::contact::Contact>) {
|
|
}
|
|
|
|
pub fn print_values(&self, numbers: &Vec<textsender_models::contact::Contact>) {
|
|
println!("Printing numbers");
|
|
|
|
for number in numbers {
|
|
println!("Number: {number:?}");
|
|
}
|
|
|
|
println!("Total numbers: {:?}", numbers.len());
|
|
}
|
|
|
|
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);
|
|
reader.lines().collect()
|
|
}
|
|
Err(err) => {
|
|
Err(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
|
|
for val in contacts {
|
|
unique_items.insert(val.phone_number.clone());
|
|
}
|
|
|
|
for unique_item in unique_items {
|
|
let contact = textsender_models::contact::Contact {
|
|
phone_number: unique_item,
|
|
..Default::default()
|
|
};
|
|
unique_contacts.push(contact);
|
|
}
|
|
|
|
unique_contacts
|
|
}
|
|
|
|
fn add_prefix(&self, raw: &String) -> String {
|
|
let mut parsed: String = String::new();
|
|
let first_char = raw.chars().nth(0).unwrap();
|
|
let second_char = raw.chars().nth(1).unwrap();
|
|
|
|
if first_char == '1' {
|
|
parsed = format!("+{raw}");
|
|
} else if first_char != '+' {
|
|
if second_char != '1' {
|
|
parsed = format!("+1{raw}");
|
|
} else {
|
|
parsed = format!("+1{raw}");
|
|
}
|
|
}
|
|
|
|
parsed
|
|
}
|
|
|
|
fn remove_some_data(&self, unparsed: &String) -> String {
|
|
let mut parsed: String = String::new();
|
|
|
|
for (_, ch) in unparsed.chars().enumerate() {
|
|
if ch == ' ' {
|
|
continue;
|
|
} else {
|
|
if ch.is_numeric() {
|
|
parsed.push(ch);
|
|
}
|
|
}
|
|
}
|
|
|
|
println!("Parsed: {parsed:?}");
|
|
|
|
parsed
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
func (prsr *NumberParser) FileDump() []contact.Contact {
|
|
log.Println("Dumping files")
|
|
objs := []contact.Contact{}
|
|
|
|
for _, line := range prsr.readLines(prsr.FilePath) {
|
|
if strings.ContainsAny(line, Letters) {
|
|
continue
|
|
}
|
|
|
|
myObj := contact.Contact{PhoneNumber: line}
|
|
if myObj.PhoneNumber != "" {
|
|
objs = append(objs, myObj)
|
|
}
|
|
}
|
|
|
|
objs = prsr.updateContacts(objs)
|
|
objs = prsr.removeDups(objs)
|
|
|
|
return objs
|
|
}
|
|
|
|
func (prsr *NumberParser) updateContacts(contacts []contact.Contact) []contact.Contact {
|
|
updated := []contact.Contact{}
|
|
|
|
for _, ct := range contacts {
|
|
parsed := ct.PhoneNumber
|
|
if len(parsed) < 2 {
|
|
log.Println("Invalid number:", parsed)
|
|
continue
|
|
}
|
|
|
|
pars := prsr.removeSomeData(parsed)
|
|
parsed = pars
|
|
|
|
log.Println("Parsed:", parsed)
|
|
|
|
updatedParsed := prsr.addPrefix(parsed)
|
|
updated = append(updated, contact.Contact{PhoneNumber: updatedParsed})
|
|
}
|
|
|
|
return updated
|
|
}
|
|
|
|
|
|
func (prsr NumberParser) SaveFile(vals []contact.Contact) {
|
|
filename := "numbers.json"
|
|
|
|
// 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
|
|
}
|
|
}
|
|
*/
|