Skip to content

Commit 3371c01

Browse files
authored
Added a Plugin trait (#536)
* Improved logging * Improved logging for more `Address` usages * Fixed lint issues. * Reverted the `Address` logging changes. * Applied the PR comment by @levkk. * Applied the PR comment by @levkk. * Applied the PR comment by @levkk. * Applied the PR comment by @levkk.
1 parent c2a483f commit 3371c01

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/config.rs

+39-4
Original file line numberDiff line numberDiff line change
@@ -867,15 +867,26 @@ pub struct Plugins {
867867
pub prewarmer: Option<Prewarmer>,
868868
}
869869

870+
pub trait Plugin {
871+
fn is_enabled(&self) -> bool;
872+
}
873+
870874
impl std::fmt::Display for Plugins {
871875
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
876+
fn is_enabled<T: Plugin>(arg: Option<&T>) -> bool {
877+
if let Some(ref arg) = arg {
878+
arg.is_enabled()
879+
} else {
880+
false
881+
}
882+
}
872883
write!(
873884
f,
874885
"interceptor: {}, table_access: {}, query_logger: {}, prewarmer: {}",
875-
self.intercept.is_some(),
876-
self.table_access.is_some(),
877-
self.query_logger.is_some(),
878-
self.prewarmer.is_some(),
886+
is_enabled(self.intercept.as_ref()),
887+
is_enabled(self.table_access.as_ref()),
888+
is_enabled(self.query_logger.as_ref()),
889+
is_enabled(self.prewarmer.as_ref()),
879890
)
880891
}
881892
}
@@ -886,23 +897,47 @@ pub struct Intercept {
886897
pub queries: BTreeMap<String, Query>,
887898
}
888899

900+
impl Plugin for Intercept {
901+
fn is_enabled(&self) -> bool {
902+
self.enabled
903+
}
904+
}
905+
889906
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
890907
pub struct TableAccess {
891908
pub enabled: bool,
892909
pub tables: Vec<String>,
893910
}
894911

912+
impl Plugin for TableAccess {
913+
fn is_enabled(&self) -> bool {
914+
self.enabled
915+
}
916+
}
917+
895918
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
896919
pub struct QueryLogger {
897920
pub enabled: bool,
898921
}
899922

923+
impl Plugin for QueryLogger {
924+
fn is_enabled(&self) -> bool {
925+
self.enabled
926+
}
927+
}
928+
900929
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
901930
pub struct Prewarmer {
902931
pub enabled: bool,
903932
pub queries: Vec<String>,
904933
}
905934

935+
impl Plugin for Prewarmer {
936+
fn is_enabled(&self) -> bool {
937+
self.enabled
938+
}
939+
}
940+
906941
impl Intercept {
907942
pub fn substitute(&mut self, db: &str, user: &str) {
908943
for (_, query) in self.queries.iter_mut() {

0 commit comments

Comments
 (0)