diff --git a/src/course_database.rs b/src/course_database.rs index 92d4da3..4d84d29 100644 --- a/src/course_database.rs +++ b/src/course_database.rs @@ -94,30 +94,45 @@ impl Course { } #[derive(Debug)] -pub enum DatabaseNode { +pub enum NodeType { Course(Course), Or, } -impl DatabaseNode { +#[derive(Debug)] +pub struct Node { + ntype: NodeType, + val: u16, +} + +impl Node { fn has_id(&self, id: &CourseId) -> bool { - match self { - DatabaseNode::Course(course) => course.id == *id, + match &self.ntype { + NodeType::Course(course) => course.id == *id, _ => false, } } } -impl From for DatabaseNode { +impl From for Node { fn from(value: Course) -> Self { - DatabaseNode::Course(value) + Self { + ntype: NodeType::Course(value), + val: 0, + } } } -impl fmt::Display for DatabaseNode { +impl From for Node { + fn from(ntype: NodeType) -> Self { + Self { ntype, val: 0 } + } +} + +impl fmt::Display for Node { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use DatabaseNode::*; - match self { + use NodeType::*; + match &self.ntype { Or => write!(f, "OR"), Course(c) => write!(f, "{}", c.id), } @@ -127,8 +142,8 @@ impl fmt::Display for DatabaseNode { /// Abstraction over some way to retrieve course info for simplicity. /// There must only be one entry for each course. #[derive(Debug)] -pub struct CourseDatabase { - pub courses: DiGraph, +pub struct CourseGraph { + pub courses: DiGraph, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -156,7 +171,7 @@ impl TryFrom<&Requirement> for Relation { } } -impl CourseDatabase { +impl CourseGraph { pub fn new(source: &str) -> anyhow::Result { // Get list of unique courses sorted let mut course_list: Vec = ron::from_str(source)?; @@ -168,7 +183,7 @@ impl CourseDatabase { let mut edge_queue = Vec::<(NodeIndex, CourseId, Relation)>::new(); fn descend_deptree( - courses: &mut DiGraph, + courses: &mut DiGraph, edge_queue: &mut Vec<(NodeIndex, CourseId, Relation)>, node: &NodeIndex, requirement: &Requirement, @@ -176,7 +191,7 @@ impl CourseDatabase { match requirement { Requirement::And(req_list) | Requirement::Or(req_list) => { let node = if let Requirement::Or(_) = requirement { - let id = courses.add_node(DatabaseNode::Or); + let id = courses.add_node(NodeType::Or.into()); courses.add_edge(*node, id, Relation::Prereq); id } else { @@ -239,8 +254,8 @@ impl CourseDatabase { .courses .node_indices() .find(|node_idx| self.courses[*node_idx].has_id(id))?; - Some(match &self.courses[idx] { - DatabaseNode::Course(course) => idx, + Some(match &self.courses[idx].ntype { + NodeType::Course(course) => idx, _ => unreachable!(), }) } @@ -285,7 +300,7 @@ mod tests { ), ]"#; #[track_caller] - fn assert_in_db(db: &CourseDatabase, id: &CourseId) -> NodeIndex { + fn assert_in_db(db: &CourseGraph, id: &CourseId) -> NodeIndex { let Some(course_idx) = db.index_of(id) else { panic!("{} not in the Database", id); }; @@ -295,7 +310,7 @@ mod tests { #[test] fn cmput_small() { - let db = match CourseDatabase::new(CMPUT_SMALL) { + let db = match CourseGraph::new(CMPUT_SMALL) { Ok(db) => db, Err(err) => panic!("Faild to build CourseDatabase {:?}", err), }; diff --git a/src/graph_widget.rs b/src/graph_widget.rs index 83acb4b..5d6f062 100644 --- a/src/graph_widget.rs +++ b/src/graph_widget.rs @@ -8,7 +8,7 @@ use iced::widget::canvas::{self}; use iced::widget::text::LineHeight; use iced::{Color, Size}; -use crate::course_database::CourseDatabase; +use crate::course_database::CourseGraph; // Think I can stick stuff in this to store widget state. // Probably since I don't get a mut reference to the struct itself. @@ -31,7 +31,7 @@ where { style: Theme::Style, cache: canvas::Cache, - course_graph: Option, + course_graph: Option, } trait IcedFrom { diff --git a/src/lib.rs b/src/lib.rs index dbd6c69..a995f46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ use iced_aw::native::Split; use iced_aw::{modal, split, Card}; mod course_database; -use course_database::{CourseDatabase, CourseId}; +use course_database::{CourseGraph, CourseId}; use icons::Icon; use petgraph::graph::NodeIndex; @@ -24,8 +24,8 @@ mod icons; pub struct FinescaleApp { // This should be sorted. desired_courses: Vec, - required_courses: Option, - course_database: Option, + required_courses: Option, + course_database: Option, ui_states: UiStates, } @@ -40,7 +40,7 @@ struct UiStates { /// put an Arc with a strong refcount != 1 in the variant. #[derive(Debug, Clone)] pub enum Message { - LoadedCourses(Arc>), + LoadedCourses(Arc>), MainDividerResize(u16), CourseInputEvent(String), CourseInputSubmit, @@ -54,13 +54,13 @@ pub enum Message { // NOTE: May make sense to compress all the files into an archive and // download from Github on each startup. -async fn load_courses>(path: P) -> Arc> { - CourseDatabase::new("[]").into() +async fn load_courses>(path: P) -> Arc> { + CourseGraph::new("[]").into() } /// `desired` is guaranteed never to be empty and all the node indices are /// valid. -fn select_courses(db: &CourseDatabase, desired: &[NodeIndex]) -> anyhow::Result { +fn select_courses(db: &CourseGraph, desired: &[NodeIndex]) -> anyhow::Result { Err(anyhow!("Course selection still needs implementation")) }