Skip to content

Commit

Permalink
Improve on documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
clementwanjau committed Jun 7, 2024
1 parent d0021f0 commit 58d16b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ pub mod prelude {
tree::{NodeRemovalStrategy, SubTree, TraversalStrategy, Tree},
};

pub type Result<T> = std::result::Result<T, crate::error::Error>;
/// The error type for this crate.
pub type Result<T> = std::result::Result<T, crate::error::Error>;
}
14 changes: 13 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ impl<Q, T> PartialEq for Node<Q, T>
Q: PartialEq + Eq + Clone,
T: PartialEq + Eq + Clone,
{
/// Compare two nodes for equality.
fn eq(&self, other: &Self) -> bool {
self.get_node_id() == other.get_node_id() && self.get_value() == other.get_value()
}
Expand All @@ -348,6 +349,7 @@ impl<Q, T> Display for Node<Q, T>
Q: PartialEq + Eq + Clone + Display,
T: PartialEq + Eq + Clone + Display + Default,
{
/// Display the node.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
Expand All @@ -363,6 +365,7 @@ impl<Q, T> Hash for Node<Q, T>
Q: PartialEq + Eq + Clone + Hash,
T: PartialEq + Eq + Clone + Hash,
{
/// Hash the node.
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.get_node_id().hash(state);
self.get_value().hash(state);
Expand All @@ -377,6 +380,7 @@ impl<Q, T> Serialize for Node<Q, T>
Q: PartialEq + Eq + Clone + Serialize,
T: PartialEq + Eq + Clone + Serialize,
{
/// Serialize the node.
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand All @@ -396,19 +400,21 @@ impl<'de, Q, T> Deserialize<'de> for Node<Q, T>
Q: PartialEq + Eq + Clone + Deserialize<'de>,
T: PartialEq + Eq + Clone + Deserialize<'de>,
{
/// Deserialize the node.
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let node: _Node<Q, T> = Deserialize::deserialize(deserializer)?;

#[cfg(not(feature = "async"))]
return Ok(crate::node::Node(Rc::new(RefCell::new(node))));
return Ok(Node(Rc::new(RefCell::new(node))));
#[cfg(feature = "async")]
return Ok(Node(Arc::new(RefCell::new(node))));
}
}

/// An internal node in a tree.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct _Node<Q, T>
Expand Down Expand Up @@ -731,6 +737,7 @@ impl<Q, T> AsRef<Nodes<Q, T>> for Nodes<Q, T> where
Q: PartialEq + Eq + Clone,
T: PartialEq + Eq + Clone
{
/// Get a reference to the nodes list.
fn as_ref(&self) -> &Nodes<Q, T> {
self
}
Expand All @@ -740,6 +747,7 @@ impl<Q, T> FromIterator<Node<Q, T>> for Nodes<Q, T> where
Q: PartialEq + Eq + Clone,
T: PartialEq + Eq + Clone
{
/// Create a nodes list from an iterator.
fn from_iter<I: IntoIterator<Item=Node<Q, T>>>(iter: I) -> Self {
Nodes(iter.into_iter().collect())
}
Expand All @@ -751,11 +759,13 @@ impl<Q, T> Iterator for Nodes<Q, T> where
{
type Item = Node<Q, T>;

/// Get the next node in the nodes list.
#[allow(clippy::iter_next_slice)]
fn next(&mut self) -> Option<Self::Item> {
self.0.iter().next().cloned()
}

/// Get the size hint of the nodes list.
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.iter().size_hint()
}
Expand All @@ -765,12 +775,14 @@ impl<Q, T> Default for Nodes<Q, T> where
Q: PartialEq + Eq + Clone,
T: PartialEq + Eq + Clone
{
/// Create an empty nodes list.
fn default() -> Self {
Nodes(vec![])
}
}

impl<Q, T> Display for Nodes<Q, T> where Q: PartialEq + Eq + Clone + Display, T: PartialEq + Eq + Clone + Display + Default {
/// Display the nodes list.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for node in self.iter() {
write!(f, "{}", node)?;
Expand Down
15 changes: 15 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@ pub enum NodeRemovalStrategy {
RemoveNodeAndChildren,
}

/// The strategy to use when traversing the tree.
///
/// This enum represents the strategy to use when traversing the tree.
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Debug, Copy)]
pub enum TraversalStrategy {
/// Traverse the tree in pre-order. This means that the root node is visited first, then the left
/// child, and then the right child.
PreOrder,
/// Traverse the tree in post-order. This means that the left child is visited first, then the right
/// child, and then the root node.
PostOrder,
/// Traverse the tree in in-order. This means that the left child is visited first, then the root node,
/// and then the right child.
InOrder,
}

/// A subtree of a tree.
///
/// This struct represents a subtree of a tree. A subtree is a tree that is a part of a larger tree.
pub type SubTree<Q, T> = Tree<Q, T>;

/// A tree data structure.
Expand Down Expand Up @@ -677,6 +689,7 @@ impl<Q, T> Default for Tree<Q, T>
Q: PartialEq + Eq + Clone,
T: PartialEq + Eq + Clone,
{
/// Create a new tree with no nodes.
fn default() -> Self {
Tree { name: None, nodes: Nodes::default() }
}
Expand All @@ -687,6 +700,7 @@ impl<Q, T> Display for Tree<Q, T>
Q: PartialEq + Eq + Clone + Display + Hash,
T: PartialEq + Eq + Clone + Display + Default,
{
/// Print the tree.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(name) = &self.name {
writeln!(f, "{}", name)?;
Expand All @@ -707,6 +721,7 @@ impl<Q, T> Drop for Tree<Q, T>
Q: PartialEq + Eq + Clone,
T: PartialEq + Eq + Clone,
{
/// Drop the tree.
fn drop(&mut self) {
self.nodes.clear();
}
Expand Down

0 comments on commit 58d16b2

Please sign in to comment.