Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d9142dc13 | ||
|
|
f9339ae9a1
|
||
|
|
a0bf202b39
|
||
|
|
f5cb2ed892 | ||
|
|
cd0ea8a387 | ||
|
|
e7f6ddec9a | ||
|
|
9ec9fa1516 | ||
|
|
f2a650ef27 | ||
|
|
c391939960 | ||
|
|
340c90211f | ||
|
|
e737280a3c | ||
|
|
18d1d2730f | ||
|
|
fc1f3fa814 | ||
|
|
3151405d32 | ||
|
|
154c9f493e | ||
|
|
7e3fff50c1 |
@@ -1,46 +0,0 @@
|
|||||||
name: Go
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ main, go_migr ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ main, go_migr ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-24.04 # You can change this to macos-latest or windows-latest if needed
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '1.24.6' # You can specify a specific version or 'stable'
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: |
|
|
||||||
go build -v -ldflags "\
|
|
||||||
-X main.version=${{ github.ref_name }} \
|
|
||||||
-X main.commit=${{ github.sha }} \
|
|
||||||
-X main.date=$(date +%Y-%m-%dT%H:%M:%S%z)" \
|
|
||||||
-o clean_file
|
|
||||||
|
|
||||||
- name: Test
|
|
||||||
run: go test -v ./...
|
|
||||||
|
|
||||||
- name: Run gofmt (optional)
|
|
||||||
# Uncomment to check code formatting with gofmt
|
|
||||||
run: |
|
|
||||||
if [ -n "$(gofmt -l.)" ]; then
|
|
||||||
echo "Go code is not formatted. Please run 'gofmt -w.' to fix it."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Run golint (optional)
|
|
||||||
# Uncomment to check code style with golint
|
|
||||||
run: |
|
|
||||||
if [ -n "$(golint./...)" ]; then
|
|
||||||
echo "Go code has style issues. Please run 'golint./...' to see them."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
name: Rust Build
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
check:
|
||||||
|
name: Check
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v5
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: 1.95
|
||||||
|
- run: |
|
||||||
|
mkdir -p ~/.ssh
|
||||||
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/deploy_key
|
||||||
|
chmod 600 ~/.ssh/deploy_key
|
||||||
|
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||||
|
eval $(ssh-agent -s)
|
||||||
|
ssh-add -v ~/.ssh/deploy_key
|
||||||
|
cargo check
|
||||||
|
|
||||||
|
test:
|
||||||
|
name: Test Suite
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
needs: setup_ssh
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v5
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: 1.95
|
||||||
|
- run: |
|
||||||
|
mkdir -p ~/.ssh
|
||||||
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/deploy_key
|
||||||
|
chmod 600 ~/.ssh/deploy_key
|
||||||
|
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||||
|
eval $(ssh-agent -s)
|
||||||
|
ssh-add -v ~/.ssh/deploy_key
|
||||||
|
cargo test
|
||||||
|
|
||||||
|
fmt:
|
||||||
|
name: Rustfmt
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
needs: setup_ssh
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v5
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: 1.95
|
||||||
|
- run: rustup component add rustfmt
|
||||||
|
- run: |
|
||||||
|
mkdir -p ~/.ssh
|
||||||
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/deploy_key
|
||||||
|
chmod 600 ~/.ssh/deploy_key
|
||||||
|
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||||
|
eval $(ssh-agent -s)
|
||||||
|
ssh-add -v ~/.ssh/deploy_key
|
||||||
|
cargo fmt --all -- --check
|
||||||
|
|
||||||
|
clippy:
|
||||||
|
name: Clippy
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
needs: setup_ssh
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v5
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: 1.95
|
||||||
|
- run: rustup component add clippy
|
||||||
|
- run: |
|
||||||
|
mkdir -p ~/.ssh
|
||||||
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/deploy_key
|
||||||
|
chmod 600 ~/.ssh/deploy_key
|
||||||
|
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||||
|
eval $(ssh-agent -s)
|
||||||
|
ssh-add -v ~/.ssh/deploy_key
|
||||||
|
cargo clippy -- -D warnings
|
||||||
|
|
||||||
|
build:
|
||||||
|
name: build
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
needs: setup_ssh
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v5
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: 1.95
|
||||||
|
- run: |
|
||||||
|
mkdir -p ~/.ssh
|
||||||
|
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/deploy_key
|
||||||
|
chmod 600 ~/.ssh/deploy_key
|
||||||
|
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||||
|
eval $(ssh-agent -s)
|
||||||
|
ssh-add -v ~/.ssh/deploy_key
|
||||||
|
cargo build --release
|
||||||
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
/clean_file
|
target/
|
||||||
numbers.json
|
numbers.json
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
stages:
|
|
||||||
- build
|
|
||||||
- test
|
|
||||||
|
|
||||||
build:
|
|
||||||
stage: build
|
|
||||||
image: rust:1.85
|
|
||||||
script:
|
|
||||||
- cargo build --release
|
|
||||||
artifacts:
|
|
||||||
paths:
|
|
||||||
- target/release/clean_file
|
|
||||||
expire_in: 1 week
|
|
||||||
cache:
|
|
||||||
key: "cargo-cache"
|
|
||||||
paths:
|
|
||||||
- target/
|
|
||||||
- ~/.cargo/
|
|
||||||
|
|
||||||
test:
|
|
||||||
stage: test
|
|
||||||
image: rust:1.85
|
|
||||||
script:
|
|
||||||
- cargo test
|
|
||||||
cache:
|
|
||||||
key: "cargo-cache"
|
|
||||||
paths:
|
|
||||||
- target/
|
|
||||||
- ~/.cargo/
|
|
||||||
Generated
+1418
File diff suppressed because it is too large
Load Diff
+17
@@ -0,0 +1,17 @@
|
|||||||
|
[package]
|
||||||
|
name = "clean_file"
|
||||||
|
version = "1.0.0"
|
||||||
|
edition = "2024"
|
||||||
|
rust-version = "1.95"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
[dependencies]
|
||||||
|
tokio = { version = "1.52.2", features = ["full"] }
|
||||||
|
futures = { version = "0.3.32" }
|
||||||
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
|
serde_json = { version = "1.0.149" }
|
||||||
|
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender-models.git", tag = "v0.2.5-v0.3.0-migrate-38dbcb09f7-111" }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tempfile = { version = "3.27.0" }
|
||||||
|
async-std = { version = "1.13.2" }
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
CLI software that processes a text file containing US phone numbers
|
|
||||||
and sanitizes the numbers. The result is contained in a json file.
|
|
||||||
|
|
||||||
## Building
|
|
||||||
```
|
|
||||||
go build
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
To include version information
|
|
||||||
```
|
|
||||||
go build -ldflags "-X main.version=3.0.101 -X main.commit=$(git rev-parse HEAD) -X main.date=$(date +%Y-%m-%dT%H:%M:%S%z)" -o clean_file
|
|
||||||
```
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
import "os"
|
|
||||||
import "git.kundeng.us/phoenix/clean_file/parser"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
fmt.Println("clean_file")
|
|
||||||
args := os.Args
|
|
||||||
|
|
||||||
if len(args) < 2 {
|
|
||||||
fmt.Println("Invalid arguments provided")
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
filepath := args[1]
|
|
||||||
|
|
||||||
fmt.Println("File path:", filepath)
|
|
||||||
prsr := parser.NumberParser{FilePath: filepath}
|
|
||||||
|
|
||||||
rawValues := prsr.FileDump()
|
|
||||||
parsedValues := prsr.UpdateValues(rawValues)
|
|
||||||
newUpdated := prsr.RemoveDups(parsedValues)
|
|
||||||
|
|
||||||
prsr.PrintValues(newUpdated, true)
|
|
||||||
|
|
||||||
prsr.SaveFile(newUpdated)
|
|
||||||
}
|
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
package parser
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
import "os"
|
|
||||||
import "unicode"
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"log"
|
|
||||||
)
|
|
||||||
import "encoding/json"
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
const Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
|
|
||||||
type SomeObject struct {
|
|
||||||
Value string `json:"value"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type NumberParser struct {
|
|
||||||
FilePath string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) FileDump() []SomeObject {
|
|
||||||
fmt.Println("Dumping files")
|
|
||||||
objs := []SomeObject{}
|
|
||||||
|
|
||||||
for _, line := range prsr.readLines(prsr.FilePath) {
|
|
||||||
if strings.ContainsAny(line, Letters) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
myObj := SomeObject{Value: line}
|
|
||||||
if myObj.Value != "" {
|
|
||||||
objs = append(objs, myObj)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return objs
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) UpdateValues(vals []SomeObject) []SomeObject {
|
|
||||||
updated := []SomeObject{}
|
|
||||||
|
|
||||||
for _, val := range vals {
|
|
||||||
parsed := val.Value
|
|
||||||
if len(parsed) < 2 {
|
|
||||||
fmt.Println("Invalid number:", parsed)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
pars := prsr.removeSomeData(parsed)
|
|
||||||
parsed = pars
|
|
||||||
|
|
||||||
fmt.Println(parsed)
|
|
||||||
|
|
||||||
updatedParsed := prsr.addPrefix(parsed)
|
|
||||||
updated = append(updated, SomeObject{Value: updatedParsed})
|
|
||||||
}
|
|
||||||
|
|
||||||
return updated
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) RemoveDups(vals []SomeObject) []SomeObject {
|
|
||||||
updated := []SomeObject{}
|
|
||||||
uniqueItems := make(map[string]string)
|
|
||||||
|
|
||||||
for _, val := range vals {
|
|
||||||
uniqueItems[val.Value] = val.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range uniqueItems {
|
|
||||||
updated = append(updated, SomeObject{Value: item})
|
|
||||||
}
|
|
||||||
|
|
||||||
return updated
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) PrintValues(vals []SomeObject, printTotal bool) {
|
|
||||||
fmt.Println("Printing values")
|
|
||||||
|
|
||||||
for _, val := range vals {
|
|
||||||
fmt.Println("Value:", val.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
if printTotal {
|
|
||||||
fmt.Println("Total Numbers:", len(vals))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) SaveFile(vals []SomeObject) {
|
|
||||||
filename := "numbers.json"
|
|
||||||
allNumbers := []string{}
|
|
||||||
|
|
||||||
for _, val := range vals {
|
|
||||||
allNumbers = append(allNumbers, val.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create json and save it to the filesystem
|
|
||||||
jsonData, err := json.MarshalIndent(allNumbers, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error marshaling JSON:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.WriteFile(filename, jsonData, 0644)
|
|
||||||
if err != nil {
|
|
||||||
fmt.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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.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
|
|
||||||
}
|
|
||||||
@@ -1,129 +0,0 @@
|
|||||||
package parser
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
const Test_File_Path = "../tests/numbers.txt"
|
|
||||||
|
|
||||||
// Add three tests
|
|
||||||
// File dump
|
|
||||||
func Test_FileDump(t *testing.T) {
|
|
||||||
parser := NumberParser{FilePath: Test_File_Path}
|
|
||||||
rawValues := parser.FileDump()
|
|
||||||
|
|
||||||
testValues := []SomeObject{
|
|
||||||
{Value: "(217) 555-0194"},
|
|
||||||
{Value: "(602) 555-0177"},
|
|
||||||
{Value: "(713) 555-0167"},
|
|
||||||
{Value: "(608) 555-0190"},
|
|
||||||
{Value: "(305) 555-0112"},
|
|
||||||
{Value: "(415) 555-0135"},
|
|
||||||
{Value: "(608) 555-0190"},
|
|
||||||
{Value: "(602) 555-0177"},
|
|
||||||
{Value: "(317) 555-0123"},
|
|
||||||
{Value: "(360) 555-0158"},
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(rawValues) < 1 {
|
|
||||||
t.Error("Dump should not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !dataEqual(rawValues, testValues) {
|
|
||||||
t.Error("File dump was not correctly dump")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse values
|
|
||||||
func Test_Parsed(t *testing.T) {
|
|
||||||
parser := NumberParser{FilePath: Test_File_Path}
|
|
||||||
rawValues := parser.FileDump()
|
|
||||||
|
|
||||||
if len(rawValues) < 1 {
|
|
||||||
t.Error("Dump should not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
parsed := parser.UpdateValues(rawValues)
|
|
||||||
testValues := []SomeObject{
|
|
||||||
{Value: "+12175550194"},
|
|
||||||
{Value: "+16025550177"},
|
|
||||||
{Value: "+17135550167"},
|
|
||||||
{Value: "+16085550190"},
|
|
||||||
{Value: "+13055550112"},
|
|
||||||
{Value: "+14155550135"},
|
|
||||||
{Value: "+16085550190"},
|
|
||||||
{Value: "+16025550177"},
|
|
||||||
{Value: "+13175550123"},
|
|
||||||
{Value: "+13605550158"},
|
|
||||||
}
|
|
||||||
|
|
||||||
aSize := len(parsed)
|
|
||||||
bSize := len(testValues)
|
|
||||||
if aSize != bSize {
|
|
||||||
t.Error("Data has varrying sizes A", aSize, " B", bSize)
|
|
||||||
}
|
|
||||||
if !dataEqual(parsed, testValues) {
|
|
||||||
t.Error("Data has not be updated")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove duplicates
|
|
||||||
func Test_RemoveDups(t *testing.T) {
|
|
||||||
parser := NumberParser{FilePath: Test_File_Path}
|
|
||||||
rawValues := parser.FileDump()
|
|
||||||
|
|
||||||
if len(rawValues) < 1 {
|
|
||||||
t.Error("Dump should not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
parsed := parser.UpdateValues(rawValues)
|
|
||||||
finalAmount := parser.RemoveDups(parsed)
|
|
||||||
testValues := []SomeObject{
|
|
||||||
{Value: "+12175550194"},
|
|
||||||
{Value: "+16025550177"},
|
|
||||||
{Value: "+17135550167"},
|
|
||||||
{Value: "+16085550190"},
|
|
||||||
{Value: "+13055550112"},
|
|
||||||
{Value: "+14155550135"},
|
|
||||||
{Value: "+13175550123"},
|
|
||||||
{Value: "+13605550158"},
|
|
||||||
}
|
|
||||||
|
|
||||||
aSize := len(finalAmount)
|
|
||||||
bSize := len(testValues)
|
|
||||||
if aSize != bSize {
|
|
||||||
t.Error("Data has varrying sizes A", aSize, " B", bSize)
|
|
||||||
}
|
|
||||||
if !dataEqual(finalAmount, testValues) {
|
|
||||||
fmt.Println("Printing final amount")
|
|
||||||
for _, val := range finalAmount {
|
|
||||||
fmt.Println("Value:", val.Value)
|
|
||||||
}
|
|
||||||
fmt.Println("Printing test values")
|
|
||||||
for _, val := range testValues {
|
|
||||||
fmt.Println("Value:", val.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Error("Data has not be updated")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func dataEqual(a, b []SomeObject) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range a {
|
|
||||||
foundIt := false
|
|
||||||
for j := range b {
|
|
||||||
if a[i] == b[j] {
|
|
||||||
foundIt = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !foundIt {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
pub mod parser;
|
||||||
|
pub mod version;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
if args.len() < 2 {
|
||||||
|
println!("Invalid arguments provided");
|
||||||
|
std::process::exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if version::contains_version(&args) {
|
||||||
|
version::print_version();
|
||||||
|
std::process::exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let filepath = &args[1];
|
||||||
|
println!("Filepath: {filepath:?}");
|
||||||
|
let prsr = parser::NumberParser {
|
||||||
|
filepath: filepath.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
match prsr.file_dump().await {
|
||||||
|
Ok(contacts) => {
|
||||||
|
prsr.print_phone_numbers(&contacts);
|
||||||
|
prsr.save_file(contacts, parser::DEFAULT_OUTPUT_FILE).await;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,267 @@
|
|||||||
|
use std::io::BufRead;
|
||||||
|
|
||||||
|
pub const DEFAULT_OUTPUT_FILE: &str = "numbers.json";
|
||||||
|
const LETTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
pub struct NumberParser {
|
||||||
|
pub filepath: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NumberParser {
|
||||||
|
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).await {
|
||||||
|
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).await;
|
||||||
|
let removed_duplicates = self.remove_dups(&updated_contacts).await;
|
||||||
|
|
||||||
|
Ok(removed_duplicates)
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn save_file(
|
||||||
|
&self,
|
||||||
|
numbers: Vec<textsender_models::contact::Contact>,
|
||||||
|
filename: &str,
|
||||||
|
) {
|
||||||
|
match serde_json::to_string(&numbers) {
|
||||||
|
Ok(json_string) => match std::fs::write(filename, json_string) {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Saved to: {filename:?}");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_phone_numbers(&self, contacts: &Vec<textsender_models::contact::Contact>) {
|
||||||
|
println!("Printing phone numbers");
|
||||||
|
|
||||||
|
for contact in contacts {
|
||||||
|
println!("Phone Number: {:?}", contact.phone_number);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Total phone numbers: {:?}", contacts.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
reader.lines().collect()
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
let phone_number = ct.phone_number.clone();
|
||||||
|
if phone_number.len() < 2 {
|
||||||
|
println!("Invalid number: {phone_number:?}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let pars = self.remove_some_data(&phone_number).await;
|
||||||
|
|
||||||
|
match self.add_prefix(&pars) {
|
||||||
|
Ok(updated_parsed) => {
|
||||||
|
let contact = textsender_models::contact::Contact {
|
||||||
|
phone_number: updated_parsed,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
updated.push(contact);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
std::process::exit(-2);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
updated
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_prefix(&self, raw: &String) -> Result<String, std::io::Error> {
|
||||||
|
if raw.is_empty() {
|
||||||
|
Err(std::io::Error::other("Empty"))
|
||||||
|
} else {
|
||||||
|
let mut parsed: String = String::new();
|
||||||
|
let first_char = raw.chars().nth(0).unwrap();
|
||||||
|
|
||||||
|
if first_char == '1' {
|
||||||
|
parsed = format!("+{raw}");
|
||||||
|
} else if first_char != '+' {
|
||||||
|
parsed = format!("+1{raw}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(parsed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn remove_some_data(&self, unparsed: &str) -> String {
|
||||||
|
let mut parsed: String = String::new();
|
||||||
|
|
||||||
|
for ch in unparsed.chars() {
|
||||||
|
if ch == ' ' {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
if ch.is_numeric() {
|
||||||
|
parsed.push(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use textsender_models::contact::Contact;
|
||||||
|
|
||||||
|
const TEST_NUMBERS_0: &str = "tests/numbers-0.txt";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_file_dump() {
|
||||||
|
let file_exists = {
|
||||||
|
let path = std::path::Path::new(TEST_NUMBERS_0);
|
||||||
|
path.exists()
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(true, file_exists, "File does not exists {TEST_NUMBERS_0:?}");
|
||||||
|
|
||||||
|
let filename = String::from(TEST_NUMBERS_0);
|
||||||
|
let prsr = super::NumberParser { filepath: filename };
|
||||||
|
let original: Vec<Contact> = vec![
|
||||||
|
Contact {
|
||||||
|
phone_number: String::from("+13055550112"),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Contact {
|
||||||
|
phone_number: String::from("+13175550123"),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Contact {
|
||||||
|
phone_number: String::from("+14155550135"),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Contact {
|
||||||
|
phone_number: String::from("+16085550190"),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Contact {
|
||||||
|
phone_number: String::from("+17135550167"),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Contact {
|
||||||
|
phone_number: String::from("+16025550177"),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Contact {
|
||||||
|
phone_number: String::from("+13605550158"),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Contact {
|
||||||
|
phone_number: String::from("+12175550194"),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
match async_std::task::block_on(prsr.file_dump()) {
|
||||||
|
Ok(contacts) => {
|
||||||
|
assert!((contacts.len() > 0), "No contacts has been parsed");
|
||||||
|
|
||||||
|
let phone_numbers_1: std::collections::HashSet<_> =
|
||||||
|
contacts.iter().map(|p| p.phone_number.clone()).collect();
|
||||||
|
let phone_numbers_2: std::collections::HashSet<_> =
|
||||||
|
original.iter().map(|p| p.phone_number.clone()).collect();
|
||||||
|
assert_eq!(phone_numbers_1, phone_numbers_2, "No match");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_save_file() {
|
||||||
|
let prsr = super::NumberParser {
|
||||||
|
filepath: String::from(TEST_NUMBERS_0),
|
||||||
|
};
|
||||||
|
let results = async_std::task::block_on(prsr.file_dump());
|
||||||
|
assert!(results.is_ok(), "File dump not successful");
|
||||||
|
let test_output_file: &str = "test-numbers.json";
|
||||||
|
|
||||||
|
let contacts = results.unwrap();
|
||||||
|
async_std::task::block_on(prsr.save_file(contacts, test_output_file));
|
||||||
|
let file_exists = {
|
||||||
|
let path = std::path::Path::new(test_output_file);
|
||||||
|
path.exists()
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(file_exists, "Contacts were not saved");
|
||||||
|
match std::fs::remove_file(test_output_file) {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("File removed");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
pub fn print_version() {
|
||||||
|
let name = env!("CARGO_PKG_NAME");
|
||||||
|
let version = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
println!("{name:?} {version:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn contains_version(args: &Vec<String>) -> bool {
|
||||||
|
let valid_flags = vec!["-v", "--version"];
|
||||||
|
|
||||||
|
for arg in args {
|
||||||
|
for valid_flag in &valid_flags {
|
||||||
|
if valid_flag == arg {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package version
|
|
||||||
|
|
||||||
var (
|
|
||||||
Version = "dev"
|
|
||||||
Commit = "none"
|
|
||||||
Date = "unknown"
|
|
||||||
)
|
|
||||||
Reference in New Issue
Block a user