-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: organize excel trait impl into smaller files
- Loading branch information
1 parent
d229371
commit 821bf6a
Showing
7 changed files
with
199 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use super::{AsStringParser, Excel, Parser, Retrieve}; | ||
|
||
impl AsStringParser for Excel { | ||
fn get_subject_with_code(&self, val: &str) -> Option<(String, String)> { | ||
let (subject_name, code) = Self::parse_subject_with_code(val)?; | ||
self.lecturer_subjects_session_map | ||
.subjects | ||
.get(&subject_name.to_lowercase()) | ||
.map(|_| (subject_name, code)) | ||
} | ||
|
||
fn get_lecturer(&self, row: u32, col: u32) -> Option<Vec<String>> { | ||
let lecturers_str = self.retrieve_class_detail(row, col)?; | ||
let lecturers = Excel::parse_lecturer(&lecturers_str)?; | ||
let lecturers_code: Vec<String> = lecturers | ||
.into_iter() | ||
.flat_map(|lecture_code| { | ||
let code = match self | ||
.lecturer_subjects_session_map | ||
.lecturers | ||
.contains_key(lecture_code.trim()) | ||
{ | ||
true => lecture_code.trim().to_string(), | ||
false => "UNK".to_string(), | ||
}; | ||
vec![code.to_string()] | ||
}) | ||
.collect(); | ||
|
||
match lecturers_code.is_empty() { | ||
true => None, | ||
false => Some(lecturers_code), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::db::repository::LecturerSubjectSessionMap; | ||
use calamine::Range; | ||
use std::collections::HashMap; | ||
|
||
#[test] | ||
fn test_get_subject_with_code() { | ||
// Create a parser | ||
let mut subject_to_id = HashMap::new(); | ||
subject_to_id.insert( | ||
"jaringan komputer".to_string(), | ||
"c6hhfe7737483833".to_string(), | ||
); | ||
|
||
let excel = Excel { | ||
lecturer_subjects_session_map: LecturerSubjectSessionMap { | ||
subjects: subject_to_id, | ||
lecturers: HashMap::new(), | ||
sessions: HashMap::new(), | ||
}, | ||
range: Range::new((0, 0), (100, 100)), | ||
}; | ||
|
||
// Test the get_subject_with_code method | ||
let result = excel.get_subject_with_code("Jaringan Komputer - C"); | ||
assert_eq!( | ||
result, | ||
Some(("Jaringan Komputer".to_string(), "C".to_string())) | ||
); | ||
|
||
let result = excel.get_subject_with_code("Physics - P101"); | ||
assert_eq!(result, None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use calamine::DataType; | ||
|
||
use crate::{db::repository::class::ClassFindSchedule, DAYS}; | ||
|
||
use super::{Excel, FindClassSchedule, Parser, Retrieve}; | ||
|
||
impl FindClassSchedule for Excel { | ||
fn find_schedule_from_class(&self, subject_name: &str) -> Vec<ClassFindSchedule> { | ||
let mut schedules: Vec<ClassFindSchedule> = Vec::with_capacity(self.range.get_size().1); | ||
for (row_idx, row) in self.range.rows().enumerate() { | ||
for (col_idx, c) in row.iter().enumerate() { | ||
let val = match c.get_string() { | ||
Some(val) => val, | ||
None => continue, | ||
}; | ||
if !val.contains(subject_name) { | ||
continue; | ||
} | ||
|
||
let lecturers_str = match self.retrieve_class_detail(row_idx as u32, col_idx as u32) | ||
{ | ||
Some(lecs) => lecs, | ||
None => continue, | ||
}; | ||
let lecturers = match Excel::parse_lecturer(&lecturers_str) { | ||
Some(lecs) => lecs, | ||
None => continue, | ||
}; | ||
let day = DAYS[row_idx / 14]; | ||
|
||
let session_str = match self.retrieve_session(row_idx as u32) { | ||
Some(session) => session, | ||
None => continue, | ||
}; | ||
let session_name = match Excel::parse_session(&session_str) { | ||
Some(session) => session, | ||
None => continue, | ||
}; | ||
let data = ClassFindSchedule { | ||
class: val.to_string(), | ||
lecturers_code: lecturers, | ||
day: day.to_string(), | ||
session_start: session_name, | ||
}; | ||
schedules.push(data); | ||
} | ||
} | ||
schedules | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.