diff --git a/src/apps.rs b/src/apps.rs index 2607a21..c1bd06e 100644 --- a/src/apps.rs +++ b/src/apps.rs @@ -1,7 +1,8 @@ -use std::{borrow::Cow, fmt}; +use std::borrow::Cow; use try_from::TryInto; +use scopes::Scopes; use errors::{Error, Result}; /// Represents an application that can be registered with a mastodon instance @@ -144,59 +145,6 @@ impl<'a> TryInto for AppBuilder<'a> { } } -/// Permission scope of the application. -/// [Details on what each permission provides][1] -/// [1]: https://github.com/tootsuite/documentation/blob/master/Using-the-API/OAuth-details.md) -#[derive(Debug, Clone, Copy, PartialEq, Serialize)] -pub enum Scopes { - /// All Permissions, equivalent to `read write follow` - #[serde(rename = "read write follow")] - All, - /// Only permission to add and remove followers. - #[serde(rename = "follow")] - Follow, - /// Read only permissions. - #[serde(rename = "read")] - Read, - /// Read & Follow permissions. - #[serde(rename = "read follow")] - ReadFollow, - /// Read & Write permissions. - #[serde(rename = "read write")] - ReadWrite, - /// Write only permissions. - #[serde(rename = "write")] - Write, - /// Write & Follow permissions. - #[serde(rename = "write follow")] - WriteFollow, -} - -impl fmt::Display for Scopes { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Scopes::*; - write!( - f, - "{}", - match *self { - All => "read%20write%20follow", - Follow => "follow", - Read => "read", - ReadFollow => "read%20follow", - ReadWrite => "read%20write", - Write => "write", - WriteFollow => "write%20follow", - } - ) - } -} - -impl Default for Scopes { - fn default() -> Self { - Scopes::Read - } -} - #[cfg(test)] mod tests { use super::*; @@ -283,39 +231,4 @@ mod tests { assert_eq!(expected, result); } - #[test] - fn test_scopes_display() { - let values = [ - Scopes::All, - Scopes::Follow, - Scopes::Read, - Scopes::ReadFollow, - Scopes::ReadWrite, - Scopes::Write, - Scopes::WriteFollow, - ]; - - let expecteds = [ - "read%20write%20follow".to_string(), - "follow".to_string(), - "read".to_string(), - "read%20follow".to_string(), - "read%20write".to_string(), - "write".to_string(), - "write%20follow".to_string(), - ]; - - let tests = values.into_iter().zip(expecteds.into_iter()); - - for (value, expected) in tests { - let result = value.to_string(); - assert_eq!(&result, expected); - } - } - - #[test] - fn test_scopes_default() { - let default: Scopes = Default::default(); - assert_eq!(default, Scopes::Read); - } } diff --git a/src/lib.rs b/src/lib.rs index 4899180..1fb1da1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,13 +102,15 @@ pub mod page; pub mod registration; /// Requests pub mod requests; +/// OAuth Scopes +pub mod scopes; /// Constructing a status pub mod status_builder; #[macro_use] mod macros; /// Automatically import the things you need pub mod prelude { - pub use apps::Scopes; + pub use scopes::Scopes; pub use Data; pub use Mastodon; pub use MastodonClient; diff --git a/src/registration.rs b/src/registration.rs index af47b37..2a5f8d7 100644 --- a/src/registration.rs +++ b/src/registration.rs @@ -3,7 +3,8 @@ use std::borrow::Cow; use reqwest::{Client, RequestBuilder, Response}; use try_from::TryInto; -use apps::{App, AppBuilder, Scopes}; +use apps::{App, AppBuilder}; +use scopes::Scopes; use http_send::{HttpSend, HttpSender}; use Data; use Error; diff --git a/src/scopes.rs b/src/scopes.rs new file mode 100644 index 0000000..282d1f9 --- /dev/null +++ b/src/scopes.rs @@ -0,0 +1,95 @@ +use std::fmt; + +/// Permission scope of the application. +/// [Details on what each permission provides][1] +/// [1]: https://github.com/tootsuite/documentation/blob/master/Using-the-API/OAuth-details.md) +#[derive(Debug, Clone, Copy, PartialEq, Hash, Serialize)] +pub enum Scopes { + /// All Permissions, equivalent to `read write follow` + #[serde(rename = "read write follow")] + All, + /// Only permission to add and remove followers. + #[serde(rename = "follow")] + Follow, + /// Read only permissions. + #[serde(rename = "read")] + Read, + /// Read & Follow permissions. + #[serde(rename = "read follow")] + ReadFollow, + /// Read & Write permissions. + #[serde(rename = "read write")] + ReadWrite, + /// Write only permissions. + #[serde(rename = "write")] + Write, + /// Write & Follow permissions. + #[serde(rename = "write follow")] + WriteFollow, +} + +impl fmt::Display for Scopes { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Scopes::*; + write!( + f, + "{}", + match *self { + All => "read%20write%20follow", + Follow => "follow", + Read => "read", + ReadFollow => "read%20follow", + ReadWrite => "read%20write", + Write => "write", + WriteFollow => "write%20follow", + } + ) + } +} + +impl Default for Scopes { + fn default() -> Self { + Scopes::Read + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_scopes_display() { + let values = [ + Scopes::All, + Scopes::Follow, + Scopes::Read, + Scopes::ReadFollow, + Scopes::ReadWrite, + Scopes::Write, + Scopes::WriteFollow, + ]; + + let expecteds = [ + "read%20write%20follow".to_string(), + "follow".to_string(), + "read".to_string(), + "read%20follow".to_string(), + "read%20write".to_string(), + "write".to_string(), + "write%20follow".to_string(), + ]; + + let tests = values.into_iter().zip(expecteds.into_iter()); + + for (value, expected) in tests { + let result = value.to_string(); + assert_eq!(&result, expected); + } + } + + #[test] + fn test_scopes_default() { + let default: Scopes = Default::default(); + assert_eq!(default, Scopes::Read); + } +}