wnfs_common/
pathnodes.rs

1use crate::utils::Arc;
2
3//--------------------------------------------------------------------------------------------------
4// Type Definitions
5//--------------------------------------------------------------------------------------------------
6
7/// Represents the directory nodes along a path.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct PathNodes<T> {
10    pub path: Vec<(Arc<T>, String)>,
11    pub tail: Arc<T>,
12}
13
14/// The kinds of outcome from getting a `PathNodes`.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum PathNodesResult<T> {
17    /// The complete path exists.
18    Complete(PathNodes<T>),
19
20    /// The path does not exist.
21    MissingLink(PathNodes<T>, String),
22
23    /// Encountered a node that is not a directory.
24    NotADirectory(PathNodes<T>, String),
25}
26
27//--------------------------------------------------------------------------------------------------
28// Implementations
29//--------------------------------------------------------------------------------------------------
30
31impl<T> PathNodes<T> {
32    /// Returns the length of the path nodes.
33    ///
34    /// # Examples
35    ///
36    /// ```
37    /// use wnfs_common::PathNodes;
38    ///
39    /// let nodes = PathNodes::<usize> {
40    ///     path: vec![
41    ///         (1.into(), "music".to_string()),
42    ///         (2.into(), "rock".to_string()),
43    ///     ],
44    ///     tail: 3.into(),
45    /// };
46    ///
47    /// assert_eq!(nodes.len(), 2);
48    /// ```
49    pub fn len(&self) -> usize {
50        self.path.len()
51    }
52
53    /// Checks if the path nodes are empty.
54    ///
55    /// # Examples
56    ///
57    /// ```
58    /// use wnfs_common::PathNodes;
59    ///
60    /// let nodes = PathNodes::<usize> {
61    ///     path: vec![
62    ///         (1.into(), "music".to_string()),
63    ///         (2.into(), "rock".to_string()),
64    ///     ],
65    ///     tail: 3.into(),
66    /// };
67    ///
68    /// assert!(!nodes.is_empty());
69    /// ```
70    pub fn is_empty(&self) -> bool {
71        self.path.is_empty()
72    }
73}