Saving changes
This commit is contained in:
+17
@@ -1,3 +1,20 @@
|
||||
pub mod parser;
|
||||
|
||||
fn main() {
|
||||
println!("clean_file");
|
||||
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
println!("Invalid arguments provided");
|
||||
std::process::exit(-1);
|
||||
}
|
||||
|
||||
let filepath = &args[1];
|
||||
println!("Filepath: {filepath:?}");
|
||||
let prsr = parser::NumberParser{filepath: *filepath};
|
||||
|
||||
let numbers = prsr.file_dump();
|
||||
prsr.print_values(&numbers);
|
||||
|
||||
prsr.save_file(numbers);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
|
||||
pub const LETTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct NumberParser {
|
||||
pub filepath: String,
|
||||
}
|
||||
|
||||
impl NumberParser {
|
||||
pub fn file_dump(&self) -> Vec<textsender_models::contact::Contact> {
|
||||
println!("Dumping files");
|
||||
let mut objs: Vec<textsender_models::contact::Contact> = Vec::new();
|
||||
|
||||
for line in self.read_lines(&self.filepath) {
|
||||
}
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
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) removeDups(contacts []contact.Contact) []contact.Contact {
|
||||
updated := []contact.Contact{}
|
||||
uniqueItems := make(map[string]string)
|
||||
|
||||
for _, val := range contacts {
|
||||
uniqueItems[val.PhoneNumber] = val.PhoneNumber
|
||||
}
|
||||
|
||||
for _, item := range uniqueItems {
|
||||
updated = append(updated, contact.Contact{PhoneNumber: item})
|
||||
}
|
||||
|
||||
return updated
|
||||
}
|
||||
|
||||
func (prsr NumberParser) PrintValues(contacts []contact.Contact, printTotal bool) {
|
||||
fmt.Println("Printing numbers")
|
||||
|
||||
for _, ct := range contacts {
|
||||
fmt.Println("Number:", ct.PhoneNumber)
|
||||
}
|
||||
|
||||
if printTotal {
|
||||
fmt.Println("Total Numbers:", len(contacts))
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func (prsr NumberParser) readLines(filename string) []string {
|
||||
// Open the file
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Create a scanner to read the file line by line
|
||||
scanner := bufio.NewScanner(file)
|
||||
lines := []string{}
|
||||
|
||||
// Read line by line
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
lines = append(lines, line)
|
||||
}
|
||||
|
||||
// Check for any errors during scanning
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
func (prsr NumberParser) removeSomeData(unparsed string) string {
|
||||
parsed := ""
|
||||
|
||||
for _, ch := range unparsed {
|
||||
if ch == ' ' {
|
||||
continue
|
||||
} else {
|
||||
if unicode.IsDigit(ch) {
|
||||
parsed += string(ch)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("Parsed:", parsed)
|
||||
|
||||
return parsed
|
||||
}
|
||||
|
||||
func (prsr NumberParser) addPrefix(raw string) string {
|
||||
parsed := raw
|
||||
|
||||
firstChar := raw[0]
|
||||
secondChar := raw[1]
|
||||
|
||||
if firstChar == '1' {
|
||||
parsed = "+" + parsed
|
||||
} else if firstChar != '+' {
|
||||
if secondChar != '1' {
|
||||
parsed = "+1" + parsed
|
||||
} else {
|
||||
parsed = "+1" + parsed
|
||||
}
|
||||
}
|
||||
|
||||
return parsed
|
||||
}
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user