Skip to content

Instantly share code, notes, and snippets.

@sethlopezme
Last active August 7, 2017 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sethlopezme/be69b56d2948d97bdd5b62807318f5d4 to your computer and use it in GitHub Desktop.
Save sethlopezme/be69b56d2948d97bdd5b62807318f5d4 to your computer and use it in GitHub Desktop.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Scope {
Any,
Account,
Edit,
// ...
}
impl fmt::Display for Scope {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Scope::Any => write!(f, "*"),
Scope::Account => write!(f, "account"),
Scope::Edit => write!(f, "edit"),
// ...
}
}
}
#[derive(Debug, Default)]
pub struct Scopes(HashSet<Scope>);
impl Serialize for Scopes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let scope_string = self.0.iter().fold(String::new(), |mut accumulator, scope| {
if !accumulator.is_empty() {
accumulator.push(' ');
}
accumulator + scope.to_string().as_str()
});
serializer.serialize_str(scope_string.as_str())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment