#[derive(Debug, Clone)]
pub struct IncompleteWork {
pub source_relay: String,
pub destination_relay: String,
}
impl IncompleteWork {
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(),
}
}
pub fn is_source_relay<T: AsRef<str>>(&self, relay_fingerprint: T) -> bool {
relay_fingerprint.as_ref() == self.source_relay
}
pub fn is_destination_relay<T: AsRef<str>>(&self, relay_fingerprint: T) -> bool {
relay_fingerprint.as_ref() == self.destination_relay
}
}
#[derive(Debug, Clone)]
pub struct CompletedWork {
pub source_relay: String,
pub destination_relay: String,
pub status: CompletedWorkStatus,
pub timestamp: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompletedWorkStatus {
Success,
Failure(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_incomplete_work_creation() {
let incomplete_work = IncompleteWork::from("relay1", "relay2");
assert_eq!(incomplete_work.source_relay, "relay1");
assert_eq!(incomplete_work.destination_relay, "relay2");
}
#[test]
fn test_incomplete_work_relay_check() {
let incomplete_work = IncompleteWork::from("relay1", "relay2");
assert!(incomplete_work.is_source_relay("relay1"));
assert!(!incomplete_work.is_source_relay("relay2"));
assert!(incomplete_work.is_destination_relay("relay2"));
assert!(!incomplete_work.is_destination_relay("relay1"));
}
#[test]
fn test_completed_work_success() {
let completed_work = CompletedWork {
source_relay: "relay1".to_string(),
destination_relay: "relay2".to_string(),
status: CompletedWorkStatus::Success,
timestamp: 12345,
};
assert_eq!(completed_work.source_relay, "relay1");
assert_eq!(completed_work.destination_relay, "relay2");
assert!(matches!(completed_work.status, CompletedWorkStatus::Success));
assert_eq!(completed_work.timestamp, 12345);
}
#[test]
fn test_completed_work_failure() {
let error_message = "Circuit build failed".to_string();
let completed_work = CompletedWork {
source_relay: "relay1".to_string(),
destination_relay: "relay2".to_string(),
status: CompletedWorkStatus::Failure(error_message.clone()),
timestamp: 12345,
};
assert_eq!(completed_work.source_relay, "relay1");
assert_eq!(completed_work.destination_relay, "relay2");
match completed_work.status {
CompletedWorkStatus::Failure(msg) => assert_eq!(msg, error_message),
CompletedWorkStatus::Success => panic!("Expected Failure status"),
}
assert_eq!(completed_work.timestamp, 12345);
}
#[test]
fn test_work_status_equality() {
assert_eq!(CompletedWorkStatus::Success, CompletedWorkStatus::Success);
let failure1 = CompletedWorkStatus::Failure("Error1".to_string());
let failure2 = CompletedWorkStatus::Failure("Error1".to_string());
let failure3 = CompletedWorkStatus::Failure("Error2".to_string());
assert_eq!(failure1, failure2);
assert_ne!(failure1, failure3);
assert_ne!(CompletedWorkStatus::Success, failure1);
}
}