|
| 1 | +#[derive(Clone, Debug, PartialEq)] |
| 2 | +pub enum MemberKind { |
| 3 | + Installation, |
| 4 | + Address, |
| 5 | +} |
| 6 | + |
| 7 | +impl std::fmt::Display for MemberKind { |
| 8 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 9 | + match self { |
| 10 | + MemberKind::Installation => write!(f, "installation"), |
| 11 | + MemberKind::Address => write!(f, "address"), |
| 12 | + } |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| 17 | +pub enum MemberIdentifier { |
| 18 | + Address(String), |
| 19 | + Installation(Vec<u8>), |
| 20 | +} |
| 21 | + |
| 22 | +impl MemberIdentifier { |
| 23 | + pub fn to_string(&self) -> String { |
| 24 | + match self { |
| 25 | + MemberIdentifier::Address(address) => address.to_string(), |
| 26 | + MemberIdentifier::Installation(installation) => hex::encode(installation), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + pub fn kind(&self) -> MemberKind { |
| 31 | + match self { |
| 32 | + MemberIdentifier::Address(_) => MemberKind::Address, |
| 33 | + MemberIdentifier::Installation(_) => MemberKind::Installation, |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl std::fmt::Display for MemberIdentifier { |
| 39 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 40 | + write!(f, "{}", self.to_string()) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl From<String> for MemberIdentifier { |
| 45 | + fn from(address: String) -> Self { |
| 46 | + MemberIdentifier::Address(address) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl From<Vec<u8>> for MemberIdentifier { |
| 51 | + fn from(installation: Vec<u8>) -> Self { |
| 52 | + MemberIdentifier::Installation(installation) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Clone, Debug, PartialEq)] |
| 57 | +pub struct Member { |
| 58 | + pub identifier: MemberIdentifier, |
| 59 | + pub added_by_entity: Option<MemberIdentifier>, |
| 60 | +} |
| 61 | + |
| 62 | +impl Member { |
| 63 | + pub fn new(identifier: MemberIdentifier, added_by_entity: Option<MemberIdentifier>) -> Self { |
| 64 | + Self { |
| 65 | + identifier, |
| 66 | + added_by_entity, |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + pub fn kind(&self) -> MemberKind { |
| 71 | + self.identifier.kind() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl PartialEq<MemberIdentifier> for Member { |
| 76 | + fn eq(&self, other: &MemberIdentifier) -> bool { |
| 77 | + self.identifier.eq(other) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[cfg(test)] |
| 82 | +mod tests { |
| 83 | + use crate::associations::test_utils; |
| 84 | + |
| 85 | + use super::*; |
| 86 | + |
| 87 | + use test_utils::rand_string; |
| 88 | + |
| 89 | + impl Default for MemberIdentifier { |
| 90 | + fn default() -> Self { |
| 91 | + MemberIdentifier::Address(rand_string()) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + impl Default for Member { |
| 96 | + fn default() -> Self { |
| 97 | + Self { |
| 98 | + identifier: MemberIdentifier::default(), |
| 99 | + added_by_entity: None, |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_identifier_comparisons() { |
| 106 | + let address_1 = MemberIdentifier::Address("0x123".to_string()); |
| 107 | + let address_2 = MemberIdentifier::Address("0x456".to_string()); |
| 108 | + let address_1_copy = MemberIdentifier::Address("0x123".to_string()); |
| 109 | + |
| 110 | + assert!(address_1 != address_2); |
| 111 | + assert!(address_1.ne(&address_2)); |
| 112 | + assert!(address_1 == address_1_copy); |
| 113 | + |
| 114 | + let installation_1 = MemberIdentifier::Installation(vec![1, 2, 3]); |
| 115 | + let installation_2 = MemberIdentifier::Installation(vec![4, 5, 6]); |
| 116 | + let installation_1_copy = MemberIdentifier::Installation(vec![1, 2, 3]); |
| 117 | + |
| 118 | + assert!(installation_1 != installation_2); |
| 119 | + assert!(installation_1.ne(&installation_2)); |
| 120 | + assert!(installation_1 == installation_1_copy); |
| 121 | + } |
| 122 | +} |
0 commit comments