Add method to create Course from current course
This commit is contained in:
parent
71cfee95bc
commit
759787023f
1 changed files with 24 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue