Browse Source

warn on deprecated keys

merge-requests/114/head
Jonathan de Jong 5 years ago
parent
commit
b89cffed34
  1. 42
      src/database.rs
  2. 2
      src/database/abstraction/sqlite.rs
  3. 5
      src/main.rs

42
src/database.rs

@ -26,9 +26,9 @@ use rocket::{
try_outcome, State, try_outcome, State,
}; };
use ruma::{DeviceId, ServerName, UserId}; use ruma::{DeviceId, ServerName, UserId};
use serde::Deserialize; use serde::{de::IgnoredAny, Deserialize};
use std::{ use std::{
collections::HashMap, collections::{BTreeMap, HashMap},
fs::{self, remove_dir_all}, fs::{self, remove_dir_all},
io::Write, io::Write,
ops::Deref, ops::Deref,
@ -42,8 +42,8 @@ use self::proxy::ProxyConfig;
pub struct Config { pub struct Config {
server_name: Box<ServerName>, server_name: Box<ServerName>,
database_path: String, database_path: String,
cache_capacity: Option<u32>, // deprecated #[serde(default = "default_db_cache_capacity")]
db_cache_capacity: Option<u32>, db_cache_capacity: u32,
#[serde(default = "default_sqlite_read_pool_size")] #[serde(default = "default_sqlite_read_pool_size")]
sqlite_read_pool_size: usize, sqlite_read_pool_size: usize,
#[serde(default = "false_fn")] #[serde(default = "false_fn")]
@ -71,28 +71,28 @@ pub struct Config {
trusted_servers: Vec<Box<ServerName>>, trusted_servers: Vec<Box<ServerName>>,
#[serde(default = "default_log")] #[serde(default = "default_log")]
pub log: String, pub log: String,
#[serde(flatten)]
catchall: BTreeMap<String, IgnoredAny>,
} }
macro_rules! deprecate_with { const DEPRECATED_KEYS: &[&str] = &["cache_capacity"];
($self:expr ; $from:ident -> $to:ident) => {
if let Some(v) = $self.$from {
let from = stringify!($from);
let to = stringify!($to);
log::warn!("{} is deprecated, use {} instead", from, to);
$self.$to.get_or_insert(v); impl Config {
} pub fn warn_deprecated(&self) {
}; let mut was_deprecated = false;
($self:expr ; $from:ident -> $to:ident or $default:expr) => { for key in self
deprecate_with!($self ; $from -> $to); .catchall
$self.$to.get_or_insert_with($default); .keys()
}; .filter(|key| DEPRECATED_KEYS.iter().any(|s| s == key))
{
log::warn!("Config parameter {} is deprecated", key);
was_deprecated = true;
} }
impl Config { if was_deprecated {
pub fn process_fallbacks(&mut self) { log::warn!("Read conduit documentation and check your configuration if any new configuration parameters should be adjusted");
// TODO: have a proper way handle into above struct (maybe serde supports something like this?) }
deprecate_with!(self ; cache_capacity -> db_cache_capacity or default_db_cache_capacity);
} }
} }

2
src/database/abstraction/sqlite.rs

@ -128,7 +128,7 @@ impl DatabaseEngine for Engine {
let pool = Pool::new( let pool = Pool::new(
Path::new(&config.database_path).join("conduit.db"), Path::new(&config.database_path).join("conduit.db"),
config.sqlite_read_pool_size, config.sqlite_read_pool_size,
config.db_cache_capacity.expect("fallbacks hasn't been called") / 1024, // bytes -> kb config.db_cache_capacity / 1024, // bytes -> kb
)?; )?;
pool.write_lock() pool.write_lock()

5
src/main.rs

@ -196,7 +196,7 @@ async fn main() {
std::env::set_var("RUST_LOG", "warn"); std::env::set_var("RUST_LOG", "warn");
let mut config = raw_config let config = raw_config
.extract::<Config>() .extract::<Config>()
.expect("It looks like your config is invalid. Please take a look at the error"); .expect("It looks like your config is invalid. Please take a look at the error");
@ -218,8 +218,7 @@ async fn main() {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
} }
// Required here to process fallbacks while logging is enabled, but before config is actually used for anything config.warn_deprecated();
config.process_fallbacks();
let db = Database::load_or_create(config) let db = Database::load_or_create(config)
.await .await

Loading…
Cancel
Save