cargo fmt

This commit is contained in:
Armaël Guéneau 2024-11-22 16:10:20 +01:00
parent da91785390
commit 5d22662499

View file

@ -1,45 +1,39 @@
use forgejo_api::{Auth, Forgejo}; use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use std::collections::HashMap;
use tokio::time::{sleep, Duration};
use bayespam::classifier::Classifier; use bayespam::classifier::Classifier;
use serde::{Serialize, Deserialize}; use forgejo_api::{Auth, Forgejo};
use std::path::Path; use lazy_static::lazy_static;
use rand::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::{BufReader, BufWriter}; use std::io::{BufReader, BufWriter};
use rand::prelude::*; use std::path::Path;
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, HttpRequest};
use tera::Tera;
use lazy_static::lazy_static;
use std::sync::Mutex; use std::sync::Mutex;
use tera::Tera;
use tokio::time::{sleep, Duration};
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
struct RepoId(i64); struct RepoId(i64);
#[derive(Debug)] #[derive(Debug, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
struct RepoData { struct RepoData {
name: String, name: String,
description: Option<String>, description: Option<String>,
} }
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
struct IssueId(i64); struct IssueId(i64);
#[derive(Debug)] #[derive(Debug, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
struct IssueData { struct IssueData {
title: String, title: String,
body: String, body: String,
} }
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
struct UserId(i64); struct UserId(i64);
#[derive(Debug)] #[derive(Debug, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
struct UserData { struct UserData {
login: String, login: String,
email: String, email: String,
@ -71,12 +65,13 @@ impl UserData {
fn to_text(&self) -> String { fn to_text(&self) -> String {
let mut text = String::new(); let mut text = String::new();
let mut add = |s: &str| { let mut add = |s: &str| {
text += s; text += " " text += s;
text += " "
}; };
for email_part in self.email.split('@') { for email_part in self.email.split('@') {
add(email_part) add(email_part)
}; }
match &self.location { match &self.location {
Some(s) => add(&s), Some(s) => add(&s),
@ -251,7 +246,8 @@ async fn get_users_data(forge: &Forgejo) -> anyhow::Result<HashMap<UserId, UserD
RepoData { RepoData {
name: repo_name, name: repo_name,
description: repo.description, description: repo.description,
})); },
));
} }
eprintln!("Fetching issues..."); eprintln!("Fetching issues...");
@ -269,10 +265,13 @@ async fn get_users_data(forge: &Forgejo) -> anyhow::Result<HashMap<UserId, UserD
continue; continue;
}; };
let Some(forge_user) = data.get_mut(&UserId(user_id)) else { let Some(forge_user) = data.get_mut(&UserId(user_id)) else {
eprintln!("WARN: issue user {} {} for issue {} is not in database", eprintln!(
user.login.unwrap_or_default(), "WARN: issue user {} {} for issue {} is not in database",
user_id, user.login.unwrap_or_default(),
issue.html_url.map_or(String::from(""), |url| url.as_str().to_string()) user_id,
issue
.html_url
.map_or(String::from(""), |url| url.as_str().to_string())
); );
continue; continue;
}; };
@ -281,7 +280,8 @@ async fn get_users_data(forge: &Forgejo) -> anyhow::Result<HashMap<UserId, UserD
IssueData { IssueData {
title: issue.title.unwrap_or_default(), title: issue.title.unwrap_or_default(),
body: issue.body.unwrap_or_default(), body: issue.body.unwrap_or_default(),
})); },
));
} }
Ok(data) Ok(data)
@ -295,12 +295,12 @@ async fn load_db() -> anyhow::Result<(Db, Classifier)> {
Classifier::new() Classifier::new()
}; };
let api_token = let api_token = std::fs::read_to_string(Path::new("api_token"))?
std::fs::read_to_string(Path::new("api_token"))? .trim()
.trim().to_string(); .to_string();
let forge = Forgejo::new( let forge = Forgejo::new(
Auth::Token(&api_token), Auth::Token(&api_token),
url::Url::parse("https://git.deuxfleurs.fr")? url::Url::parse("https://git.deuxfleurs.fr")?,
)?; )?;
let db_path = Path::new("db.json"); let db_path = Path::new("db.json");
@ -324,13 +324,11 @@ async fn load_db() -> anyhow::Result<(Db, Classifier)> {
Ok((db, classifier)) Ok((db, classifier))
} }
fn unclassified_users<'a>(db: &'a Db, classifier: &Classifier) -> fn unclassified_users<'a>(db: &'a Db, classifier: &Classifier) -> Vec<(&'a UserId, &'a UserData)> {
Vec<(&'a UserId, &'a UserData)>
{
db.users db.users
.iter() .iter()
.filter(|(user_id, _)| ! db.classification.contains_key(&user_id)) .filter(|(user_id, _)| !db.classification.contains_key(&user_id))
.collect() .collect()
} }
/* /*
@ -407,8 +405,10 @@ async fn index(data: web::Data<AppState>) -> impl Responder {
eprintln!("compute unclassified users"); eprintln!("compute unclassified users");
let users: Vec<&UserData> = let users: Vec<&UserData> = unclassified_users(db, classifier)
unclassified_users(db, classifier).into_iter().map(|(_id, u)| u).collect(); .into_iter()
.map(|(_id, u)| u)
.collect();
let mut context = tera::Context::new(); let mut context = tera::Context::new();
@ -422,9 +422,14 @@ async fn index(data: web::Data<AppState>) -> impl Responder {
} }
#[post("/")] #[post("/")]
async fn apply(data: web::Data<AppState>, req: web::Form<HashMap<String, String>>) -> impl Responder { async fn apply(
data: web::Data<AppState>,
req: web::Form<HashMap<String, String>>,
) -> impl Responder {
println!("{:#?}", req); println!("{:#?}", req);
HttpResponse::SeeOther().insert_header(("Location", "/")).finish() HttpResponse::SeeOther()
.insert_header(("Location", "/"))
.finish()
} }
#[actix_web::main] #[actix_web::main]