From b6ae04617dd0e038bdaf5635a0c5d96f9f9b9ee6 Mon Sep 17 00:00:00 2001 From: Bennekrouf Date: Sat, 27 Jan 2024 08:39:23 +0100 Subject: [PATCH] Modules declarations --- src/api/mod.rs | 6 ++ src/domain/chapter/mod.rs | 2 + src/domain/exercise/convert_to_exercise.rs | 47 ---------------- src/domain/exercise/mod.rs | 8 +++ src/domain/mod.rs | 5 ++ src/domain/similar/mod.rs | 4 ++ src/domain/similar/similars_by_key_count.rs | 22 -------- src/domain/verse/mod.rs | 2 + src/files/mod.rs | 2 + src/main.rs | 61 ++------------------- src/utils/mod.rs | 8 +++ 11 files changed, 43 insertions(+), 124 deletions(-) create mode 100644 src/api/mod.rs create mode 100644 src/domain/chapter/mod.rs delete mode 100644 src/domain/exercise/convert_to_exercise.rs create mode 100644 src/domain/exercise/mod.rs create mode 100644 src/domain/mod.rs create mode 100644 src/domain/similar/mod.rs delete mode 100644 src/domain/similar/similars_by_key_count.rs create mode 100644 src/domain/verse/mod.rs create mode 100644 src/files/mod.rs create mode 100644 src/utils/mod.rs diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..09f2818 --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1,6 @@ +pub mod generate_exercise_endpoint; +pub mod get_chapters; +pub mod get_labels; +pub mod verse_by_chapter; +pub mod verse_similar_by_chapter; +pub mod ping; \ No newline at end of file diff --git a/src/domain/chapter/mod.rs b/src/domain/chapter/mod.rs new file mode 100644 index 0000000..278506f --- /dev/null +++ b/src/domain/chapter/mod.rs @@ -0,0 +1,2 @@ +pub mod chapter; +pub mod chapters_init; \ No newline at end of file diff --git a/src/domain/exercise/convert_to_exercise.rs b/src/domain/exercise/convert_to_exercise.rs deleted file mode 100644 index 2993f0f..0000000 --- a/src/domain/exercise/convert_to_exercise.rs +++ /dev/null @@ -1,47 +0,0 @@ -use crate::models::{Similar, ExerciseOutput, Statement, VerseOutput}; -use crate::models::Database; -use crate::domain::similar::sourate_from_verse::sourate_name_from_verse; - -pub fn convert_to_exercise(dbs: &Database, similar: &Similar) -> ExerciseOutput { - let similar_db = &dbs.similar_db; - - let verses_from_similar = similar.verses.iter().map(|verse| create_statement(dbs, verse, &similar.kalima, has_opposites(&similar.opposites))); - - let verses_from_opposites = similar.opposites.iter() - .flat_map(|opposites| opposites.iter()) - .filter_map(|kalima| { - let similar_from_db = get_similar_from_db(similar_db, kalima); - similar_from_db.map(|s| (s.verses.clone(), kalima.clone(), s.opposites.clone())) // Clone the necessary data - }) - .flat_map(|(verses, kalima, opposites)| { - verses.into_iter().map(move |verse| { - create_statement(dbs, &verse, &kalima, has_opposites(&opposites)) - }) - }); - - let all_verses = verses_from_similar.chain(verses_from_opposites).collect(); - - ExerciseOutput { - kalima: similar.kalima.clone(), - verses: all_verses, - } -} - -fn create_statement(dbs: &Database, verse: &VerseOutput, kalima: &str, has_opposites: bool) -> Statement { - let mut modified_verse = verse.clone(); - modified_verse.sourate = Some(sourate_name_from_verse(dbs, verse)); - - Statement { - verse: modified_verse, - kalima: kalima.to_string(), - has_opposites, - } -} - -fn has_opposites(opposites: &Option>) -> bool { - opposites.as_ref().map_or(false, |o| !o.is_empty()) -} - -fn get_similar_from_db(similar_db: &sled::Db, kalima: &str) -> Option { - similar_db.get(kalima).ok().flatten().and_then(|data| bincode::deserialize::(&data).ok()) -} diff --git a/src/domain/exercise/mod.rs b/src/domain/exercise/mod.rs new file mode 100644 index 0000000..1e06bda --- /dev/null +++ b/src/domain/exercise/mod.rs @@ -0,0 +1,8 @@ +pub mod get_exercises; +mod generate_one_exercise; +mod exercise_type_impl; +mod get_solution; +mod extract_and_shuffle_options; +mod select_random_verse_index; +mod sort_exercises; +mod exercises_for_similar; \ No newline at end of file diff --git a/src/domain/mod.rs b/src/domain/mod.rs new file mode 100644 index 0000000..7615baf --- /dev/null +++ b/src/domain/mod.rs @@ -0,0 +1,5 @@ +pub mod chapter; +pub mod similar; +pub mod exercise; +pub mod verse; +pub mod all_db; \ No newline at end of file diff --git a/src/domain/similar/mod.rs b/src/domain/similar/mod.rs new file mode 100644 index 0000000..8b6532e --- /dev/null +++ b/src/domain/similar/mod.rs @@ -0,0 +1,4 @@ +pub mod similars_init; +pub mod similars_by_chapter; +mod similars_by_key; +pub mod sourate_from_verse; \ No newline at end of file diff --git a/src/domain/similar/similars_by_key_count.rs b/src/domain/similar/similars_by_key_count.rs deleted file mode 100644 index 7208111..0000000 --- a/src/domain/similar/similars_by_key_count.rs +++ /dev/null @@ -1,22 +0,0 @@ -use crate::models::{Database, Similar}; - -pub fn get_count_for_kalima(dbs: &Database, kalima: &str) -> usize { - let similar_db = &dbs.similar_db; - - // Use the `find_map` iterator method to find the first (and likely only) - // entry with the given k alima, and then return the count of its verses. - similar_db - .iter() - .find_map(|result| { - let (key, value) = result.ok()?; - let key = std::str::from_utf8(&key).ok()?; - if key == kalima { - // Deserialize the value into Similar - let similar: Similar = bincode::deserialize(&value).ok()?; - Some(similar.verses.len()) - } else { - None - } - }) - .unwrap_or(0) // Return 0 if no match is found -} \ No newline at end of file diff --git a/src/domain/verse/mod.rs b/src/domain/verse/mod.rs new file mode 100644 index 0000000..97fd720 --- /dev/null +++ b/src/domain/verse/mod.rs @@ -0,0 +1,2 @@ +pub mod verses_by_chapter; +pub mod count_verses_in_chapter; \ No newline at end of file diff --git a/src/files/mod.rs b/src/files/mod.rs new file mode 100644 index 0000000..86390ca --- /dev/null +++ b/src/files/mod.rs @@ -0,0 +1,2 @@ +pub mod chapters_from_yaml; +pub mod similars_from_yaml; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 46f0f18..08d42b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,62 +1,13 @@ -mod files { - pub mod chapters_from_yaml; - pub mod similars_from_yaml; -} - -pub mod models; -pub mod api { - pub mod generate_exercise_endpoint; - pub mod get_chapters; - pub mod get_labels; - pub mod verse_by_chapter; - pub mod verse_similar_by_chapter; - pub mod ping; -} -pub mod domain { - pub mod chapter { - pub mod chapter; - pub mod chapters_init; - } - pub mod similar { - pub mod similars_init; - pub mod similars_by_chapter; - pub mod similars_by_key; - pub mod similars_by_key_count; - pub mod sourate_from_verse; - } - pub mod exercise { - pub mod get_exercises; - pub mod generate_one_exercise; - pub mod exercise_type_impl; - pub mod convert_to_exercise; - pub mod get_solution; - pub mod extract_and_shuffle_options; - pub mod select_random_verse_index; - pub mod sort_exercises; - pub mod exercises_for_similar; - } - pub mod verse { - pub mod verses_by_chapter; - pub mod count_verses_in_chapter; - } - pub mod all_db; -} - -mod utils { - pub mod data_folder_path; - pub mod yml_path; - pub mod sort; - pub mod extract_parts; - pub mod insert_in_sled; - pub mod parse_ranges; - pub mod is_chapter_in_range; - pub mod read_labels; -} - +mod files; +mod models; +mod api; +mod domain; +mod utils; mod validator; mod server; mod xhr_guard; mod cors; + #[tokio::main] async fn main() { // Check deserialization of all files before starting the server diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..450eb93 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1,8 @@ +pub mod data_folder_path; +pub mod yml_path; +pub mod sort; +pub mod extract_parts; +pub mod insert_in_sled; +pub mod parse_ranges; +pub mod is_chapter_in_range; +pub mod read_labels; \ No newline at end of file