-
-
Notifications
You must be signed in to change notification settings - Fork 155
feat(cache,http,model)!: add AfkTimeout for Guild::afk_timeout
#1922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::time::Duration; | ||
|
|
||
| /// Duration of a user being AFK before being timed out from a voice channel. | ||
| /// | ||
| /// This value is configured [for guilds][`Guild::afk_timeout`]. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use twilight_model::guild::AfkTimeout; | ||
| /// | ||
| /// assert_eq!(300, AfkTimeout::FIVE_MINUTES); | ||
| /// ``` | ||
| /// | ||
| /// [`Guild::afk_timeout`]: super::Guild::afk_timeout | ||
| #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] | ||
| #[non_exhaustive] | ||
| pub struct AfkTimeout(u16); | ||
|
|
||
| impl AfkTimeout { | ||
| /// AFK timeout of one minute. | ||
| pub const ONE_MINUTE: Self = Self(60); | ||
|
|
||
| /// AFK timeout of five minutes. | ||
| pub const FIVE_MINUTES: Self = Self(300); | ||
|
|
||
| /// AFK timeout of fifteen minutes. | ||
| pub const FIFTEEN_MINUTES: Self = Self(900); | ||
|
|
||
| /// AFK timeout of thirty minutes. | ||
| pub const THIRTY_MINUTES: Self = Self(1800); | ||
|
|
||
| /// AFK timeout of one hour. | ||
| pub const ONE_HOUR: Self = Self(3600); | ||
|
|
||
| /// Retrieve the duration of the AFK timeout in seconds. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use twilight_model::guild::AfkTimeout; | ||
| /// | ||
| /// assert_eq!(60, AfkTimeout::ONE_MINUTE.get()); | ||
| /// ``` | ||
| pub const fn get(self) -> u16 { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| impl From<u16> for AfkTimeout { | ||
| fn from(value: u16) -> Self { | ||
| Self(value) | ||
| } | ||
| } | ||
|
|
||
| impl From<AfkTimeout> for Duration { | ||
| fn from(value: AfkTimeout) -> Self { | ||
| Self::from_secs(u64::from(value.get())) | ||
| } | ||
| } | ||
|
|
||
| impl PartialEq<u16> for AfkTimeout { | ||
| fn eq(&self, other: &u16) -> bool { | ||
| self.get() == *other | ||
| } | ||
| } | ||
|
|
||
| impl PartialEq<AfkTimeout> for u16 { | ||
| fn eq(&self, other: &AfkTimeout) -> bool { | ||
| *self == other.get() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::AfkTimeout; | ||
| use serde::{Deserialize, Serialize}; | ||
| use serde_test::Token; | ||
| use static_assertions::assert_impl_all; | ||
| use std::{fmt::Debug, hash::Hash, time::Duration}; | ||
|
|
||
| assert_impl_all!( | ||
| AfkTimeout: Clone, | ||
| Copy, | ||
| Debug, | ||
| Deserialize<'static>, | ||
| Eq, | ||
| Hash, | ||
| PartialEq, | ||
| Send, | ||
| Serialize, | ||
| Sync | ||
| ); | ||
|
|
||
| const MAP: &[(AfkTimeout, u16)] = &[ | ||
| (AfkTimeout::ONE_MINUTE, 60), | ||
| (AfkTimeout::FIVE_MINUTES, 300), | ||
| (AfkTimeout::FIFTEEN_MINUTES, 900), | ||
| (AfkTimeout::THIRTY_MINUTES, 1800), | ||
| (AfkTimeout::ONE_HOUR, 3600), | ||
| ]; | ||
|
|
||
| #[test] | ||
| fn serde() { | ||
| for (value, seconds) in MAP { | ||
| serde_test::assert_tokens( | ||
| value, | ||
| &[ | ||
| Token::NewtypeStruct { name: "AfkTimeout" }, | ||
| Token::U16(*seconds), | ||
| ], | ||
| ); | ||
| assert_eq!(*value, AfkTimeout::from(*seconds)); | ||
| assert_eq!(*seconds, value.get()); | ||
| } | ||
| } | ||
|
|
||
| /// Test two-way equality implementation. | ||
| #[test] | ||
| fn eq() { | ||
| assert_eq!(300, AfkTimeout::FIVE_MINUTES); | ||
| assert_eq!(AfkTimeout::FIVE_MINUTES, 300); | ||
| } | ||
|
|
||
| /// Test conversion to [`std::time::Duration`]. | ||
| #[test] | ||
| fn std_time_duration() { | ||
| for (kind, _) in MAP { | ||
| let std_duration = Duration::from(*kind); | ||
| assert_eq!(u64::from(kind.get()), std_duration.as_secs()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just highlighting that this allows any value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine since it's non exaustive and we have no guarantee discord won't add any extra values later