Add method to create Course from current course

This commit is contained in:
Jana Lemke 2023-05-24 11:11:12 +02:00
parent 5449d10a62
commit c6d839d024

View file

@ -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<std::path::Path> + 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<String> {
&self.semesters