Skip to main content

djs_cache/
config.rs

1//! What the cache keeps: one [`Limit`] per [`Resource`].
2
3use core::{fmt, time::Duration};
4
5/// A kind of Discord resource the cache can keep.
6///
7/// Each resource has its own [`Store`](crate::Store) and its own [`Limit`] in the
8/// [`CacheConfig`]. A [`Limit::Max`] counts in the resource's scope, the same as the
9/// corresponding discord.js manager: over the whole cache, per guild or per channel (D-159).
10///
11/// discord.js counterpart: the managers passed to `makeCache` (`GuildManager`,
12/// `GuildMemberManager`, `MessageManager`, …).
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
14#[non_exhaustive]
15pub enum Resource {
16    /// Guilds. Limit counted over the whole cache. Cached by default.
17    ///
18    /// discord.js: `GuildManager` (`client.guilds`).
19    Guild,
20    /// Guild channels and DM channels, without threads. Limit counted over the whole cache.
21    /// Cached by default.
22    ///
23    /// discord.js: `ChannelManager` (`client.channels`).
24    Channel,
25    /// Threads. Limit counted per guild.
26    ///
27    /// discord.js: `GuildTextThreadManager`, `GuildForumThreadManager`.
28    Thread,
29    /// Roles. Limit counted per guild. Cached by default.
30    ///
31    /// discord.js: `RoleManager` (`guild.roles`).
32    Role,
33    /// Custom guild emojis. Limit counted per guild.
34    ///
35    /// discord.js: `GuildEmojiManager` (`guild.emojis`).
36    Emoji,
37    /// Guild stickers. Limit counted per guild.
38    ///
39    /// discord.js: `GuildStickerManager` (`guild.stickers`).
40    Sticker,
41    /// Guild members. Limit counted per guild.
42    ///
43    /// discord.js: `GuildMemberManager` (`guild.members`).
44    Member,
45    /// Users. Limit counted over the whole cache.
46    ///
47    /// discord.js: `UserManager` (`client.users`).
48    User,
49    /// Messages. Limit counted per channel.
50    ///
51    /// discord.js: `MessageManager` (`channel.messages`).
52    Message,
53    /// Presences (status and activities of members). Limit counted per guild.
54    ///
55    /// discord.js: `PresenceManager` (`guild.presences`).
56    Presence,
57    /// Voice states. Limit counted per guild.
58    ///
59    /// discord.js: `VoiceStateManager` (`guild.voiceStates`).
60    VoiceState,
61    /// Scheduled events. Limit counted per guild.
62    ///
63    /// discord.js: `GuildScheduledEventManager` (`guild.scheduledEvents`).
64    ScheduledEvent,
65    /// Stage instances. Limit counted per guild.
66    ///
67    /// discord.js: `StageInstanceManager` (`guild.stageInstances`).
68    StageInstance,
69    /// Guild soundboard sounds. Limit counted per guild.
70    ///
71    /// discord.js: `GuildSoundboardSoundManager` (`guild.soundboardSounds`).
72    SoundboardSound,
73}
74
75impl Resource {
76    /// Every resource, in declaration order.
77    pub const ALL: &'static [Self] = &[
78        Self::Guild,
79        Self::Channel,
80        Self::Thread,
81        Self::Role,
82        Self::Emoji,
83        Self::Sticker,
84        Self::Member,
85        Self::User,
86        Self::Message,
87        Self::Presence,
88        Self::VoiceState,
89        Self::ScheduledEvent,
90        Self::StageInstance,
91        Self::SoundboardSound,
92    ];
93
94    /// The number of resources, the length of [`CacheConfig`]'s limit table.
95    const COUNT: usize = Self::ALL.len();
96
97    /// What a [`Limit::Max`] of this resource counts in (D-159).
98    pub(crate) const fn scope(self) -> Scope {
99        match self {
100            Self::Guild | Self::Channel | Self::User => Scope::Cache,
101            Self::Message => Scope::Channel,
102            Self::Thread
103            | Self::Role
104            | Self::Emoji
105            | Self::Sticker
106            | Self::Member
107            | Self::Presence
108            | Self::VoiceState
109            | Self::ScheduledEvent
110            | Self::StageInstance
111            | Self::SoundboardSound => Scope::Guild,
112        }
113    }
114
115    /// The position of this resource in [`Self::ALL`].
116    const fn index(self) -> usize {
117        self as usize
118    }
119}
120
121/// What a [`Limit::Max`] counts in (D-159).
122#[derive(Clone, Copy, Debug, PartialEq, Eq)]
123pub(crate) enum Scope {
124    /// All entries of the resource.
125    Cache,
126    /// The entries of one guild.
127    Guild,
128    /// The entries of one channel.
129    Channel,
130}
131
132/// Whether a [`Resource`] is cached, and how many of its entries are kept.
133///
134/// A limit counts in the resource's scope (see [`Resource`]): `Limit::Max(100)` for
135/// [`Resource::Member`] keeps up to 100 members in every guild, for [`Resource::Message`] up
136/// to 100 messages in every channel. When a scope is full, inserting a new entry evicts the
137/// one inserted first (D-157); updating an entry that is already cached does not change its
138/// place.
139///
140/// discord.js counterpart: the numbers passed to `Options.cacheWithLimits` (`0` disables a
141/// manager's cache, no entry means unlimited).
142///
143/// # Examples
144///
145/// ```
146/// use djs_cache::Limit;
147///
148/// assert!(Limit::Max(200).is_enabled());
149/// assert_eq!(Limit::Max(200).max(), Some(200));
150/// assert!(!Limit::Disabled.is_enabled());
151/// // A maximum of zero keeps nothing; the configuration stores it as `Disabled`.
152/// assert!(!Limit::Max(0).is_enabled());
153/// assert_eq!(Limit::Unlimited.max(), None);
154/// ```
155#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
156#[non_exhaustive]
157pub enum Limit {
158    /// The resource is not cached.
159    Disabled,
160    /// At most this many entries per scope. `Max(0)` is the same as [`Limit::Disabled`].
161    Max(usize),
162    /// Every entry is kept.
163    Unlimited,
164}
165
166impl Limit {
167    /// Returns whether the resource is cached at all.
168    ///
169    /// discord.js counterpart: none (a manager's cache always exists in discord.js).
170    #[must_use]
171    pub const fn is_enabled(self) -> bool {
172        !matches!(self, Self::Disabled | Self::Max(0))
173    }
174
175    /// Returns the maximum number of entries per scope, or `None` if there is no maximum
176    /// ([`Limit::Unlimited`]) or nothing is cached ([`Limit::Disabled`]).
177    ///
178    /// discord.js counterpart: the `maxSize` of `LimitedCollection`.
179    #[must_use]
180    pub const fn max(self) -> Option<usize> {
181        match self {
182            Self::Max(0) | Self::Disabled | Self::Unlimited => None,
183            Self::Max(max) => Some(max),
184        }
185    }
186
187    /// `Max(0)` becomes `Disabled`, so equal settings compare equal.
188    const fn normalized(self) -> Self {
189        match self {
190            Self::Max(0) => Self::Disabled,
191            other => other,
192        }
193    }
194}
195
196/// Which resources the cache keeps, and how many entries of each.
197///
198/// [`CacheConfig::default`] keeps only guilds, channels and roles (and always the current
199/// user), which covers most bots at the lowest cost (D-152). [`CacheConfig::discord_js`]
200/// keeps what discord.js keeps by default. Customise either with [`CacheConfig::builder`] or
201/// [`CacheConfigBuilder::from`].
202///
203/// discord.js counterpart: the `makeCache` client option.
204///
205/// # Examples
206///
207/// ```
208/// use djs_cache::{CacheConfig, Limit, Resource};
209///
210/// let config = CacheConfig::builder()
211///     .members(Limit::Unlimited)
212///     .messages(Limit::Max(50))
213///     .build();
214/// assert_eq!(config.limit(Resource::Member), Limit::Unlimited);
215/// assert_eq!(config.limit(Resource::Message), Limit::Max(50));
216/// // Not mentioned: the default.
217/// assert_eq!(config.limit(Resource::Guild), Limit::Unlimited);
218/// assert_eq!(config.limit(Resource::Presence), Limit::Disabled);
219/// ```
220#[derive(Clone, PartialEq, Eq, Hash)]
221pub struct CacheConfig {
222    limits: [Limit; Resource::COUNT],
223    sweepers: SweepConfig,
224}
225
226impl CacheConfig {
227    /// Returns a builder that starts from the [default](Self::default) configuration.
228    ///
229    /// discord.js counterpart: `Options.cacheWithLimits`.
230    pub fn builder() -> CacheConfigBuilder {
231        CacheConfigBuilder {
232            config: Self::default(),
233        }
234    }
235
236    /// Returns discord.js' default cache: every resource without a limit, except messages,
237    /// which keep the latest 200 per channel.
238    ///
239    /// discord.js counterpart: `Options.cacheWithLimits(Options.DefaultMakeCacheSettings)`.
240    ///
241    /// # Examples
242    ///
243    /// ```
244    /// use djs_cache::{CacheConfig, Limit, Resource};
245    ///
246    /// let config = CacheConfig::discord_js();
247    /// assert_eq!(config.limit(Resource::Message), Limit::Max(200));
248    /// assert_eq!(config.limit(Resource::Presence), Limit::Unlimited);
249    /// ```
250    #[must_use]
251    pub fn discord_js() -> Self {
252        let mut config = Self::uniform(Limit::Unlimited);
253        config.set(Resource::Message, Limit::Max(200));
254        config
255    }
256
257    /// Returns the limit of `resource`.
258    ///
259    /// discord.js counterpart: a manager's entry in `Options.cacheWithLimits`.
260    #[must_use]
261    pub const fn limit(&self, resource: Resource) -> Limit {
262        self.limits[resource.index()]
263    }
264
265    /// Returns whether `resource` is cached.
266    ///
267    /// discord.js counterpart: none (a manager's cache always exists in discord.js).
268    #[must_use]
269    pub const fn is_enabled(&self, resource: Resource) -> bool {
270        self.limit(resource).is_enabled()
271    }
272
273    /// Returns which sweeps run periodically (D-167). The cache itself runs nothing: the
274    /// djs-rs `Client` runs them, or call the [`Cache`](crate::Cache) sweep methods yourself.
275    ///
276    /// discord.js counterpart: the `sweepers` option of `ClientOptions`.
277    #[must_use]
278    pub const fn sweepers(&self) -> &SweepConfig {
279        &self.sweepers
280    }
281
282    const fn uniform(limit: Limit) -> Self {
283        Self {
284            limits: [limit; Resource::COUNT],
285            sweepers: SweepConfig::DEFAULT,
286        }
287    }
288
289    const fn set(&mut self, resource: Resource, limit: Limit) {
290        self.limits[resource.index()] = limit.normalized();
291    }
292}
293
294impl Default for CacheConfig {
295    /// Guilds, channels and roles without a limit; everything else disabled (D-152).
296    fn default() -> Self {
297        let mut config = Self::uniform(Limit::Disabled);
298        for resource in [Resource::Guild, Resource::Channel, Resource::Role] {
299            config.set(resource, Limit::Unlimited);
300        }
301        config
302    }
303}
304
305impl fmt::Debug for CacheConfig {
306    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
307        struct Limits<'a>(&'a CacheConfig);
308
309        impl fmt::Debug for Limits<'_> {
310            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
311                f.debug_map()
312                    .entries(
313                        Resource::ALL
314                            .iter()
315                            .map(|&resource| (resource, self.0.limit(resource))),
316                    )
317                    .finish()
318            }
319        }
320
321        f.debug_struct("CacheConfig")
322            .field("limits", &Limits(self))
323            .field("sweepers", &self.sweepers)
324            .finish()
325    }
326}
327
328/// Builds a [`CacheConfig`]. Created by [`CacheConfig::builder`], or from an existing
329/// configuration with [`CacheConfigBuilder::from`].
330///
331/// Every method sets the [`Limit`] of one [`Resource`]; resources not mentioned keep the
332/// starting configuration's limit.
333///
334/// # Examples
335///
336/// Start from discord.js' defaults and turn presences off:
337///
338/// ```
339/// use djs_cache::{CacheConfig, CacheConfigBuilder, Limit, Resource};
340///
341/// let config = CacheConfigBuilder::from(CacheConfig::discord_js())
342///     .presences(Limit::Disabled)
343///     .build();
344/// assert!(!config.is_enabled(Resource::Presence));
345/// assert!(config.is_enabled(Resource::Member));
346/// ```
347#[derive(Clone, Debug)]
348#[must_use = "a builder does nothing until `build` is called"]
349pub struct CacheConfigBuilder {
350    config: CacheConfig,
351}
352
353impl CacheConfigBuilder {
354    /// Sets the limit of any resource.
355    ///
356    /// discord.js counterpart: a manager's entry in `Options.cacheWithLimits`.
357    pub const fn limit(mut self, resource: Resource, limit: Limit) -> Self {
358        self.config.set(resource, limit);
359        self
360    }
361
362    /// Sets the limit of [`Resource::Guild`] (default [`Limit::Unlimited`]).
363    ///
364    /// discord.js counterpart: the `GuildManager` entry of `Options.cacheWithLimits`.
365    pub const fn guilds(self, limit: Limit) -> Self {
366        self.limit(Resource::Guild, limit)
367    }
368
369    /// Sets the limit of [`Resource::Channel`] (default [`Limit::Unlimited`]).
370    ///
371    /// discord.js counterpart: the `ChannelManager` entry of `Options.cacheWithLimits`.
372    pub const fn channels(self, limit: Limit) -> Self {
373        self.limit(Resource::Channel, limit)
374    }
375
376    /// Sets the limit of [`Resource::Thread`], per guild (default [`Limit::Disabled`]).
377    ///
378    /// discord.js counterpart: the `ThreadManager` entry of `Options.cacheWithLimits`.
379    pub const fn threads(self, limit: Limit) -> Self {
380        self.limit(Resource::Thread, limit)
381    }
382
383    /// Sets the limit of [`Resource::Role`], per guild (default [`Limit::Unlimited`]).
384    ///
385    /// discord.js counterpart: the `RoleManager` entry of `Options.cacheWithLimits`.
386    pub const fn roles(self, limit: Limit) -> Self {
387        self.limit(Resource::Role, limit)
388    }
389
390    /// Sets the limit of [`Resource::Emoji`], per guild (default [`Limit::Disabled`]).
391    ///
392    /// discord.js counterpart: the `GuildEmojiManager` entry of `Options.cacheWithLimits`.
393    pub const fn emojis(self, limit: Limit) -> Self {
394        self.limit(Resource::Emoji, limit)
395    }
396
397    /// Sets the limit of [`Resource::Sticker`], per guild (default [`Limit::Disabled`]).
398    ///
399    /// discord.js counterpart: the `GuildStickerManager` entry of `Options.cacheWithLimits`.
400    pub const fn stickers(self, limit: Limit) -> Self {
401        self.limit(Resource::Sticker, limit)
402    }
403
404    /// Sets the limit of [`Resource::Member`], per guild (default [`Limit::Disabled`]).
405    ///
406    /// discord.js counterpart: the `GuildMemberManager` entry of `Options.cacheWithLimits`.
407    pub const fn members(self, limit: Limit) -> Self {
408        self.limit(Resource::Member, limit)
409    }
410
411    /// Sets the limit of [`Resource::User`] (default [`Limit::Disabled`]).
412    ///
413    /// discord.js counterpart: the `UserManager` entry of `Options.cacheWithLimits`.
414    pub const fn users(self, limit: Limit) -> Self {
415        self.limit(Resource::User, limit)
416    }
417
418    /// Sets the limit of [`Resource::Message`], per channel (default [`Limit::Disabled`]).
419    ///
420    /// discord.js counterpart: the `MessageManager` entry of `Options.cacheWithLimits`.
421    pub const fn messages(self, limit: Limit) -> Self {
422        self.limit(Resource::Message, limit)
423    }
424
425    /// Sets the limit of [`Resource::Presence`], per guild (default [`Limit::Disabled`]).
426    ///
427    /// discord.js counterpart: the `PresenceManager` entry of `Options.cacheWithLimits`.
428    pub const fn presences(self, limit: Limit) -> Self {
429        self.limit(Resource::Presence, limit)
430    }
431
432    /// Sets the limit of [`Resource::VoiceState`], per guild (default [`Limit::Disabled`]).
433    ///
434    /// discord.js counterpart: the `VoiceStateManager` entry of `Options.cacheWithLimits`.
435    pub const fn voice_states(self, limit: Limit) -> Self {
436        self.limit(Resource::VoiceState, limit)
437    }
438
439    /// Sets the limit of [`Resource::ScheduledEvent`], per guild (default
440    /// [`Limit::Disabled`]).
441    ///
442    /// discord.js counterpart: the `GuildScheduledEventManager` entry of `Options.cacheWithLimits`.
443    pub const fn scheduled_events(self, limit: Limit) -> Self {
444        self.limit(Resource::ScheduledEvent, limit)
445    }
446
447    /// Sets the limit of [`Resource::StageInstance`], per guild (default
448    /// [`Limit::Disabled`]).
449    ///
450    /// discord.js counterpart: the `StageInstanceManager` entry of `Options.cacheWithLimits`.
451    pub const fn stage_instances(self, limit: Limit) -> Self {
452        self.limit(Resource::StageInstance, limit)
453    }
454
455    /// Sets the limit of [`Resource::SoundboardSound`], per guild (default
456    /// [`Limit::Disabled`]).
457    ///
458    /// discord.js counterpart: the `GuildSoundboardSoundManager` entry of `Options.cacheWithLimits`.
459    pub const fn soundboard_sounds(self, limit: Limit) -> Self {
460        self.limit(Resource::SoundboardSound, limit)
461    }
462
463    /// Sets the periodic sweep of old messages (default none: messages are bounded by their
464    /// [limit](Self::messages)). See [`Cache::sweep_messages_older_than`](crate::Cache::sweep_messages_older_than).
465    ///
466    /// discord.js counterpart: the `messages` key of the `sweepers` client option.
467    pub const fn sweep_messages(mut self, sweep: Option<Sweep>) -> Self {
468        self.config.sweepers.messages = sweep;
469        self
470    }
471
472    /// Sets the periodic sweep of long-archived threads (default every hour, archived more
473    /// than 4 hours ago). See [`Cache::sweep_archived_threads`](crate::Cache::sweep_archived_threads).
474    ///
475    /// discord.js counterpart: the `threads` key of the `sweepers` client option.
476    pub const fn sweep_threads(mut self, sweep: Option<Sweep>) -> Self {
477        self.config.sweepers.threads = sweep;
478        self
479    }
480
481    /// Builds the configuration.
482    ///
483    /// discord.js counterpart: `Options.cacheWithLimits`.
484    #[must_use]
485    pub fn build(self) -> CacheConfig {
486        self.config
487    }
488}
489
490/// A periodic sweep: every `interval`, remove the entries older than `lifetime` (D-167).
491///
492/// discord.js counterpart: `SweepOptions` with `interval` and `filter:
493/// Sweepers.filterByLifetime({ lifetime })`.
494///
495/// # Examples
496///
497/// ```
498/// use std::time::Duration;
499///
500/// use djs_cache::{CacheConfig, Sweep};
501///
502/// // Every 10 minutes, sweep messages older than 30 minutes.
503/// let config = CacheConfig::builder()
504///     .sweep_messages(Some(Sweep::new(
505///         Duration::from_secs(600),
506///         Duration::from_secs(1_800),
507///     )))
508///     .build();
509/// assert_eq!(config.sweepers().messages.unwrap().lifetime, Duration::from_secs(1_800));
510/// ```
511#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
512#[non_exhaustive]
513pub struct Sweep {
514    /// How often the sweep runs.
515    pub interval: Duration,
516    /// How old an entry must be to be swept.
517    pub lifetime: Duration,
518}
519
520impl Sweep {
521    /// Creates a sweep running every `interval` over entries older than `lifetime`.
522    ///
523    /// discord.js counterpart: a `SweeperDefinitions` entry with `interval` and `lifetime`.
524    #[must_use]
525    pub const fn new(interval: Duration, lifetime: Duration) -> Self {
526        Self { interval, lifetime }
527    }
528}
529
530/// The periodic sweeps of a [`CacheConfig`] (D-167, D-169). Set them with
531/// [`CacheConfigBuilder::sweep_messages`] and [`CacheConfigBuilder::sweep_threads`].
532///
533/// discord.js counterpart: the `sweepers` client option.
534#[derive(Clone, Debug, PartialEq, Eq, Hash)]
535#[non_exhaustive]
536pub struct SweepConfig {
537    /// Sweeps messages by age (their last edit, else their creation). Default: none.
538    pub messages: Option<Sweep>,
539    /// Sweeps threads archived for longer than the lifetime. Default: every hour, archived
540    /// more than 4 hours ago, like discord.js.
541    pub threads: Option<Sweep>,
542}
543
544impl SweepConfig {
545    const DEFAULT: Self = Self {
546        messages: None,
547        threads: Some(Sweep::new(
548            Duration::from_secs(60 * 60),
549            Duration::from_secs(4 * 60 * 60),
550        )),
551    };
552}
553
554impl Default for SweepConfig {
555    /// discord.js' defaults (`Options.DefaultSweeperSettings`): archived threads only.
556    fn default() -> Self {
557        Self::DEFAULT
558    }
559}
560
561impl From<CacheConfig> for CacheConfigBuilder {
562    /// Starts a builder from an existing configuration.
563    fn from(config: CacheConfig) -> Self {
564        Self { config }
565    }
566}
567
568#[cfg(test)]
569mod tests {
570    use super::{CacheConfig, Limit, Resource, Scope};
571
572    #[test]
573    fn resource_indexes_match_all() {
574        for (position, resource) in Resource::ALL.iter().enumerate() {
575            assert_eq!(resource.index(), position, "{resource:?}");
576        }
577    }
578
579    #[test]
580    fn the_default_is_minimal() {
581        let config = CacheConfig::default();
582        for &resource in Resource::ALL {
583            let expected = matches!(
584                resource,
585                Resource::Guild | Resource::Channel | Resource::Role
586            );
587            assert_eq!(config.is_enabled(resource), expected, "{resource:?}");
588        }
589        assert_eq!(CacheConfig::builder().build(), config);
590    }
591
592    #[test]
593    fn the_discord_js_preset_limits_only_messages() {
594        let config = CacheConfig::discord_js();
595        for &resource in Resource::ALL {
596            let expected = if resource == Resource::Message {
597                Limit::Max(200)
598            } else {
599                Limit::Unlimited
600            };
601            assert_eq!(config.limit(resource), expected, "{resource:?}");
602        }
603    }
604
605    #[test]
606    fn a_zero_maximum_is_stored_as_disabled() {
607        let config = CacheConfig::builder().guilds(Limit::Max(0)).build();
608        assert_eq!(config.limit(Resource::Guild), Limit::Disabled);
609        assert_eq!(
610            config,
611            CacheConfig::builder().guilds(Limit::Disabled).build()
612        );
613    }
614
615    #[test]
616    fn every_builder_method_sets_its_resource() {
617        let config = CacheConfig::builder()
618            .guilds(Limit::Max(1))
619            .channels(Limit::Max(2))
620            .threads(Limit::Max(3))
621            .roles(Limit::Max(4))
622            .emojis(Limit::Max(5))
623            .stickers(Limit::Max(6))
624            .members(Limit::Max(7))
625            .users(Limit::Max(8))
626            .messages(Limit::Max(9))
627            .presences(Limit::Max(10))
628            .voice_states(Limit::Max(11))
629            .scheduled_events(Limit::Max(12))
630            .stage_instances(Limit::Max(13))
631            .soundboard_sounds(Limit::Max(14))
632            .build();
633        for (position, &resource) in Resource::ALL.iter().enumerate() {
634            assert_eq!(
635                config.limit(resource),
636                Limit::Max(position + 1),
637                "{resource:?}"
638            );
639        }
640    }
641
642    #[test]
643    fn scopes_follow_the_discord_js_managers() {
644        assert_eq!(Resource::Guild.scope(), Scope::Cache);
645        assert_eq!(Resource::User.scope(), Scope::Cache);
646        assert_eq!(Resource::Channel.scope(), Scope::Cache);
647        assert_eq!(Resource::Message.scope(), Scope::Channel);
648        assert_eq!(Resource::Member.scope(), Scope::Guild);
649        assert_eq!(Resource::Presence.scope(), Scope::Guild);
650    }
651
652    #[test]
653    fn debug_lists_every_resource() {
654        let debug = format!("{:?}", CacheConfig::default());
655        assert!(
656            debug.starts_with(
657                "CacheConfig { limits: {Guild: Unlimited, Channel: Unlimited, Thread: Disabled"
658            ),
659            "{debug}"
660        );
661        assert!(debug.contains("SoundboardSound: Disabled}, sweepers: SweepConfig {"));
662    }
663
664    #[test]
665    fn sweeps_default_to_discord_js() {
666        use core::time::Duration;
667
668        use super::{Sweep, SweepConfig};
669
670        let sweepers = CacheConfig::default().sweepers().clone();
671        assert_eq!(sweepers, SweepConfig::default());
672        assert_eq!(sweepers.messages, None);
673        assert_eq!(
674            sweepers.threads,
675            Some(Sweep::new(
676                Duration::from_secs(3_600),
677                Duration::from_secs(14_400)
678            ))
679        );
680        assert_eq!(CacheConfig::discord_js().sweepers(), &sweepers);
681        let off = CacheConfig::builder().sweep_threads(None).build();
682        assert_eq!(off.sweepers().threads, None);
683    }
684}