commit cccad0780b6f9978c90076f7a3d5fc41e9e70c26
parent 7c48c27b1cc5b603e3ae77c320107e31bb5b358c
Author: Antoine A <>
Date: Thu, 20 Nov 2025 10:29:27 +0100
clean code
Diffstat:
2 files changed, 28 insertions(+), 34 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "robocop"
version = "1.0.0"
-edition = "2021"
+edition = "2024"
[dependencies]
serde_json = "1.0"
diff --git a/src/main.rs b/src/main.rs
@@ -15,11 +15,11 @@
//
// Copyright (C) 2025 Taler Systems SA
+use serde_json::{Map, Value};
use std::collections::HashMap;
use std::env;
use std::fs;
-use std::io::{self, Write, BufRead, BufReader};
-use serde_json::{Value, Map};
+use std::io::{self, BufRead, BufReader, Write};
use std::process;
const VERSION: &str = "1.0.0";
@@ -44,7 +44,6 @@ fn print_usage() {
eprintln!("Try 'robocop --help' for more information.");
}
-
fn parse_args() -> Option<String> {
let args: Vec<String> = env::args().collect();
@@ -67,7 +66,7 @@ fn parse_args() -> Option<String> {
}
filename => {
// Return the filename to continue processing
- return Some(filename.to_string());
+ Some(filename.to_string())
}
}
}
@@ -79,7 +78,6 @@ fn parse_args() -> Option<String> {
}
}
-
#[derive(Debug, Clone)]
struct Matching {
// Final states with their associated values and costs
@@ -95,7 +93,6 @@ impl Matching {
}
}
-
fn add_string(&mut self, s: &str) {
let chars: Vec<char> = s.chars().collect();
let mut state_ids = Vec::new();
@@ -109,20 +106,16 @@ impl Matching {
// Add final states
for (i, &state_id) in state_ids.iter().enumerate() {
- self.final_states.insert(
- state_id,
- (s.to_string(), chars.len() - i)
- );
+ self.final_states
+ .insert(state_id, (s.to_string(), chars.len() - i));
}
-
}
-
fn find_best_match(&self, input: &str) -> Option<(String, f64)> {
let mut best_match = None;
let mut best_score = 0.0;
- for (_, (candidate, _)) in &self.final_states {
+ for (candidate, _) in self.final_states.values() {
let distance = levenshtein_distance(input, candidate);
let max_len = input.len().max(candidate.len());
let score = if max_len == 0 {
@@ -183,7 +176,8 @@ impl MatchingEngine {
for (idx, item) in json_array.iter().enumerate() {
if let Value::Object(obj) = item {
- let ssid = obj.get("ssid")
+ let ssid = obj
+ .get("ssid")
.and_then(|v| v.as_str())
.unwrap_or(&format!("record_{}", idx))
.to_string();
@@ -228,28 +222,28 @@ impl MatchingEngine {
let total_fields = record.fields.len();
for (key, input_value) in input {
- if let Some(input_str) = input_value.as_str() {
-
- if let Some(fsm) = record.fields.get(key) {
- if let Some((_, score)) = fsm.find_best_match(input_str) {
- total_score += score;
- matching_fields += 1;
- }
- }
+ if let Some(input_str) = input_value.as_str()
+ && let Some(fsm) = record.fields.get(key)
+ && let Some((_, score)) = fsm.find_best_match(input_str)
+ {
+ total_score += score;
+ matching_fields += 1;
}
}
max_fields = max_fields.max(total_fields);
- if total_fields > 0 {
- if total_score > best_overall_score {
- best_overall_score = total_score;
- best_avg_score = total_score / matching_fields as f64;
- best_confidence = matching_fields;
- best_ssid = record.ssid.clone();
- }
+ if total_fields > 0 && total_score > best_overall_score {
+ best_overall_score = total_score;
+ best_avg_score = total_score / matching_fields as f64;
+ best_confidence = matching_fields;
+ best_ssid = record.ssid.clone();
}
}
- (best_avg_score, best_confidence as f64 / max_fields as f64, best_ssid)
+ (
+ best_avg_score,
+ best_confidence as f64 / max_fields as f64,
+ best_ssid,
+ )
}
}
@@ -276,10 +270,10 @@ fn levenshtein_distance(s1: &str, s2: &str) -> usize {
let cost = if chars1[i - 1] == chars2[j - 1] { 0 } else { 1 };
matrix[i][j] = std::cmp::min(
std::cmp::min(
- matrix[i - 1][j] + 1, // deletion
- matrix[i][j - 1] + 1 // insertion
+ matrix[i - 1][j] + 1, // deletion
+ matrix[i][j - 1] + 1, // insertion
),
- matrix[i - 1][j - 1] + cost // substitution
+ matrix[i - 1][j - 1] + cost, // substitution
);
}
}