Skip to content

Commit

Permalink
Small refactoring and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
bennekrouf committed Jan 22, 2024
1 parent d0571ff commit 62baf64
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/api/generate_exercise_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?<ranges>")]
pub fn generate_exercise_list_endpoint(
#[get("/exercises?<ranges>")]
pub fn generate_exercises_endpoint(
dbs: &State<Database>,
ranges: Option<String> ,
) -> Json<Vec<Exercise>> {
Expand Down
4 changes: 2 additions & 2 deletions src/api/get_chapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?<ranges>")]
pub fn get_chapters(dbs: &State<Database>, ranges: Option<String>) -> Json<Vec<Chapter>> {
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)),
Expand Down
1 change: 0 additions & 1 deletion src/cors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// cors.rs
use rocket::{Request, Response};
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header;
Expand Down
6 changes: 3 additions & 3 deletions src/domain/exercise/exercises_for_similar.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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<Vec<(u8, u8)>>) -> 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()
.flat_map(|opposites| opposites.iter())
.filter_map(|kalima| similar_db.get(kalima).ok().flatten())
.filter_map(|data| bincode::deserialize::<Similar>(&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();
Expand Down
20 changes: 5 additions & 15 deletions src/domain/exercise/generate_one_exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Alternative> = 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),
Expand All @@ -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,
}),
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/domain/exercise/get_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<(u8, u8)>>) -> Vec<ExerciseOutput> {
let similar_db = &dbs.similar_db;
Expand All @@ -16,7 +16,7 @@ pub fn get_solution(dbs: &Database, ranges: &Option<Vec<(u8, u8)>>) -> Vec<Exerc
let similar: Similar = bincode::deserialize(&value).ok()?;

// Check if any verse of this Similar is in the specified range
if similar.verses.iter().any(|verse| is_in_range(&verse.chapter_no, &ranges)) {
if similar.verses.iter().any(|verse| is_chapter_in_range(&verse.chapter_no, &ranges)) {
// Convert Similar to ExerciseOutput if it passes the range and opposites check
let exercise = create(dbs, &similar, ranges);
Some(exercise)
Expand Down
9 changes: 4 additions & 5 deletions src/domain/similar/similars_by_chapter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use super::similars_by_key;
use log::info;

use crate::models::{VerseOutput, SimilarOutputAdapted};
use crate::models::Database;
use crate::models::{Database, VerseOutput, SimilarOutputAdapted};
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 get(dbs: &Database, chapter_no: u32, chapter_range: &Option<Vec<(u8, u8)>>) -> Vec<SimilarOutputAdapted> {
// println!("Parsed Ranges: {:?}", chapter_range);
Expand All @@ -24,7 +23,7 @@ pub fn get(dbs: &Database, chapter_no: u32, chapter_range: &Option<Vec<(u8, u8)>
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);
Expand All @@ -36,7 +35,7 @@ pub fn get(dbs: &Database, chapter_no: u32, chapter_range: &Option<Vec<(u8, u8)>

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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +54,7 @@ fn rocket() -> Rocket<Build> {
.manage(all_db.clone())
.mount("/", routes![
get_verse,
generate_exercise_list_endpoint,
generate_exercises_endpoint,
get_chapters,
get_labels,
ping,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn is_in_range(chapter_no: &u32, chapter_range: &Option<Vec<(u8, u8)>>) -> bool {
pub fn is_chapter_in_range(chapter_no: &u32, chapter_range: &Option<Vec<(u8, u8)>>) -> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 62baf64

Please sign in to comment.