1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Items to store metadata for/of circuit creation
//!
//! In the context of ```Scanner```,
//!
//! [IncompleteWork] is the information about
//! the guard relay and the exit relay of the two hop circuit to verify
//! if those relays are **partitioned** or **not partitioned**
//!
//! [CompletedWork] is the end result of using the [IncompleteWork] to
//! perform the circuit creation attempt

/// The metadata about the two hop circuit we want to create.
///
/// It represents the two hop circuit that needs to be created where ```source_relay```
/// represents the guard relay of the two hop and ```destination_relay``` represents
/// the exit relay of the two hop circuit
#[derive(Debug, Clone)]
pub struct IncompleteWork {
    /// Metadata to identify the source relay
    pub source_relay: String,

    /// Metadata to identify the destination relay
    pub destination_relay: String,
}

impl IncompleteWork {
    /// Create ```IncompleteWork``` from a ```source_relay``` and ```destination_relay``` that
    /// can be converted to String
    pub fn from<T: ToString>(source_relay: T, destination_relay: T) -> Self {
        Self {
            source_relay: source_relay.to_string(),
            destination_relay: destination_relay.to_string(),
        }
    }
    /// Check whether the given relay fingerprint is of the source relay of IncompleteWork
    pub fn is_source_relay<T: AsRef<str>>(&self, relay_fingerprint: T) -> bool {
        relay_fingerprint.as_ref() == self.source_relay
    }

    /// Check whether the given relay fingerprint is of the destination relay of IncompleteWork
    pub fn is_destination_relay<T: AsRef<str>>(&self, relay_fingerprint: T) -> bool {
        relay_fingerprint.as_ref() == self.destination_relay
    }
}

/// The end result of two hop circuit creation attempt between
/// a guard relay and an exit relay represented by
/// ```source_relay``` and ```destination_relay``` respectively
#[derive(Debug, Clone)]
pub struct CompletedWork {
    /// Fingerprint to identify the source relay
    pub source_relay: String,

    /// Fingerprint to identify the destination relay
    pub destination_relay: String,

    /// Shows how the work was completed, was it a success or failure
    pub status: CompletedWorkStatus,

    /// The Unix epoch at which the test was performed
    pub timestamp: u64,
}

/// The status of completion of [CompletedWork]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompletedWorkStatus {
    /// The circuit creation attempt successful
    Success,

    /// The circuit creation attempt failed with an error
    Failure(String),
}