From c6d839d0240e28583011db1009ee9f1af0e011d6 Mon Sep 17 00:00:00 2001 From: Jana Lemke Date: Wed, 24 May 2023 11:11:12 +0200 Subject: [PATCH] Add method to create Course from current course --- src/courses.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/courses.rs b/src/courses.rs index a28c247..0b86321 100644 --- a/src/courses.rs +++ b/src/courses.rs @@ -37,6 +37,30 @@ pub struct Meeting { } impl Course { + pub fn current() -> Self { + Self::from_path(&CONFIG.link_path) + } + + // TODO: make this the single method to read a Course + pub fn from_path(path: impl AsRef + Clone) -> Self { + // TODO: make "course.toml" a *documented* magic value or configurable + let path: PathBuf = path.as_ref().join("course.toml"); + let course_string = std::fs::read_to_string(&path); + if let Ok(course_string) = course_string { + let mut course: Course = toml::from_str(&course_string).unwrap_or_else(|e| { + panic!("Error while parsing course at {:?} \n{}", &path, e); + }); + course.path = path; + course + } else { + panic!( + "Error while reading course at {:?}\n{}", + path, + course_string.unwrap_err() + ); + } + } + /// Get a reference to the course's semesters. pub fn semesters(&self) -> &Vec { &self.semesters