1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
use bitflags::bitflags;

bitflags! {
    /// A set of bitflags which can be used to specify what resource to process
    /// into the cache.
    ///
    /// For example, specifying [`CHANNEL`] but not [`MESSAGE`] will cache
    /// created channels, channel updates, and channel deletes, but not their
    /// messages.
    pub struct ResourceType: u64 {
        /// Information relating to channels.
        const CHANNEL = 1;
        /// Information relating to emojis.
        const EMOJI = 1 << 1;
        /// Information relating to guilds.
        const GUILD = 1 << 2;
        /// Information relating to members.
        const MEMBER = 1 << 3;
        /// Information relating to messages.
        const MESSAGE = 1 << 4;
        /// Information relating to presences.
        const PRESENCE = 1 << 5;
        /// Information relating to reactions.
        const REACTION = 1 << 6;
        /// Information relating to roles.
        const ROLE = 1 << 7;
        /// Information relating the current user.
        const USER_CURRENT = 1 << 8;
        /// Information relating to users.
        const USER = 1 << 9;
        /// Information relating to voice states.
        const VOICE_STATE = 1 << 10;
        /// Information relating to stage instances.
        const STAGE_INSTANCE = 1 << 11;
        /// Information relating to guild integrations.
        const INTEGRATION = 1 << 12;
        /// Information relating to guild stickers.
        const STICKER = 1 << 13;
    }
}

/// Configuration for an [`InMemoryCache`].
///
/// [`InMemoryCache`]: crate::InMemoryCache
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Config {
    pub(super) resource_types: ResourceType,
    pub(super) message_cache_size: usize,
}

impl Config {
    /// Create a new default configuration.
    ///
    /// Refer to individual getters for their defaults.
    pub const fn new() -> Self {
        Self {
            resource_types: ResourceType::all(),
            message_cache_size: 100,
        }
    }

    /// Returns an immutable reference to the message cache size.
    ///
    /// Defaults to 100.
    pub const fn message_cache_size(&self) -> usize {
        self.message_cache_size
    }

    /// Returns a mutable reference to the message cache size.
    pub fn message_cache_size_mut(&mut self) -> &mut usize {
        &mut self.message_cache_size
    }
    /// Returns an immutable reference to the resource types enabled.
    ///
    /// Defaults to all resource types.
    pub const fn resource_types(&self) -> ResourceType {
        self.resource_types
    }

    /// Returns a mutable reference to the resource types enabled.
    pub fn resource_types_mut(&mut self) -> &mut ResourceType {
        &mut self.resource_types
    }
}

impl Default for Config {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::{Config, ResourceType};
    use static_assertions::assert_fields;

    assert_fields!(Config: resource_types, message_cache_size);

    #[test]
    fn test_defaults() {
        let conf = Config {
            resource_types: ResourceType::all(),
            message_cache_size: 100,
        };
        let default = Config::default();
        assert_eq!(conf.resource_types, default.resource_types);
        assert_eq!(conf.message_cache_size, default.message_cache_size);
    }
}