-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: allow users to specify scopes (#37)
- Loading branch information
Showing
5 changed files
with
147 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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 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,99 @@ | ||
use std::ops::{BitOr, BitOrAssign}; | ||
|
||
/// Scopes bitflags for an [`Osu`] client. | ||
/// | ||
/// To specify multiple scopes, create a union using the `|` operator. | ||
/// | ||
/// See <https://osu.ppy.sh/docs/index.html#scopes>. | ||
/// | ||
/// [`Osu`]: crate::Osu | ||
#[derive(Copy, Clone, PartialEq, Eq)] | ||
pub struct Scopes(u16); | ||
|
||
macro_rules! define_scopes { | ||
( $( | ||
#[doc = $doc:literal] | ||
$scope:ident: $shift:literal, $str:literal; | ||
)* ) => { | ||
define_scopes! {@ $( | ||
#[doc = $doc] | ||
$scope: 1 << $shift, $str; | ||
)* } | ||
}; | ||
(@ $( | ||
#[doc = $doc:literal] | ||
$scope:ident: $bit:expr, $str:literal; | ||
)* ) => { | ||
$( | ||
#[allow(non_upper_case_globals)] | ||
impl Scopes { | ||
#[doc = $doc] | ||
pub const $scope: Self = Self($bit); | ||
} | ||
)* | ||
|
||
impl Scopes { | ||
const fn contains(self, bit: u16) -> bool { | ||
(self.0 & bit) > 0 | ||
} | ||
|
||
pub(crate) fn format(self, s: &mut String, separator: char) { | ||
let mut first_scope = true; | ||
|
||
$( | ||
if self.contains($bit) { | ||
if !first_scope { | ||
s.push(separator); | ||
} | ||
|
||
s.push_str($str); | ||
|
||
#[allow(unused_assignments)] | ||
{ | ||
first_scope = false; | ||
} | ||
} | ||
)* | ||
} | ||
} | ||
}; | ||
} | ||
|
||
define_scopes! { | ||
/// Allows reading chat messages on a user's behalf. | ||
ChatRead: 0, "chat.read"; | ||
/// Allows sending chat messages on a user's behalf. | ||
ChatWrite: 1, "chat.write"; | ||
/// Allows joining and leaving chat channels on a user's behalf. | ||
ChatWriteManage: 2, "chat.write_manage"; | ||
/// Allows acting as the owner of a client. | ||
Delegate: 3, "delegate"; | ||
/// Allows creating and editing forum posts on a user's behalf. | ||
ForumWrite: 4, "forum.write"; | ||
/// Allows reading of the user's friend list. | ||
FriendsRead: 5, "friends.read"; | ||
/// Allows reading of the public profile of the user. | ||
Identify: 6, "identify"; | ||
/// Allows reading of publicly available data on behalf of the user. | ||
Public: 7, "public"; | ||
} | ||
|
||
impl Default for Scopes { | ||
fn default() -> Self { | ||
Self::Public | ||
} | ||
} | ||
|
||
impl BitOr for Scopes { | ||
type Output = Self; | ||
|
||
fn bitor(self, rhs: Self) -> Self::Output { | ||
Self(self.0.bitor(rhs.0)) | ||
} | ||
} | ||
|
||
impl BitOrAssign for Scopes { | ||
fn bitor_assign(&mut self, rhs: Self) { | ||
self.0.bitor_assign(rhs.0); | ||
} | ||
} |
This file contains 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 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