use serde::{
de::{self, Deserializer},
Deserialize, Serialize,
};
use rmqtt::utils::Bytesize;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PluginConfig {
#[serde(
default = "PluginConfig::storage_default",
deserialize_with = "PluginConfig::deserialize_storage"
)]
pub storage: Option<Config>,
#[serde(default = "PluginConfig::cleanup_count_default")]
pub cleanup_count: usize,
}
impl PluginConfig {
fn storage_default() -> Option<Config> {
#[cfg(feature = "ram")]
return Some(Config::Ram(RamConfig::default()));
#[cfg(not(feature = "ram"))]
None
}
fn cleanup_count_default() -> usize {
2000
}
#[inline]
fn deserialize_storage<'de, D>(deserializer: D) -> std::result::Result<Option<Config>, D::Error>
where
D: Deserializer<'de>,
{
let storage = serde_json::Value::deserialize(deserializer)?;
let typ = storage.as_object().and_then(|obj| obj.get("type").and_then(|typ| typ.as_str()));
match typ {
#[cfg(feature = "ram")]
Some("ram") => {
match storage
.as_object()
.and_then(|obj| {
obj.get("ram").map(|ram| serde_json::from_value::<RamConfig>(ram.clone()))
})
.unwrap_or_else(|| Ok(RamConfig::default()))
{
Err(e) => Err(de::Error::custom(e.to_string())),
Ok(ram) => Ok(Some(Config::Ram(ram))),
}
}
#[cfg(any(feature = "redis", feature = "redis-cluster"))]
Some("redis") | Some("redis-cluster") => {
match serde_json::from_value::<rmqtt_storage::Config>(storage) {
Err(e) => Err(de::Error::custom(e.to_string())),
Ok(s_cfg) => Ok(Some(Config::Storage(s_cfg))),
}
}
_ => Err(de::Error::custom(format!("Unsupported storage type, {typ:?}"))),
}
}
#[inline]
pub fn to_json(&self) -> serde_json::Value {
serde_json::json!(self)
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Config {
#[cfg(feature = "ram")]
Ram(RamConfig),
#[cfg(any(feature = "redis", feature = "redis-cluster"))]
Storage(rmqtt_storage::Config),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RamConfig {
pub cache_capacity: Bytesize,
pub cache_max_count: usize,
pub encode: bool,
}
impl Default for RamConfig {
#[inline]
fn default() -> Self {
RamConfig {
cache_capacity: Bytesize::from(1024 * 1024 * 1024 * 2),
cache_max_count: usize::MAX,
encode: false,
}
}
}