Skip to content

feat: debug_ensure{,_eq,_ne} macros combining debug_assert* and anyhow::ensure (#6907) #6922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: link2xt/pgp-contacts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::mimeparser::AvatarAction;
use crate::param::{Param, Params};
use crate::sync::{self, Sync::*};
use crate::tools::{duration_to_str, get_abs_path, time, SystemTime};
use crate::{chat, chatlist_events, stock_str};
use crate::{chat, chatlist_events, debug_ensure_ne, stock_str};

/// Time during which a contact is considered as seen recently.
const SEEN_RECENTLY_SECONDS: i64 = 600;
Expand Down Expand Up @@ -1879,9 +1879,10 @@ pub(crate) async fn mark_contact_id_as_verified(
contact_id: ContactId,
verifier_id: ContactId,
) -> Result<()> {
debug_assert_ne!(
contact_id, verifier_id,
"Contact cannot be verified by self"
debug_ensure_ne!(
contact_id,
verifier_id,
"Contact cannot be verified by self",
);
context
.sql
Expand Down
13 changes: 7 additions & 6 deletions src/mimefactory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::constants::ASM_SUBJECT;
use crate::constants::{Chattype, DC_FROM_HANDSHAKE};
use crate::contact::{Contact, ContactId, Origin};
use crate::context::Context;
use crate::debug_ensure;
use crate::e2ee::EncryptHelper;
use crate::ephemeral::Timer as EphemeralTimer;
use crate::key::self_fingerprint;
Expand Down Expand Up @@ -309,7 +310,7 @@ impl MimeFactory {
} else if id == ContactId::SELF {
member_fingerprints.push(self_fingerprint.to_string());
} else {
debug_assert!(member_fingerprints.is_empty(), "If some past member is a PGP-contact, all other past members should be PGP-contacts too");
debug_ensure!(member_fingerprints.is_empty(), "If some past member is a PGP-contact, all other past members should be PGP-contacts too");
}
}
member_timestamps.push(add_timestamp);
Expand Down Expand Up @@ -360,16 +361,16 @@ impl MimeFactory {
// if we are leaving the group.
past_member_fingerprints.push(self_fingerprint.to_string());
} else {
debug_assert!(past_member_fingerprints.is_empty(), "If some past member is a PGP-contact, all other past members should be PGP-contacts too");
debug_ensure!(past_member_fingerprints.is_empty(), "If some past member is a PGP-contact, all other past members should be PGP-contacts too");
}
}
}
}
}
}

debug_assert!(member_timestamps.len() >= to.len());
debug_assert!(member_fingerprints.is_empty() || member_fingerprints.len() >= to.len());
debug_ensure!(member_timestamps.len() >= to.len());
debug_ensure!(member_fingerprints.is_empty() || member_fingerprints.len() >= to.len());

if to.len() > 1 {
if let Some(position) = to.iter().position(|(_, x)| x == &from_addr) {
Expand Down Expand Up @@ -446,7 +447,7 @@ impl MimeFactory {
};
let attach_selfavatar = Self::should_attach_selfavatar(context, &msg).await;

debug_assert!(
debug_ensure!(
member_timestamps.is_empty()
|| to.len() + past_members.len() == member_timestamps.len()
);
Expand Down Expand Up @@ -669,7 +670,7 @@ impl MimeFactory {
));
}

debug_assert!(
debug_ensure!(
self.member_timestamps.is_empty()
|| to.len() + past_members.len() == self.member_timestamps.len()
);
Expand Down
12 changes: 6 additions & 6 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::simplify;
use crate::stock_str;
use crate::sync::Sync::*;
use crate::tools::{self, buf_compress, remove_subject_prefix};
use crate::{chatlist_events, location};
use crate::{chatlist_events, debug_ensure, debug_ensure_eq, location};
use crate::{contact, imap};

/// This is the struct that is returned after receiving one email (aka MIME message).
Expand Down Expand Up @@ -1413,7 +1413,7 @@ async fn do_chat_assignment(
false => None,
};
if let Some(chat) = chat {
debug_assert!(chat.typ == Chattype::Single);
debug_ensure!(chat.typ == Chattype::Single);
let mut new_protection = match verified_encryption {
VerifiedEncryption::Verified => ProtectionStatus::Protected,
VerifiedEncryption::NotVerified(_) => ProtectionStatus::Unprotected,
Expand Down Expand Up @@ -2078,7 +2078,7 @@ RETURNING id
// afterwards insert additional parts.
replace_msg_id = None;

debug_assert!(!row_id.is_special());
debug_ensure!(!row_id.is_special());
created_db_entries.push(row_id);
}

Expand Down Expand Up @@ -2341,7 +2341,7 @@ async fn lookup_chat_by_reply(
// lookup by reply should never be needed
// as we can directly assign the message to the chat
// by its group ID.
debug_assert!(mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted());
debug_ensure!(mime_parser.get_chat_group_id().is_none() || !mime_parser.was_encrypted());

// Try to assign message to the same chat as the parent message.
let Some(parent_chat_id) = ChatId::lookup_by_message(parent) else {
Expand Down Expand Up @@ -3587,7 +3587,7 @@ async fn add_or_lookup_pgp_contacts_by_address_list(
}
}

debug_assert_eq!(contact_ids.len(), address_list.len());
debug_ensure_eq!(contact_ids.len(), address_list.len(),);
Ok(contact_ids)
}

Expand Down Expand Up @@ -3741,7 +3741,7 @@ async fn lookup_pgp_contacts_by_address_list(
contact_ids.push(contact_id);
}
}
debug_assert_eq!(address_list.len(), contact_ids.len());
debug_ensure_eq!(address_list.len(), contact_ids.len(),);
Ok(contact_ids)
}

Expand Down
38 changes: 38 additions & 0 deletions src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,5 +765,43 @@ pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
Ok(())
}

/// Return early with an error if a condition is not satisfied.
/// In non-optimized builds, panics instead if so.
#[macro_export]
macro_rules! debug_ensure {
($($arg:tt)*) => {
debug_assert!($($arg)*);
anyhow::ensure!($($arg)*);
};
}

/// Return early with an error on two expressions inequality.
/// In non-optimized builds, panics instead if so.
#[macro_export]
macro_rules! debug_ensure_eq {
($left:expr, $right:expr, $($arg:tt)*) => {
match (&$left, &$right) {
(left_val, right_val) => {
debug_assert_eq!(left_val, right_val, $($arg)*);
anyhow::ensure!(left_val == right_val, $($arg)*);
}
}
};
}

/// Return early with an error on two expressions equality.
/// In non-optimized builds, panics instead if so.
#[macro_export]
macro_rules! debug_ensure_ne {
($left:expr, $right:expr, $($arg:tt)*) => {
match (&$left, &$right) {
(left_val, right_val) => {
debug_assert_ne!(left_val, right_val, $($arg)*);
anyhow::ensure!(left_val != right_val, $($arg)*);
}
}
};
}

#[cfg(test)]
mod tools_tests;
Loading