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