Skip to main content

spire_core/crawler/meta/
attempt.rs

1use std::fmt;
2
3/// How many times a request was tried before.
4///
5/// The first try is attempt 0.
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
7pub struct Attempt(u32);
8
9impl Attempt {
10    /// The first try.
11    pub const FIRST: Self = Self(0);
12
13    /// Creates an attempt.
14    #[must_use]
15    pub const fn new(attempt: u32) -> Self {
16        Self(attempt)
17    }
18
19    /// Returns the attempt as a number.
20    #[must_use]
21    pub const fn get(self) -> u32 {
22        self.0
23    }
24
25    /// Returns the attempt after this one.
26    #[must_use]
27    pub const fn next(self) -> Self {
28        Self(self.0.saturating_add(1))
29    }
30}
31
32impl fmt::Display for Attempt {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        self.0.fmt(f)
35    }
36}