rename utils to misc rs file + split cours into specific utils directory specialized in utils for specific routes
This commit is contained in:
parent
9dde91f8ed
commit
40cd5bdca5
14 changed files with 83 additions and 78 deletions
|
@ -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 actix_web::{get, http::header::ContentType, routes, web, HttpResponse, Responder};
|
||||||
use cached::proc_macro::once;
|
use cached::proc_macro::once;
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
|
|
|
@ -19,7 +19,7 @@ use crate::{
|
||||||
utils::{
|
utils::{
|
||||||
date::Date,
|
date::Date,
|
||||||
markdown::{get_metadata, get_options, File, FileMetadataBlog, TypeFileMetadata},
|
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},
|
template::{InfosPage, NavBar},
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,7 @@ use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
utils::{
|
utils::{
|
||||||
markdown::{File, TypeFileMetadata},
|
markdown::{File, TypeFileMetadata},
|
||||||
utils::{make_kw, read_file, Html},
|
misc::{make_kw, read_file, Html},
|
||||||
},
|
},
|
||||||
template::{InfosPage, NavBar},
|
template::{InfosPage, NavBar},
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
utils::{
|
utils::{
|
||||||
github::{fetch_pr, ProjectState},
|
github::{fetch_pr, ProjectState},
|
||||||
utils::{make_kw, Html},
|
misc::{make_kw, Html},
|
||||||
},
|
},
|
||||||
template::{InfosPage, NavBar},
|
template::{InfosPage, NavBar},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use actix_web::{get, web, Responder};
|
use actix_web::{get, web, Responder};
|
||||||
use cached::proc_macro::cached;
|
use cached::proc_macro::cached;
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
template::{InfosPage, NavBar},
|
template::{InfosPage, NavBar},
|
||||||
utils::{
|
utils::{
|
||||||
markdown::{File, TypeFileMetadata},
|
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>,
|
content: Option<File>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize)]
|
|
||||||
struct FileNode {
|
|
||||||
name: String,
|
|
||||||
is_dir: bool,
|
|
||||||
children: Vec<FileNode>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cached]
|
#[cached]
|
||||||
fn compile_patterns(exclusion_list: Vec<String>) -> Vec<Regex> {
|
fn compile_patterns(exclusion_list: Vec<String>) -> Vec<Regex> {
|
||||||
exclusion_list
|
exclusion_list
|
||||||
|
@ -47,62 +39,6 @@ fn compile_patterns(exclusion_list: Vec<String>) -> Vec<Regex> {
|
||||||
.collect()
|
.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
|
/// Get a page content
|
||||||
fn get_content(
|
fn get_content(
|
||||||
cours_dir: &str,
|
cours_dir: &str,
|
||||||
|
|
|
@ -4,11 +4,11 @@ use ramhorns::Content;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
|
template::{InfosPage, NavBar},
|
||||||
utils::{
|
utils::{
|
||||||
markdown::{File, TypeFileMetadata},
|
markdown::{File, TypeFileMetadata},
|
||||||
utils::{make_kw, read_file, Html},
|
misc::{make_kw, read_file, Html},
|
||||||
},
|
},
|
||||||
template::{InfosPage, NavBar},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
|
|
@ -4,7 +4,7 @@ use ramhorns::Content;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
utils::utils::{get_url, Html},
|
utils::misc::{get_url, Html},
|
||||||
template::{InfosPage, NavBar},
|
template::{InfosPage, NavBar},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
utils::{
|
utils::{
|
||||||
markdown::{File, TypeFileMetadata},
|
markdown::{File, TypeFileMetadata},
|
||||||
utils::{make_kw, read_file, Html},
|
misc::{make_kw, read_file, Html},
|
||||||
},
|
},
|
||||||
template::{InfosPage, NavBar},
|
template::{InfosPage, NavBar},
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,7 @@ use cached::proc_macro::once;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
utils::utils::{make_kw, Html},
|
utils::misc::{make_kw, Html},
|
||||||
template::InfosPage,
|
template::InfosPage,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use reqwest::{header::ACCEPT, Error};
|
use reqwest::{header::ACCEPT, Error};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::utils::utils::get_reqwest_client;
|
use crate::utils::misc::get_reqwest_client;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct GithubResponse {
|
struct GithubResponse {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod date;
|
pub mod date;
|
||||||
pub mod github;
|
pub mod github;
|
||||||
pub mod markdown;
|
pub mod markdown;
|
||||||
pub mod utils;
|
pub mod misc;
|
||||||
|
pub mod routes;
|
||||||
|
|
67
src/utils/routes/cours.rs
Normal file
67
src/utils/routes/cours.rs
Normal 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
1
src/utils/routes/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod cours;
|
Loading…
Reference in a new issue