Skip to main content

spire_core/crawler/meta/
depth.rs

1use std::fmt;
2
3/// How many links away from a seed a request is.
4///
5/// Seeds have depth 0; every followed link is one deeper than its parent.
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
7pub struct Depth(u32);
8
9impl Depth {
10    /// The depth of a seed.
11    pub const ROOT: Self = Self(0);
12
13    /// Creates a depth.
14    #[must_use]
15    pub const fn new(depth: u32) -> Self {
16        Self(depth)
17    }
18
19    /// Returns the depth as a number.
20    #[must_use]
21    pub const fn get(self) -> u32 {
22        self.0
23    }
24
25    /// Returns the depth of a link followed from this one.
26    #[must_use]
27    pub const fn child(self) -> Self {
28        Self(self.0.saturating_add(1))
29    }
30}
31
32impl fmt::Display for Depth {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        self.0.fmt(f)
35    }
36}