Skip to main content

shared/
censor.rs

1pub const CENSORED_PLACEHOLDER: &str = "";
2
3/// Implemented by values carrying secrets, so each one names its own instead of a central list
4/// having to track them.
5///
6/// Censoring happens in place and on an owned value rather than during serialization: the same
7/// [`serde::Serialize`] impls back both the API responses and the `jsonb` columns these values are
8/// stored in, so a serializer that censored would write the placeholder to the database.
9pub trait Censor {
10    /// Overwrite every secret this value carries, leaving everything else intact.
11    fn censor(&mut self);
12
13    #[inline]
14    fn censored(mut self) -> Self
15    where
16        Self: Sized,
17    {
18        self.censor();
19        self
20    }
21}
22
23impl<T: Censor> Censor for Option<T> {
24    fn censor(&mut self) {
25        if let Some(value) = self {
26            value.censor();
27        }
28    }
29}
30
31impl Censor for wings_api::Config {
32    fn censor(&mut self) {
33        self.token_id = CENSORED_PLACEHOLDER.into();
34        self.token = CENSORED_PLACEHOLDER.into();
35    }
36}
37
38impl Censor for db_agent_api::Config {
39    fn censor(&mut self) {
40        self.api.token = CENSORED_PLACEHOLDER.into();
41    }
42}