diff options
| author | Sophie Forrest <git@sophieforrest.com> | 2024-09-12 02:48:04 +1200 |
|---|---|---|
| committer | Sophie Forrest <git@sophieforrest.com> | 2024-09-12 02:48:04 +1200 |
| commit | a77ce3001b619a33f82597a66193307086005d86 (patch) | |
| tree | 3a03f65fd5499a48e9040e53a47430d29b207607 | |
| parent | 6775f0f9bf6932d9651fd74f5c0851eed021de1e (diff) | |
fix(lib): prevent dumping of points into prereqs
Prevents points from being dumped into prerequisites by not dumping them if the list isn't at least two items long.
| -rw-r--r-- | src/lib.rs | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/lib.rs b/src/lib.rs index 9bd2259..8e1261e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,7 +134,9 @@ impl<'a> Course<'a> { .map(|s| s.split(SPLIT_SLICE).map(str::trim).collect::<Vec<&str>>()) .unwrap_or_default(); } - } else { + } else if details_split.len() > 1 { + // Prevent the points from being dumped into requirements if they're the only + // item. self.prerequisites = vec![requirements]; } @@ -145,11 +147,11 @@ impl<'a> Course<'a> { /// Parses the course timetable. /// - /// # Panics + /// # Errors /// - /// Panics if CRN doesn't exist, trimester doesn't exist, slice is made in the middle of - /// a byte, CRN isn't parseable, or trimester isn't parseable. - pub fn parse_timetable(&mut self, elem: ElementRef<'a>) { + /// This function will return an error if nom fails to parse the timetable from the provided + /// data. + pub fn parse_timetable(&mut self, elem: ElementRef<'a>) -> Result<(), NomError> { // Parse timetable / CRNs. let details = elem .first_child() @@ -158,9 +160,10 @@ impl<'a> Course<'a> { if let Some(details) = details { info!("{:#?}", &details); - self.timetable - .push(offering(details).expect("cannot parse course offering").1); + self.timetable.push(offering(details)?.1); } + + Ok(()) } } @@ -299,7 +302,9 @@ pub fn parse_document(document: &Html) -> HashMap<&str, Course<'_>> { working_course.description = description; } else if elem_value.has_class("timetable", CaseSensitivity::CaseSensitive) { - working_course.parse_timetable(elem); + working_course + .parse_timetable(elem) + .expect("could not parse timetable"); } else if elem_value.has_class("coursepoints", CaseSensitivity::CaseSensitive) { working_course.parse_coursepoints(elem); } |