Skip to content

Commit

Permalink
core: get node parent
Browse files Browse the repository at this point in the history
  • Loading branch information
barsoosayque committed Sep 27, 2024
1 parent 8836310 commit 9509401
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions crates/opensi-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,35 @@ impl Package {
}

/// Package tree node which operates on indices and is easy to copy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(
serde::Deserialize, serde::Serialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
)]
pub enum PackageNode {
Round { index: usize },
Theme { round_index: usize, index: usize },
Question { round_index: usize, theme_index: usize, index: usize },
}

impl PackageNode {
/// Get parent of the node, unless it's a round node..
pub fn get_parent(&self) -> Option<PackageNode> {
match *self {
PackageNode::Round { .. } => None,
PackageNode::Theme { round_index, .. } => {
PackageNode::Round { index: round_index }.into()
},
PackageNode::Question { round_index, theme_index, .. } => {
PackageNode::Theme { round_index, index: theme_index }.into()
},
}
}

/// Get index of the node itself.
pub fn index(&self) -> usize {
match self {
&PackageNode::Round { index }
| &PackageNode::Theme { index, .. }
| &PackageNode::Question { index, .. } => index,
match *self {
PackageNode::Round { index }
| PackageNode::Theme { index, .. }
| PackageNode::Question { index, .. } => index,
}
}
}
Expand Down

0 comments on commit 9509401

Please sign in to comment.