Strongly type stock strings

This changes the internal stock strings API to be more strongly typed,
ensuring that the caller can not construct the stock string in the
wrong way.

The old approach left it to the callers to figure out how a stock
string should be created, now each stock string has their specific
arguments and callers can not make mistakes.  In particular all the
subtleties and different ways of calling stock_system_msg() disappear.

This could not use a trait for stock strings, as this would not allow
having per-message typed arguments.  So we needed a type per message
with a custom method, only by convention this method is .stock_str().
The type is a enum without variants to avoid allowing someone to
create the type.

Sadly the fallback string and substitutions are still far away from
each other, but it is now only one place which needs to know how to
construct the string instead of many.
This commit is contained in:
Floris Bruynooghe
2021-02-07 15:52:30 +01:00
parent ad640e163c
commit 29f184b4c4
19 changed files with 1488 additions and 647 deletions

View File

@@ -10,22 +10,19 @@ use std::time::Duration;
use anyhow::format_err;
use rusqlite::{Connection, Error as SqlError, OpenFlags};
use crate::chat::add_device_msg;
use crate::chat::{add_device_msg, update_device_icon, update_saved_messages_icon};
use crate::config::Config;
use crate::config::Config::DeleteServerAfter;
use crate::constants::{ShowEmails, DC_CHAT_ID_TRASH};
use crate::constants::{ShowEmails, Viewtype, DC_CHAT_ID_TRASH};
use crate::context::Context;
use crate::dc_tools::{dc_delete_file, time, EmailAddress};
use crate::ephemeral::start_ephemeral_timers;
use crate::imap;
use crate::message::Message;
use crate::param::{Param, Params};
use crate::peerstate::Peerstate;
use crate::provider::get_provider_by_domain;
use crate::stock::StockMessage;
use crate::{
chat::{update_device_icon, update_saved_messages_icon},
config::Config,
};
use crate::{constants::Viewtype, message::Message};
use crate::stock::DeleteServerTurnedOff;
#[macro_export]
macro_rules! paramsv {
@@ -1544,12 +1541,7 @@ CREATE INDEX devmsglabels_index1 ON devmsglabels (label);
// So, for people who have delete_server enabled, disable it and add a hint to the devicechat:
if context.get_config_delete_server_after().await.is_some() {
let mut msg = Message::new(Viewtype::Text);
msg.text = Some(
context
.stock_str(StockMessage::DeleteServerTurnedOff)
.await
.into(),
);
msg.text = Some(DeleteServerTurnedOff::stock_str(context).await.into());
add_device_msg(context, None, Some(&mut msg)).await?;
context.set_config(DeleteServerAfter, Some("0")).await?;
}