Skip to content

Commit

Permalink
refactor(course_database): add val field to Node
Browse files Browse the repository at this point in the history
  • Loading branch information
GenericConfluent committed Mar 7, 2024
1 parent 14c3937 commit d31b9ef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
51 changes: 33 additions & 18 deletions src/course_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Course> for DatabaseNode {
impl From<Course> for Node {
fn from(value: Course) -> Self {
DatabaseNode::Course(value)
Self {
ntype: NodeType::Course(value),
val: 0,
}
}
}

impl fmt::Display for DatabaseNode {
impl From<NodeType> 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),
}
Expand All @@ -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<DatabaseNode, Relation>,
pub struct CourseGraph {
pub courses: DiGraph<Node, Relation>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -156,7 +171,7 @@ impl TryFrom<&Requirement> for Relation {
}
}

impl CourseDatabase {
impl CourseGraph {
pub fn new(source: &str) -> anyhow::Result<Self> {
// Get list of unique courses sorted
let mut course_list: Vec<Course> = ron::from_str(source)?;
Expand All @@ -168,15 +183,15 @@ impl CourseDatabase {
let mut edge_queue = Vec::<(NodeIndex, CourseId, Relation)>::new();

fn descend_deptree(
courses: &mut DiGraph<DatabaseNode, Relation>,
courses: &mut DiGraph<Node, Relation>,
edge_queue: &mut Vec<(NodeIndex, CourseId, Relation)>,
node: &NodeIndex,
requirement: &Requirement,
) {
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 {
Expand Down Expand Up @@ -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!(),
})
}
Expand Down Expand Up @@ -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);
};
Expand All @@ -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),
};
Expand Down
4 changes: 2 additions & 2 deletions src/graph_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -31,7 +31,7 @@ where
{
style: Theme::Style,
cache: canvas::Cache,
course_graph: Option<CourseDatabase>,
course_graph: Option<CourseGraph>,
}

trait IcedFrom<T> {
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,8 +24,8 @@ mod icons;
pub struct FinescaleApp {
// This should be sorted.
desired_courses: Vec<CourseId>,
required_courses: Option<CourseDatabase>,
course_database: Option<CourseDatabase>,
required_courses: Option<CourseGraph>,
course_database: Option<CourseGraph>,
ui_states: UiStates,
}

Expand All @@ -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<anyhow::Result<CourseDatabase>>),
LoadedCourses(Arc<anyhow::Result<CourseGraph>>),
MainDividerResize(u16),
CourseInputEvent(String),
CourseInputSubmit,
Expand All @@ -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<P: AsRef<std::path::Path>>(path: P) -> Arc<anyhow::Result<CourseDatabase>> {
CourseDatabase::new("[]").into()
async fn load_courses<P: AsRef<std::path::Path>>(path: P) -> Arc<anyhow::Result<CourseGraph>> {
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<CourseDatabase> {
fn select_courses(db: &CourseGraph, desired: &[NodeIndex]) -> anyhow::Result<CourseGraph> {
Err(anyhow!("Course selection still needs implementation"))
}

Expand Down

0 comments on commit d31b9ef

Please sign in to comment.