Remove rust code (#6)
Reviewed-on: #6 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -1,37 +0,0 @@
|
|||||||
name: Rust
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ main, master ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ main, master ]
|
|
||||||
|
|
||||||
env:
|
|
||||||
# Customize the profile here (e.g., release, debug)
|
|
||||||
PROFILE: release
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-24.04
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Install Rust
|
|
||||||
uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
toolchain: 1.88
|
|
||||||
override: true
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: build
|
|
||||||
args: --release # Or --debug, depending on the env.PROFILE
|
|
||||||
|
|
||||||
- name: Run tests
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: test
|
|
||||||
args: --release # Or --debug, depending on the env.PROFILE
|
|
||||||
|
|
||||||
+1
-1
@@ -1 +1 @@
|
|||||||
/target
|
|
||||||
|
|||||||
Generated
-96
@@ -1,96 +0,0 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 4
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "clean_file"
|
|
||||||
version = "0.4.0"
|
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
"serde_json",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "itoa"
|
|
||||||
version = "1.0.15"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "memchr"
|
|
||||||
version = "2.7.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "proc-macro2"
|
|
||||||
version = "1.0.95"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
|
|
||||||
dependencies = [
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "quote"
|
|
||||||
version = "1.0.40"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ryu"
|
|
||||||
version = "1.0.20"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde"
|
|
||||||
version = "1.0.219"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
|
|
||||||
dependencies = [
|
|
||||||
"serde_derive",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde_derive"
|
|
||||||
version = "1.0.219"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde_json"
|
|
||||||
version = "1.0.140"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
|
|
||||||
dependencies = [
|
|
||||||
"itoa",
|
|
||||||
"memchr",
|
|
||||||
"ryu",
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "syn"
|
|
||||||
version = "2.0.104"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-ident"
|
|
||||||
version = "1.0.18"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
|
||||||
-12
@@ -1,12 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "clean_file"
|
|
||||||
description = "Software tool that sanitizes a list of phone numbers"
|
|
||||||
version = "0.4.0"
|
|
||||||
edition = "2024"
|
|
||||||
rust-version = "1.88"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
|
||||||
serde_json = "1.0.140"
|
|
||||||
-39
@@ -1,39 +0,0 @@
|
|||||||
use std::env;
|
|
||||||
|
|
||||||
mod parser;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let args: Vec<String> = env::args().collect();
|
|
||||||
|
|
||||||
if args.len() < 2 {
|
|
||||||
println!("Provide path to file");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let filepath: &String = args.get(1).unwrap();
|
|
||||||
|
|
||||||
println!("Filepath is: {:?}", filepath);
|
|
||||||
|
|
||||||
let myparser = parser::parser::NumberParser {
|
|
||||||
filepath: filepath.clone(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let rawvalues = myparser.filedump();
|
|
||||||
|
|
||||||
let parsed_values = myparser.updatevalues(&rawvalues);
|
|
||||||
|
|
||||||
let newupdated = myparser.removedups(&parsed_values);
|
|
||||||
|
|
||||||
myparser.print_values(&newupdated, true);
|
|
||||||
|
|
||||||
let result = myparser.save_file(parsed_values);
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Ok(o) => {
|
|
||||||
println!("Successfully saved file: {:?}", o);
|
|
||||||
}
|
|
||||||
Err(er) => {
|
|
||||||
println!("Error saving file: {:?}", er);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-212
@@ -1,212 +0,0 @@
|
|||||||
pub mod parser {
|
|
||||||
use std::collections::HashSet;
|
|
||||||
use std::fs::File;
|
|
||||||
use std::io::prelude::*;
|
|
||||||
use std::io::{BufRead, BufReader};
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
#[derive(Clone, Deserialize, Serialize)]
|
|
||||||
pub struct SomeObject {
|
|
||||||
pub value: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct NumberParser {
|
|
||||||
pub filepath: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NumberParser {
|
|
||||||
pub fn filedump(&self) -> Vec<SomeObject> {
|
|
||||||
println!("\nDumping files");
|
|
||||||
let mut objs: Vec<SomeObject> = Vec::new();
|
|
||||||
|
|
||||||
if let Ok(lines) = self.read_lines(&self.filepath) {
|
|
||||||
for line in lines {
|
|
||||||
if let Ok(phonenumber) = line {
|
|
||||||
let contains_letters = phonenumber.chars().any(|c| c.is_alphabetic());
|
|
||||||
|
|
||||||
if contains_letters {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let myobj: SomeObject = SomeObject { value: phonenumber };
|
|
||||||
|
|
||||||
if !myobj.value.is_empty() {
|
|
||||||
objs.push(myobj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return objs;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn updatevalues(&self, vals: &Vec<SomeObject>) -> Vec<SomeObject> {
|
|
||||||
println!("\nUpdating values");
|
|
||||||
let mut updated: Vec<SomeObject> = Vec::new();
|
|
||||||
|
|
||||||
for a in vals {
|
|
||||||
let mut parsed: String = a.value.clone();
|
|
||||||
if a.value.len() < 2 {
|
|
||||||
println!("{} is an invalid number", a.value);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let pars = self.remove_some_data(&parsed);
|
|
||||||
parsed.clear();
|
|
||||||
parsed = pars;
|
|
||||||
|
|
||||||
println!("{}", parsed.clone());
|
|
||||||
|
|
||||||
let updated_parsed = self.add_prefix(parsed);
|
|
||||||
|
|
||||||
let myobj: SomeObject = SomeObject {
|
|
||||||
value: updated_parsed.clone(),
|
|
||||||
};
|
|
||||||
|
|
||||||
updated.push(myobj);
|
|
||||||
}
|
|
||||||
|
|
||||||
return updated;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn removedups(&self, vals: &Vec<SomeObject>) -> Vec<SomeObject> {
|
|
||||||
println!("\nRemoving duplicates");
|
|
||||||
let mut tmp: HashSet<String> = HashSet::new();
|
|
||||||
let mut mm: Vec<SomeObject> = Vec::new();
|
|
||||||
mm.clone_from(vals);
|
|
||||||
mm.retain(|e| tmp.insert(e.value.clone()));
|
|
||||||
|
|
||||||
let mut updated: Vec<SomeObject> = Vec::new();
|
|
||||||
|
|
||||||
for t in tmp {
|
|
||||||
let so: SomeObject = SomeObject { value: t.clone() };
|
|
||||||
|
|
||||||
updated.push(so);
|
|
||||||
}
|
|
||||||
|
|
||||||
return updated;
|
|
||||||
}
|
|
||||||
pub fn print_values(&self, vals: &Vec<SomeObject>, print_total: bool) {
|
|
||||||
println!("\nPrinting values");
|
|
||||||
|
|
||||||
for o in vals {
|
|
||||||
println!("Value: {}", o.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if print_total {
|
|
||||||
println!("\nTotal numbers: {:?}", vals.len());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn save_file(&self, vals: Vec<SomeObject>) -> std::io::Result<()> {
|
|
||||||
println!("\nSaving file");
|
|
||||||
let filename: String = String::from("numbers.json");
|
|
||||||
let mut all_numbers: Vec<String> = Vec::new();
|
|
||||||
|
|
||||||
for val in &vals {
|
|
||||||
all_numbers.push(val.value.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
let json = serde_json::to_string_pretty(&all_numbers).unwrap();
|
|
||||||
let mut file = File::create(filename)?;
|
|
||||||
file.write_all(json.as_bytes())?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_lines(&self, filename: &str) -> std::io::Result<std::io::Lines<BufReader<File>>> {
|
|
||||||
let file = File::open(filename)?;
|
|
||||||
Ok(BufReader::new(file).lines())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn remove_some_data(&self, unparsed: &String) -> String {
|
|
||||||
println!("Unparsed: {}", unparsed);
|
|
||||||
|
|
||||||
let mut parsed: String = String::new();
|
|
||||||
let mychars = unparsed.chars();
|
|
||||||
|
|
||||||
for i in 0..unparsed.len() {
|
|
||||||
let mychar_wrap = mychars.clone().nth(i);
|
|
||||||
|
|
||||||
if let None = mychar_wrap {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mychar = mychar_wrap.unwrap();
|
|
||||||
|
|
||||||
if mychar.is_numeric() {
|
|
||||||
parsed.push(mychar.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("Parsed: {}", parsed);
|
|
||||||
|
|
||||||
return parsed;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_prefix(&self, raw: String) -> String {
|
|
||||||
let mut mychars = raw.chars().clone();
|
|
||||||
let first_char: char = mychars.nth(0).clone().unwrap();
|
|
||||||
let second_char: char = mychars.nth(1).clone().unwrap();
|
|
||||||
|
|
||||||
let mut parsed: String = raw.clone();
|
|
||||||
|
|
||||||
if first_char == '1' {
|
|
||||||
parsed.insert(0, '+');
|
|
||||||
} else if first_char != '+' {
|
|
||||||
if second_char != '1' {
|
|
||||||
parsed.insert(0, '+');
|
|
||||||
parsed.insert(1, '1');
|
|
||||||
} else {
|
|
||||||
parsed.insert(0, '+');
|
|
||||||
parsed.insert(1, '1');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn removing_text() {
|
|
||||||
let sometext: String = String::from("a1234567890");
|
|
||||||
let numberparser = NumberParser {
|
|
||||||
filepath: String::new(),
|
|
||||||
};
|
|
||||||
let removetext = numberparser.remove_some_data(&sometext);
|
|
||||||
let mut result = true;
|
|
||||||
|
|
||||||
for i in 0..removetext.len() {
|
|
||||||
let c = removetext.chars().nth(i).unwrap();
|
|
||||||
|
|
||||||
if !c.is_numeric() {
|
|
||||||
result = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(result, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn adding_prefix() {
|
|
||||||
let sometext: String = String::from("1234567890");
|
|
||||||
let numberparser = NumberParser {
|
|
||||||
filepath: String::new(),
|
|
||||||
};
|
|
||||||
let modified = numberparser.add_prefix(sometext);
|
|
||||||
|
|
||||||
let chars = modified.chars().clone();
|
|
||||||
let firstcharcheck: char = chars.clone().nth(0).unwrap();
|
|
||||||
let secondcharcheck: char = chars.clone().nth(1).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(firstcharcheck, '+');
|
|
||||||
assert_eq!(secondcharcheck, '1');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user