From 62baf64e411f27508dcc431fe1b6432f6d1487cc Mon Sep 17 00:00:00 2001 From: Mohamed Date: Mon, 22 Jan 2024 11:10:09 +0100 Subject: [PATCH] Small refactoring and renaming --- src/api/generate_exercise_endpoint.rs | 4 ++-- src/api/get_chapters.rs | 4 ++-- src/cors.rs | 1 - src/domain/exercise/exercises_for_similar.rs | 6 +++--- src/domain/exercise/generate_one_exercise.rs | 20 +++++-------------- src/domain/exercise/get_solution.rs | 4 ++-- src/domain/similar/similars_by_chapter.rs | 9 ++++----- src/main.rs | 4 ++-- src/server.rs | 4 ++-- ...{is_in_range.rs => is_chapter_in_range.rs} | 2 +- ...ault_range_from_json.rs => read_labels.rs} | 2 +- 11 files changed, 24 insertions(+), 36 deletions(-) rename src/utils/{is_in_range.rs => is_chapter_in_range.rs} (70%) rename src/utils/{read_default_range_from_json.rs => read_labels.rs} (92%) diff --git a/src/api/generate_exercise_endpoint.rs b/src/api/generate_exercise_endpoint.rs index d8e8bab..37a0dd8 100644 --- a/src/api/generate_exercise_endpoint.rs +++ b/src/api/generate_exercise_endpoint.rs @@ -5,8 +5,8 @@ use crate::models::{Database, Exercise}; use crate::domain::exercise::get_exercises::get_exercises; use crate::utils::parse_ranges::parse_ranges; -#[get("/exercise_list?")] -pub fn generate_exercise_list_endpoint( +#[get("/exercises?")] +pub fn generate_exercises_endpoint( dbs: &State, ranges: Option , ) -> Json> { diff --git a/src/api/get_chapters.rs b/src/api/get_chapters.rs index 496cec5..7b83801 100644 --- a/src/api/get_chapters.rs +++ b/src/api/get_chapters.rs @@ -6,11 +6,11 @@ use crate::models::Chapter; use crate::models::Database; use crate::domain::similar::similars_by_chapter; use crate::domain::verse::count_verses_in_chapter::count_verses_in_chapter; -use crate::utils::read_default_range_from_json::read_default_range_from_json; +use crate::utils::read_labels::read_labels; #[get("/chapters?")] pub fn get_chapters(dbs: &State, ranges: Option) -> Json> { - let default_range = read_default_range_from_json().unwrap_or((1, 114)); + let default_range = read_labels().unwrap_or((1, 114)); let parsed_ranges = match ranges.as_deref() { Some("undefined") | None => Some(parse_ranges(&format!("{}-{}", default_range.0, default_range.1))), Some(r) => Some(parse_ranges(r)), diff --git a/src/cors.rs b/src/cors.rs index 1160418..dfc713c 100644 --- a/src/cors.rs +++ b/src/cors.rs @@ -1,4 +1,3 @@ -// cors.rs use rocket::{Request, Response}; use rocket::fairing::{Fairing, Info, Kind}; use rocket::http::Header; diff --git a/src/domain/exercise/exercises_for_similar.rs b/src/domain/exercise/exercises_for_similar.rs index 45329b3..17871d0 100644 --- a/src/domain/exercise/exercises_for_similar.rs +++ b/src/domain/exercise/exercises_for_similar.rs @@ -1,12 +1,12 @@ use crate::models::{Database, Similar, VerseOutput, ExerciseOutput, Statement}; use crate::domain::similar::sourate_from_verse::sourate_name_from_verse; -use crate::utils::is_in_range::is_in_range; +use crate::utils::is_chapter_in_range::is_chapter_in_range; pub fn create(dbs: &Database, similar: &Similar, ranges: &Option>) -> ExerciseOutput { let similar_db = &dbs.similar_db; let verses_from_similar = similar.verses.iter() - .filter(|verse| is_in_range(&verse.chapter_no, ranges)) + .filter(|verse| is_chapter_in_range(&verse.chapter_no, ranges)) .map(|verse| create_statement(dbs, verse, &similar.kalima, similar.opposites.as_ref().map_or(false, |o| !o.is_empty()))); let verses_from_opposites = similar.opposites.iter() @@ -14,7 +14,7 @@ pub fn create(dbs: &Database, similar: &Similar, ranges: &Option>) .filter_map(|kalima| similar_db.get(kalima).ok().flatten()) .filter_map(|data| bincode::deserialize::(&data).ok()) .flat_map(|similar| similar.verses.clone()) - .filter(|verse| is_in_range(&verse.chapter_no, ranges)) + .filter(|verse| is_chapter_in_range(&verse.chapter_no, ranges)) .map(|verse| create_statement(dbs, &verse, &similar.kalima, !similar.opposites.as_ref().unwrap_or(&Vec::new()).is_empty())); let all_verses = verses_from_similar.chain(verses_from_opposites).collect(); diff --git a/src/domain/exercise/generate_one_exercise.rs b/src/domain/exercise/generate_one_exercise.rs index acb55b4..2475761 100644 --- a/src/domain/exercise/generate_one_exercise.rs +++ b/src/domain/exercise/generate_one_exercise.rs @@ -19,18 +19,12 @@ pub fn generate_one_exercise(dbs: &Database, exercise: &mut ExerciseOutput, exer // Prepare incorrect alternatives and shuffle them let mut incorrect_alternatives: Vec = extracted_values.into_iter().map(|value| { - // Ensure the correct answer is added first - // let valid_verse = exercise.verses.get(valid_verse_index).unwrap(); - // let mut alternatives = vec![Alternative { verse: Some(valid_verse.verse.clone()) }]; - - // // Add random alternatives - // alternatives.extend(extracted_values.into_iter().map(|value| { match exercise_type { ExerciseType::FindDiscriminant => { Alternative { verse: Some(VerseOutput { - chapter_no: 0, - verse_no: 0, + chapter_no: value.1.chapter_no, + verse_no: value.1.verse_no, sourate: value.1.sourate, ungrouped_text: Some(UngroupedText { discriminant: Some(value.0), @@ -43,9 +37,9 @@ pub fn generate_one_exercise(dbs: &Database, exercise: &mut ExerciseOutput, exer ExerciseType::FindSourate => { Alternative { verse: Some(VerseOutput { - sourate: Some(value.0), chapter_no: value.1.chapter_no, verse_no: value.1.verse_no, + sourate: Some(value.0), ungrouped_text: None, }), } @@ -55,13 +49,9 @@ pub fn generate_one_exercise(dbs: &Database, exercise: &mut ExerciseOutput, exer }).filter(|alt| alt.verse != Some(exercise.verses[valid_verse_index].verse.clone())) .collect(); - incorrect_alternatives.shuffle(&mut rand::thread_rng()); - - // Combine correct and incorrect alternatives, correct answer at the end - incorrect_alternatives.push(correct_alternative); - - // Truncate the alternatives to the desired length (e.g., 4) incorrect_alternatives.truncate(4); + incorrect_alternatives.push(correct_alternative); + incorrect_alternatives.shuffle(&mut rand::thread_rng()); // Use the combined and truncated list as the final alternatives let alternatives = incorrect_alternatives; diff --git a/src/domain/exercise/get_solution.rs b/src/domain/exercise/get_solution.rs index d661652..2efbeac 100644 --- a/src/domain/exercise/get_solution.rs +++ b/src/domain/exercise/get_solution.rs @@ -3,7 +3,7 @@ use crate::models::Database; use crate::domain::exercise::sort_exercises::sort_exercises; use crate::domain::exercise::exercises_for_similar::create; -use crate::utils::is_in_range::is_in_range; +use crate::utils::is_chapter_in_range::is_chapter_in_range; pub fn get_solution(dbs: &Database, ranges: &Option>) -> Vec { let similar_db = &dbs.similar_db; @@ -16,7 +16,7 @@ pub fn get_solution(dbs: &Database, ranges: &Option>) -> Vec>) -> Vec { // println!("Parsed Ranges: {:?}", chapter_range); @@ -24,7 +23,7 @@ pub fn get(dbs: &Database, chapter_no: u32, chapter_range: &Option let kalima = similar[0].kalima.clone(); for mut verse_output in similar[0].verses.iter().cloned() { - if is_in_range(&verse_output.chapter_no, &chapter_range) { + if is_chapter_in_range(&verse_output.chapter_no, &chapter_range) { verse_output.sourate = Some(sourate_name_from_verse(dbs, &verse_output)); if verse_output.chapter_no == chapter_no { verses.push(verse_output); @@ -36,7 +35,7 @@ pub fn get(dbs: &Database, chapter_no: u32, chapter_range: &Option if let Some(opposite_verses) = &similar[0].opposites { for mut verse_output in opposite_verses.iter().cloned() { - if is_in_range(&verse_output.chapter_no, &chapter_range) { + if is_chapter_in_range(&verse_output.chapter_no, &chapter_range) { verse_output.sourate = Some(sourate_name_from_verse(dbs, &verse_output)); opposites.push(verse_output); } diff --git a/src/main.rs b/src/main.rs index d6331dc..46f0f18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,8 +49,8 @@ mod utils { pub mod extract_parts; pub mod insert_in_sled; pub mod parse_ranges; - pub mod is_in_range; - pub mod read_default_range_from_json; + pub mod is_chapter_in_range; + pub mod read_labels; } mod validator; diff --git a/src/server.rs b/src/server.rs index bdc2ab4..2ac9660 100644 --- a/src/server.rs +++ b/src/server.rs @@ -12,7 +12,7 @@ use crate::domain::all_db; use crate::api::verse_by_chapter::get_verse; use crate::api::get_chapters::get_chapters; use crate::api::get_labels::get_labels; -use crate::api::generate_exercise_endpoint::generate_exercise_list_endpoint; +use crate::api::generate_exercise_endpoint::generate_exercises_endpoint; use crate::api::verse_similar_by_chapter::get_verse_similar_by_chapter_route; use crate::cors::CORS; @@ -54,7 +54,7 @@ fn rocket() -> Rocket { .manage(all_db.clone()) .mount("/", routes![ get_verse, - generate_exercise_list_endpoint, + generate_exercises_endpoint, get_chapters, get_labels, ping, diff --git a/src/utils/is_in_range.rs b/src/utils/is_chapter_in_range.rs similarity index 70% rename from src/utils/is_in_range.rs rename to src/utils/is_chapter_in_range.rs index b79f354..077403c 100644 --- a/src/utils/is_in_range.rs +++ b/src/utils/is_chapter_in_range.rs @@ -1,4 +1,4 @@ -pub fn is_in_range(chapter_no: &u32, chapter_range: &Option>) -> bool { +pub fn is_chapter_in_range(chapter_no: &u32, chapter_range: &Option>) -> bool { match chapter_range { Some(ranges) => ranges.iter().any(|&(start, end)| *chapter_no >= start as u32 && *chapter_no <= end as u32), None => true, // If no range is specified, all chapters are considered diff --git a/src/utils/read_default_range_from_json.rs b/src/utils/read_labels.rs similarity index 92% rename from src/utils/read_default_range_from_json.rs rename to src/utils/read_labels.rs index 661075f..25c70f4 100644 --- a/src/utils/read_default_range_from_json.rs +++ b/src/utils/read_labels.rs @@ -9,7 +9,7 @@ pub struct RangeEntry { pub section: String, } -pub fn read_default_range_from_json() -> Option<(u8, u8)> { +pub fn read_labels() -> Option<(u8, u8)> { let file_path = "static/labels.json"; match fs::read_to_string(file_path) { Ok(contents) => {