rename utils to misc rs file + split cours into specific utils directory specialized in utils for specific routes

This commit is contained in:
Mylloon 2024-11-09 16:41:56 +01:00
parent 9dde91f8ed
commit 40cd5bdca5
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
14 changed files with 83 additions and 78 deletions

View file

@ -1,4 +1,4 @@
use crate::{config::Config, utils::utils::get_url, template::InfosPage};
use crate::{config::Config, utils::misc::get_url, template::InfosPage};
use actix_web::{get, http::header::ContentType, routes, web, HttpResponse, Responder};
use cached::proc_macro::once;
use ramhorns::Content;

View file

@ -19,7 +19,7 @@ use crate::{
utils::{
date::Date,
markdown::{get_metadata, get_options, File, FileMetadataBlog, TypeFileMetadata},
utils::{get_url, make_kw, read_file, Html},
misc::{get_url, make_kw, read_file, Html},
},
template::{InfosPage, NavBar},
};

View file

@ -8,7 +8,7 @@ use crate::{
config::Config,
utils::{
markdown::{File, TypeFileMetadata},
utils::{make_kw, read_file, Html},
misc::{make_kw, read_file, Html},
},
template::{InfosPage, NavBar},
};

View file

@ -4,7 +4,7 @@ use crate::{
config::Config,
utils::{
github::{fetch_pr, ProjectState},
utils::{make_kw, Html},
misc::{make_kw, Html},
},
template::{InfosPage, NavBar},
};

View file

@ -1,17 +1,16 @@
use std::path::Path;
use actix_web::{get, web, Responder};
use cached::proc_macro::cached;
use ramhorns::Content;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use crate::{
config::Config,
template::{InfosPage, NavBar},
utils::{
markdown::{File, TypeFileMetadata},
utils::{make_kw, read_file, Html},
routes::cours::get_filetree,
misc::{make_kw, read_file, Html},
},
};
@ -32,13 +31,6 @@ struct CoursTemplate {
content: Option<File>,
}
#[derive(Clone, Debug, Serialize)]
struct FileNode {
name: String,
is_dir: bool,
children: Vec<FileNode>,
}
#[cached]
fn compile_patterns(exclusion_list: Vec<String>) -> Vec<Regex> {
exclusion_list
@ -47,62 +39,6 @@ fn compile_patterns(exclusion_list: Vec<String>) -> Vec<Regex> {
.collect()
}
fn get_filetree(
dir_path: &str,
exclusion_list: &[String],
exclusion_patterns: &[Regex],
) -> FileNode {
let children = std::fs::read_dir(dir_path)
.unwrap()
.filter_map(Result::ok)
.filter_map(|entry| {
let entry_path = entry.path();
let entry_name = entry_path.file_name()?.to_string_lossy().to_string();
// Exclusion checks
if exclusion_list
.iter()
.any(|excluded_term| entry_name.contains(excluded_term.as_str()))
{
return None;
}
if exclusion_patterns.iter().any(|re| re.is_match(&entry_name)) {
return None;
}
if entry_path.is_file() {
Some(FileNode {
name: entry_name,
is_dir: false,
children: vec![],
})
} else {
// Exclude empty directories
let children_of_children = get_filetree(
entry_path.to_str().unwrap(),
exclusion_list,
exclusion_patterns,
);
if children_of_children.is_dir && children_of_children.children.is_empty() {
None
} else {
Some(children_of_children)
}
}
})
.collect();
FileNode {
name: Path::new(dir_path)
.file_name()
.unwrap()
.to_string_lossy()
.to_string(),
is_dir: true,
children,
}
}
/// Get a page content
fn get_content(
cours_dir: &str,

View file

@ -4,11 +4,11 @@ use ramhorns::Content;
use crate::{
config::Config,
template::{InfosPage, NavBar},
utils::{
markdown::{File, TypeFileMetadata},
utils::{make_kw, read_file, Html},
misc::{make_kw, read_file, Html},
},
template::{InfosPage, NavBar},
};
#[get("/")]

View file

@ -4,7 +4,7 @@ use ramhorns::Content;
use crate::{
config::Config,
utils::utils::{get_url, Html},
utils::misc::{get_url, Html},
template::{InfosPage, NavBar},
};

View file

@ -7,7 +7,7 @@ use crate::{
config::Config,
utils::{
markdown::{File, TypeFileMetadata},
utils::{make_kw, read_file, Html},
misc::{make_kw, read_file, Html},
},
template::{InfosPage, NavBar},
};

View file

@ -3,7 +3,7 @@ use cached::proc_macro::once;
use crate::{
config::Config,
utils::utils::{make_kw, Html},
utils::misc::{make_kw, Html},
template::InfosPage,
};

View file

@ -1,7 +1,7 @@
use reqwest::{header::ACCEPT, Error};
use serde::Deserialize;
use crate::utils::utils::get_reqwest_client;
use crate::utils::misc::get_reqwest_client;
#[derive(Debug, Deserialize)]
struct GithubResponse {

View file

@ -1,4 +1,5 @@
pub mod date;
pub mod github;
pub mod markdown;
pub mod utils;
pub mod misc;
pub mod routes;

67
src/utils/routes/cours.rs Normal file
View file

@ -0,0 +1,67 @@
use std::path::Path;
use regex::Regex;
use serde::Serialize;
#[derive(Clone, Debug, Serialize)]
pub struct FileNode {
name: String,
is_dir: bool,
children: Vec<FileNode>,
}
pub fn get_filetree(
dir_path: &str,
exclusion_list: &[String],
exclusion_patterns: &[Regex],
) -> FileNode {
let children = std::fs::read_dir(dir_path)
.unwrap()
.filter_map(Result::ok)
.filter_map(|entry| {
let entry_path = entry.path();
let entry_name = entry_path.file_name()?.to_string_lossy().to_string();
// Exclusion checks
if exclusion_list
.iter()
.any(|excluded_term| entry_name.contains(excluded_term.as_str()))
{
return None;
}
if exclusion_patterns.iter().any(|re| re.is_match(&entry_name)) {
return None;
}
if entry_path.is_file() {
Some(FileNode {
name: entry_name,
is_dir: false,
children: vec![],
})
} else {
// Exclude empty directories
let children_of_children = get_filetree(
entry_path.to_str().unwrap(),
exclusion_list,
exclusion_patterns,
);
if children_of_children.is_dir && children_of_children.children.is_empty() {
None
} else {
Some(children_of_children)
}
}
})
.collect();
FileNode {
name: Path::new(dir_path)
.file_name()
.unwrap()
.to_string_lossy()
.to_string(),
is_dir: true,
children,
}
}

1
src/utils/routes/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod cours;