From 6c47e86380a43d6eadf03e88e57ddfdf2b1e3d2a Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 22 Aug 2026 01:01:15 +0000 Subject: [PATCH] refactor: turn DC_MSG_ID_* into MsgId::* associated constants --- deltachat-ffi/src/dc_array.rs | 3 +-- deltachat-ffi/src/lib.rs | 5 ++--- deltachat-jsonrpc/src/api.rs | 5 ++--- deltachat-repl/src/cmdline.rs | 4 ++-- src/constants.rs | 3 --- src/message.rs | 27 +++++++++++---------------- 6 files changed, 18 insertions(+), 29 deletions(-) diff --git a/deltachat-ffi/src/dc_array.rs b/deltachat-ffi/src/dc_array.rs index 98def5d0e..56307758e 100644 --- a/deltachat-ffi/src/dc_array.rs +++ b/deltachat-ffi/src/dc_array.rs @@ -1,5 +1,4 @@ use crate::chat::ChatItem; -use crate::constants::DC_MSG_ID_DAYMARKER; use crate::contact::ContactId; use crate::location::Location; use crate::message::MsgId; @@ -21,7 +20,7 @@ impl dc_array_t { Self::ContactIds(array) => array[index].to_u32(), Self::Chat(array) => match array[index] { ChatItem::Message { msg_id } => msg_id.to_u32(), - ChatItem::DayMarker { .. } => DC_MSG_ID_DAYMARKER, + ChatItem::DayMarker { .. } => MsgId::DAYMARKER.to_u32(), }, Self::Locations(array) => array[index].location_id, Self::Uint(array) => array[index], diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 5d0b8a69b..66e05403a 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -23,7 +23,6 @@ use std::time::{Duration, SystemTime}; use anyhow::Context as _; use deltachat::chat::{ChatId, ChatVisibility, MessageListOptions, MuteDuration}; -use deltachat::constants::DC_MSG_ID_LAST_SPECIAL; use deltachat::contact::{Contact, ContactId, Origin}; use deltachat::context::{Context, ContextBuilder}; use deltachat::ephemeral::Timer as EphemeralTimer; @@ -2038,7 +2037,7 @@ pub unsafe extern "C" fn dc_get_msg(context: *mut dc_context_t, msg_id: u32) -> { Ok(msg) => msg, Err(_) => { - if msg_id <= constants::DC_MSG_ID_LAST_SPECIAL { + if MsgId::new(msg_id).is_special() { // C-core API returns empty messages, do the same message::Message::new(Viewtype::default()) } else { @@ -4407,7 +4406,7 @@ fn convert_and_prune_message_ids(msg_ids: *const u32, msg_cnt: libc::c_int) -> V let ids = unsafe { std::slice::from_raw_parts(msg_ids, msg_cnt as usize) }; let msg_ids: Vec = ids .iter() - .filter(|id| **id > DC_MSG_ID_LAST_SPECIAL) + .filter(|id| **id > MsgId::LAST_SPECIAL.to_u32()) .map(|id| MsgId::new(*id)) .collect(); diff --git a/deltachat-jsonrpc/src/api.rs b/deltachat-jsonrpc/src/api.rs index 89ac5a60c..a82f15d3d 100644 --- a/deltachat-jsonrpc/src/api.rs +++ b/deltachat-jsonrpc/src/api.rs @@ -17,7 +17,6 @@ use deltachat::chat::{ }; use deltachat::chatlist::Chatlist; use deltachat::config::{Config, get_all_ui_config_keys}; -use deltachat::constants::DC_MSG_ID_DAYMARKER; use deltachat::contact::{Contact, ContactId, Origin, may_be_valid_addr}; use deltachat::context::get_info; use deltachat::ephemeral::Timer; @@ -1382,7 +1381,7 @@ impl CommandApi { /// /// * chat_id The chat ID of which the messages IDs should be queried. /// * _info_only: Deprecated, pass `false` here. - /// * `add_daymarker` - If `true`, add day markers as `DC_MSG_ID_DAYMARKER` to the result, + /// * `add_daymarker` - If `true`, add day markers as `MsgId::DAYMARKER` to the result, /// e.g. [1234, 1237, 9, 1239]. The day marker timestamp is the midnight one for the /// corresponding (following) day in the local timezone. async fn get_message_ids( @@ -1404,7 +1403,7 @@ impl CommandApi { .map(|chat_item| -> u32 { match chat_item { deltachat::chat::ChatItem::Message { msg_id } => msg_id.to_u32(), - deltachat::chat::ChatItem::DayMarker { .. } => DC_MSG_ID_DAYMARKER, + deltachat::chat::ChatItem::DayMarker { .. } => MsgId::DAYMARKER.to_u32(), } }) .collect()) diff --git a/deltachat-repl/src/cmdline.rs b/deltachat-repl/src/cmdline.rs index 7070d2824..098936350 100644 --- a/deltachat-repl/src/cmdline.rs +++ b/deltachat-repl/src/cmdline.rs @@ -228,7 +228,7 @@ async fn log_msg(context: &Context, prefix: impl AsRef, msg: &Message) { async fn log_msglist(context: &Context, msglist: &[MsgId]) -> Result<()> { let mut lines_out = 0; for &msg_id in msglist { - if msg_id == MsgId::new(DC_MSG_ID_DAYMARKER) { + if msg_id == MsgId::DAYMARKER { println!( "--------------------------------------------------------------------------------" ); @@ -630,7 +630,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu .into_iter() .map(|x| match x { ChatItem::Message { msg_id } => msg_id, - ChatItem::DayMarker { .. } => MsgId::new(DC_MSG_ID_DAYMARKER), + ChatItem::DayMarker { .. } => MsgId::DAYMARKER, }) .collect(); diff --git a/src/constants.rs b/src/constants.rs index 469a8ab42..9ded94cfa 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -130,9 +130,6 @@ pub enum Chattype { InBroadcast = 165, } -pub const DC_MSG_ID_DAYMARKER: u32 = 9; -pub const DC_MSG_ID_LAST_SPECIAL: u32 = 9; - /// String that indicates that something is left out or truncated. pub(crate) const DC_ELLIPSIS: &str = "[...]"; // how many lines desktop can display when fullscreen (fullscreen at zoomlevel 1x) diff --git a/src/message.rs b/src/message.rs index db64daaa3..f6def15ad 100644 --- a/src/message.rs +++ b/src/message.rs @@ -4,7 +4,7 @@ use std::collections::BTreeSet; use std::path::{Path, PathBuf}; use std::str; -use anyhow::{Context as _, Result, ensure, format_err}; +use anyhow::{Context as _, Result, ensure}; use deltachat_contact_tools::{VcardContact, parse_vcard}; use deltachat_derive::{FromSql, ToSql}; use humansize::BINARY; @@ -17,7 +17,7 @@ use crate::blob::BlobObject; use crate::chat::{Chat, ChatId, ChatIdBlocked, ChatVisibility, send_msg}; use crate::chatlist_events; use crate::config::Config; -use crate::constants::{Blocked, Chattype, DC_MSG_ID_LAST_SPECIAL}; +use crate::constants::{Blocked, Chattype}; use crate::contact::{self, Contact, ContactId}; use crate::context::Context; use crate::debug_logging::set_debug_logging_xdc; @@ -49,13 +49,18 @@ use crate::tools::{ pub struct MsgId(u32); impl MsgId { + /// Markers added before each day in a local timezone. + pub const DAYMARKER: MsgId = MsgId::new(9); + /// Largest reserved message ID. + pub const LAST_SPECIAL: MsgId = MsgId::new(9); + /// Create a new [MsgId]. - pub fn new(id: u32) -> MsgId { + pub const fn new(id: u32) -> MsgId { MsgId(id) } /// Create a new unset [MsgId]. - pub fn new_unset() -> MsgId { + pub const fn new_unset() -> MsgId { MsgId(0) } @@ -63,7 +68,7 @@ impl MsgId { /// /// This kind of message ID can not be used for real messages. pub fn is_special(self) -> bool { - self.0 <= DC_MSG_ID_LAST_SPECIAL + (0..=Self::LAST_SPECIAL.0).contains(&self.0) } /// Whether the message ID is unset. @@ -355,18 +360,8 @@ impl std::fmt::Display for MsgId { /// Allow converting [MsgId] to an SQLite type. /// /// This allows you to directly store [MsgId] into the database. -/// -/// # Errors -/// -/// This **does** ensure that no special message IDs are written into -/// the database and the conversion will fail if this is not the case. impl rusqlite::types::ToSql for MsgId { fn to_sql(&self) -> rusqlite::Result> { - if self.0 <= DC_MSG_ID_LAST_SPECIAL { - return Err(rusqlite::Error::ToSqlConversionFailure( - format_err!("Invalid MsgId {}", self.0).into(), - )); - } let val = rusqlite::types::Value::Integer(i64::from(self.0)); let out = rusqlite::types::ToSqlOutput::Owned(val); Ok(out) @@ -2106,7 +2101,7 @@ pub async fn estimate_deletion_cnt( AND chat_id != ?3 AND chat_id != ?4 AND hidden = 0;", ( - DC_MSG_ID_LAST_SPECIAL, + MsgId::LAST_SPECIAL, threshold_timestamp, self_chat_id, ChatId::TRASH,