diff options
Diffstat (limited to 'kittybox-rs/indieauth/src/scopes.rs')
-rw-r--r-- | kittybox-rs/indieauth/src/scopes.rs | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/kittybox-rs/indieauth/src/scopes.rs b/kittybox-rs/indieauth/src/scopes.rs index 18ebfbd..ae039a6 100644 --- a/kittybox-rs/indieauth/src/scopes.rs +++ b/kittybox-rs/indieauth/src/scopes.rs @@ -9,6 +9,7 @@ use serde::{ } }; +/// Various scopes that can be requested through IndieAuth. #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum Scope { @@ -38,6 +39,7 @@ pub enum Scope { Custom(String) } impl Scope { + /// Create a custom scope from a string slice. pub fn custom(scope: &str) -> Scope { Scope::Custom(scope.to_string()) } @@ -81,20 +83,28 @@ impl From<&str> for Scope { } } } + +/// A list of scopes that serializes to a space-separated string instead of a list. +/// +/// OAuth2 is weird, don't ask me why it's a thing. #[derive(PartialEq, Eq, Debug, Clone)] pub struct Scopes(Vec<Scope>); impl Scopes { + /// Create a list of scopes from a vector of scopes. pub fn new(scopes: Vec<Scope>) -> Self { Self(scopes) } + /// Ensure a certain scope is listed in the scope list. pub fn has(&self, scope: &Scope) -> bool { self.0.iter().any(|s| s == scope) } + /// Ensure all of the requested scopes are in the list. pub fn has_all(&self, scopes: &[Scope]) -> bool { scopes.iter() .map(|s1| self.iter().any(|s2| s1 == s2)) .all(|s| s) } + /// Transform this into an iterator over individual scopes. pub fn iter(&self) -> std::slice::Iter<'_, Scope> { self.0.iter() } |