1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-04-02 02:51:12 +03:00

support some deprecations

This commit is contained in:
Jonathan de Jong 2021-07-12 18:05:43 +02:00
parent 735d7a0815
commit 6e8beb604d
3 changed files with 29 additions and 4 deletions
src
database.rs
database/abstraction
main.rs

View file

@ -42,8 +42,8 @@ use self::proxy::ProxyConfig;
pub struct Config {
server_name: Box<ServerName>,
database_path: String,
#[serde(default = "default_db_cache_capacity")]
db_cache_capacity: u32,
cache_capacity: Option<u32>, // deprecated
db_cache_capacity: Option<u32>,
#[serde(default = "default_sqlite_read_pool_size")]
sqlite_read_pool_size: usize,
#[serde(default = "false_fn")]
@ -73,6 +73,30 @@ pub struct Config {
pub log: String,
}
macro_rules! deprecate_with {
($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);
}
};
($self:expr ; $from:ident -> $to:ident or $default:expr) => {
deprecate_with!($self ; $from -> $to);
$self.$to.get_or_insert_with($default);
};
}
impl Config {
pub fn fallbacks(mut self) -> Self {
// 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);
self
}
}
fn false_fn() -> bool {
false
}

View file

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

View file

@ -196,7 +196,8 @@ async fn main() {
let config = raw_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")
.fallbacks();
let db = Database::load_or_create(config.clone())
.await