From a77ce3001b619a33f82597a66193307086005d86 Mon Sep 17 00:00:00 2001 From: Sophie Forrest Date: Thu, 12 Sep 2024 02:48:04 +1200 Subject: 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. --- src/lib.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/lib.rs') 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::>()) .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); } -- cgit 1.4.1