From 8b2a65d6f67ef86126ce08d2833e01d737aefa21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arma=C3=ABl=20Gu=C3=A9neau?= Date: Sat, 23 Nov 2024 13:10:18 +0100 Subject: [PATCH] cargo fmt --- src/main.rs | 73 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9848d2b..7a04be0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -116,14 +116,28 @@ impl Db { fn from_path(path: &Path, classifier: &Classifier) -> anyhow::Result { let file = File::open(path)?; let (users, is_spam) = serde_json::from_reader(BufReader::new(file))?; - let mut db = Db { users, is_spam, tokens: HashMap::new(), score: HashMap::new() }; + let mut db = Db { + users, + is_spam, + tokens: HashMap::new(), + score: HashMap::new(), + }; db.recompute_tokens(); db.recompute_scores(classifier); Ok(db) } - fn from_users(users: HashMap, is_spam: HashMap, classifier: &Classifier) -> Db { - let mut db = Db { users, is_spam, tokens: HashMap::new(), score: HashMap::new() }; + fn from_users( + users: HashMap, + is_spam: HashMap, + classifier: &Classifier, + ) -> Db { + let mut db = Db { + users, + is_spam, + tokens: HashMap::new(), + score: HashMap::new(), + }; db.recompute_tokens(); db.recompute_scores(classifier); db @@ -131,7 +145,8 @@ impl Db { fn store_to_path(&self, path: &Path) -> anyhow::Result<()> { let file = File::create(path)?; - let dat: (&HashMap, &HashMap) = (&self.users, &self.is_spam); + let dat: (&HashMap, &HashMap) = + (&self.users, &self.is_spam); serde_json::to_writer(BufWriter::new(file), &dat)?; Ok(()) } @@ -329,12 +344,7 @@ async fn load_db() -> anyhow::Result<(Db, Classifier)> { let db: Db = if db_path.is_file() { Db::from_path(db_path, &classifier)? } else { - let db = - Db::from_users( - get_users_data(&forge).await?, - HashMap::new(), - &classifier - ); + let db = Db::from_users(get_users_data(&forge).await?, HashMap::new(), &classifier); db.store_to_path(db_path)?; db }; @@ -389,7 +399,7 @@ struct AppState { #[derive(Debug, Deserialize)] struct SortSetting { - sort: Option + sort: Option, } #[get("/")] @@ -399,8 +409,8 @@ async fn index(data: web::Data, q: web::Query) -> impl Re let db = &data.db.lock().unwrap(); eprintln!("scoring users..."); - let mut users: Vec<_> = - unclassified_users(db).into_iter() + let mut users: Vec<_> = unclassified_users(db) + .into_iter() .map(|(id, u)| (id, u, *db.score.get(id).unwrap())) .collect(); let mut rng = rand::thread_rng(); @@ -410,15 +420,12 @@ async fn index(data: web::Data, q: web::Query) -> impl Re eprintln!("sorting..."); match q.sort.as_ref().map(|s| s.as_str()) { - Some("legit") => - // sort "legit first": by increasing score - users.sort_by_key(|(_, _, score)| (score * 1000.) as u64), - Some("random") => - // keep the random order - (), - _ => - // sort "spam first": by decreasing score - users.sort_by_key(|(_, _, score)| 1000 - (score * 1000.) as u64), + // sort "legit first": by increasing score + Some("legit") => users.sort_by_key(|(_, _, score)| (score * 1000.) as u64), + // keep the random order + Some("random") => (), + // sort "spam first": by decreasing score + _ => users.sort_by_key(|(_, _, score)| 1000 - (score * 1000.) as u64), }; users.truncate(50); @@ -427,7 +434,10 @@ async fn index(data: web::Data, q: web::Query) -> impl Re let mut context = tera::Context::new(); context.insert("users", &users); - context.insert("unclassified_users_count", &(users_count - classified_count)); + context.insert( + "unclassified_users_count", + &(users_count - classified_count), + ); context.insert("total_users_count", &users_count); eprintln!("rendering template..."); let page = TEMPLATES.render("index.html", &context).unwrap(); @@ -436,24 +446,23 @@ async fn index(data: web::Data, q: web::Query) -> impl Re } #[post("/")] -async fn apply( - data: web::Data, - req: web::Form>, -) -> impl Responder { +async fn apply(data: web::Data, req: web::Form>) -> impl Responder { eprintln!("POST /"); let db = &mut data.db.lock().unwrap(); let classifier = &mut data.classifier.lock().unwrap(); - let updates: Vec<(UserId, bool)> = - req.iter() - .map(|(id, classification)| (UserId(*id), classification == "spam")) - .collect(); + let updates: Vec<(UserId, bool)> = req + .iter() + .map(|(id, classification)| (UserId(*id), classification == "spam")) + .collect(); set_spam(db, classifier, &updates); db.store_to_path(Path::new("db.json")).unwrap(); // FIXME - classifier.save(&mut File::create(Path::new("model.json")).unwrap(), false).unwrap(); // FIXME + classifier + .save(&mut File::create(Path::new("model.json")).unwrap(), false) + .unwrap(); // FIXME HttpResponse::SeeOther() .insert_header(("Location", ""))