Skip to content

Commit 3c933d7

Browse files
authored
Remove PartialEq from enum errors (#641)
Secondary errors will be required to implement `PartialEq` but it's not possible for external errors. If we want to compare errors, we can use different approach as showed.
1 parent 1a3105b commit 3c933d7

File tree

4 files changed

+42
-49
lines changed

4 files changed

+42
-49
lines changed

xmtp_id/src/associations/association_log.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::state::AssociationState;
55

66
use thiserror::Error;
77

8-
#[derive(Debug, Error, PartialEq)]
8+
#[derive(Debug, Error)]
99
pub enum AssociationError {
1010
#[error("Error creating association {0}")]
1111
Generic(String),
@@ -16,7 +16,7 @@ pub enum AssociationError {
1616
#[error("Signature validation failed {0}")]
1717
Signature(#[from] SignatureError),
1818
#[error("Member of kind {0} not allowed to add {1}")]
19-
MemberNotAllowed(String, String),
19+
MemberNotAllowed(MemberKind, MemberKind),
2020
#[error("Missing existing member")]
2121
MissingExistingMember,
2222
#[error("Legacy key is only allowed to be associated using a legacy signature with nonce 0")]
@@ -165,8 +165,8 @@ impl IdentityAction for AddAssociation {
165165

166166
// Ensure that the new member signature is correct for the new member type
167167
allowed_association(
168-
&existing_member_identifier.kind(),
169-
&self.new_member_identifier.kind(),
168+
existing_member_identifier.kind(),
169+
self.new_member_identifier.kind(),
170170
)?;
171171

172172
let new_member = Member::new(new_member_address, Some(existing_entity_id));
@@ -347,16 +347,16 @@ fn is_legacy_signature(signature: &Box<dyn Signature>) -> bool {
347347
}
348348

349349
fn allowed_association(
350-
existing_member_kind: &MemberKind,
351-
new_member_kind: &MemberKind,
350+
existing_member_kind: MemberKind,
351+
new_member_kind: MemberKind,
352352
) -> Result<(), AssociationError> {
353353
// The only disallowed association is an installation adding an installation
354-
if existing_member_kind.eq(&MemberKind::Installation)
355-
&& new_member_kind.eq(&MemberKind::Installation)
354+
if existing_member_kind == MemberKind::Installation
355+
&& new_member_kind == MemberKind::Installation
356356
{
357357
return Err(AssociationError::MemberNotAllowed(
358-
existing_member_kind.to_string(),
359-
new_member_kind.to_string(),
358+
existing_member_kind,
359+
new_member_kind,
360360
));
361361
}
362362

xmtp_id/src/associations/builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl IdentityUpdateBuilder {
141141
}
142142
}
143143

144-
#[derive(Debug, Error, PartialEq)]
144+
#[derive(Debug, Error)]
145145
pub enum SignatureRequestError {
146146
#[error("Unknown signer")]
147147
UnknownSigner,
@@ -434,9 +434,9 @@ mod tests {
434434
MockSignature::new_boxed(true, rand_string().into(), SignatureKind::Erc191, None),
435435
);
436436

437-
assert_eq!(
437+
assert!(matches!(
438438
attempt_to_add_random_member,
439439
Err(SignatureRequestError::UnknownSigner)
440-
);
440+
));
441441
}
442442
}

xmtp_id/src/associations/mod.rs

+28-35
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ mod tests {
233233
..Default::default()
234234
});
235235
let update_result = apply_update(state, IdentityUpdate::new_test(vec![update]));
236-
assert!(update_result.is_err());
237-
assert_eq!(update_result.err().unwrap(), AssociationError::Replay);
236+
assert!(matches!(update_result, Err(AssociationError::Replay)));
238237
}
239238

240239
#[test]
@@ -285,11 +284,10 @@ mod tests {
285284
let state_result = get_state(vec![IdentityUpdate::new_test(vec![Action::CreateInbox(
286285
action,
287286
)])]);
288-
assert!(state_result.is_err());
289-
assert_eq!(
290-
state_result.err().unwrap(),
291-
AssociationError::Signature(SignatureError::Invalid)
292-
);
287+
assert!(matches!(
288+
state_result,
289+
Err(AssociationError::Signature(SignatureError::Invalid))
290+
));
293291
}
294292

295293
#[test]
@@ -307,11 +305,10 @@ mod tests {
307305
initial_state.clone(),
308306
IdentityUpdate::new_test(vec![update_with_bad_existing_member]),
309307
);
310-
assert!(update_result.is_err());
311-
assert_eq!(
312-
update_result.err().unwrap(),
313-
AssociationError::Signature(SignatureError::Invalid)
314-
);
308+
assert!(matches!(
309+
update_result,
310+
Err(AssociationError::Signature(SignatureError::Invalid))
311+
));
315312

316313
let update_with_bad_new_member = Action::AddAssociation(AddAssociation {
317314
new_member_signature: bad_signature.clone(),
@@ -328,11 +325,10 @@ mod tests {
328325
initial_state,
329326
IdentityUpdate::new_test(vec![update_with_bad_new_member]),
330327
);
331-
assert!(update_result_2.is_err());
332-
assert_eq!(
333-
update_result_2.err().unwrap(),
334-
AssociationError::Signature(SignatureError::Invalid)
335-
);
328+
assert!(matches!(
329+
update_result_2,
330+
Err(AssociationError::Signature(SignatureError::Invalid))
331+
));
336332
}
337333

338334
#[test]
@@ -351,11 +347,10 @@ mod tests {
351347
});
352348

353349
let state_result = get_state(vec![IdentityUpdate::new_test(vec![create_request, update])]);
354-
assert!(state_result.is_err());
355-
assert_eq!(
356-
state_result.err().unwrap(),
357-
AssociationError::MissingExistingMember
358-
);
350+
assert!(matches!(
351+
state_result,
352+
Err(AssociationError::MissingExistingMember)
353+
));
359354
}
360355

361356
#[test]
@@ -383,14 +378,13 @@ mod tests {
383378
});
384379

385380
let update_result = apply_update(existing_state, IdentityUpdate::new_test(vec![update]));
386-
assert!(update_result.is_err());
387-
assert_eq!(
388-
update_result.err().unwrap(),
389-
AssociationError::MemberNotAllowed(
390-
MemberKind::Installation.to_string(),
391-
MemberKind::Installation.to_string()
392-
)
393-
);
381+
assert!(matches!(
382+
update_result,
383+
Err(AssociationError::MemberNotAllowed(
384+
MemberKind::Installation,
385+
MemberKind::Installation
386+
))
387+
));
394388
}
395389

396390
#[test]
@@ -569,10 +563,9 @@ mod tests {
569563

570564
let revoke_result =
571565
apply_update(new_state, IdentityUpdate::new_test(vec![attempted_revoke]));
572-
assert!(revoke_result.is_err());
573-
assert_eq!(
574-
revoke_result.err().unwrap(),
575-
AssociationError::MissingExistingMember
576-
);
566+
assert!(matches!(
567+
revoke_result,
568+
Err(AssociationError::MissingExistingMember)
569+
));
577570
}
578571
}

xmtp_id/src/associations/signature.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use thiserror::Error;
22

33
use super::MemberIdentifier;
44

5-
#[derive(Debug, Error, PartialEq)]
5+
#[derive(Debug, Error)]
66
pub enum SignatureError {
77
#[error("Signature validation failed")]
88
Invalid,

0 commit comments

Comments
 (0)