2022-11-27 14:44:43 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2022-11-29 23:50:29 +00:00
|
|
|
use serde::Serialize;
|
|
|
|
// use try_from::TryInto;
|
2022-11-27 14:44:43 +00:00
|
|
|
|
2022-11-29 23:50:29 +00:00
|
|
|
use crate::{
|
|
|
|
errors::{Error, Result},
|
|
|
|
scopes::Scopes,
|
|
|
|
};
|
2022-11-27 14:44:43 +00:00
|
|
|
|
|
|
|
/// Represents an application that can be registered with a mastodon instance
|
|
|
|
#[derive(Clone, Debug, Default, Serialize, PartialEq)]
|
|
|
|
pub struct App {
|
|
|
|
client_name: String,
|
|
|
|
redirect_uris: String,
|
|
|
|
scopes: Scopes,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
website: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
/// Get an AppBuilder object
|
|
|
|
///
|
2022-11-29 23:50:29 +00:00
|
|
|
/// // Example
|
2022-11-27 14:44:43 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2022-12-22 18:19:49 +00:00
|
|
|
/// use mastodon_async::apps::App;
|
2022-11-27 14:44:43 +00:00
|
|
|
///
|
|
|
|
/// let mut builder = App::builder();
|
|
|
|
/// ```
|
|
|
|
pub fn builder<'a>() -> AppBuilder<'a> {
|
|
|
|
AppBuilder::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the list of scopes that apply to this App
|
|
|
|
///
|
2022-11-29 23:50:29 +00:00
|
|
|
/// // Example
|
2022-11-27 14:44:43 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2022-12-22 18:19:49 +00:00
|
|
|
/// use mastodon_async::{apps::App, scopes::Scopes};
|
2022-11-27 14:44:43 +00:00
|
|
|
///
|
|
|
|
/// let mut builder = App::builder();
|
2022-12-22 18:19:49 +00:00
|
|
|
/// builder.client_name("mastodon-async-test");
|
2022-11-29 23:50:29 +00:00
|
|
|
/// let app = builder.build().unwrap();
|
2022-11-27 14:44:43 +00:00
|
|
|
/// let scopes = app.scopes();
|
|
|
|
/// assert_eq!(scopes, &Scopes::read_all());
|
|
|
|
/// ```
|
|
|
|
pub fn scopes(&self) -> &Scopes {
|
|
|
|
&self.scopes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builder struct for defining your application.
|
|
|
|
/// ```
|
2022-12-22 18:19:49 +00:00
|
|
|
/// use mastodon_async::{apps::App};
|
2022-11-27 14:44:43 +00:00
|
|
|
///
|
|
|
|
/// let mut builder = App::builder();
|
2022-12-22 18:19:49 +00:00
|
|
|
/// builder.client_name("mastodon-async_test");
|
2022-11-29 23:50:29 +00:00
|
|
|
/// let app = builder.build().unwrap();
|
2022-11-27 14:44:43 +00:00
|
|
|
/// ```
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
|
|
|
|
pub struct AppBuilder<'a> {
|
|
|
|
client_name: Option<Cow<'a, str>>,
|
|
|
|
redirect_uris: Option<Cow<'a, str>>,
|
|
|
|
scopes: Option<Scopes>,
|
|
|
|
website: Option<Cow<'a, str>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AppBuilder<'a> {
|
|
|
|
/// Creates a new AppBuilder object
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Name of the application. Will be displayed when the user is deciding to
|
|
|
|
/// grant permission.
|
|
|
|
///
|
|
|
|
/// In order to turn this builder into an App, this needs to be provided
|
|
|
|
pub fn client_name<I: Into<Cow<'a, str>>>(&mut self, name: I) -> &mut Self {
|
|
|
|
self.client_name = Some(name.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Where the user should be redirected after authorization
|
|
|
|
///
|
|
|
|
/// If none is specified, the default is `urn:ietf:wg:oauth:2.0:oob`
|
|
|
|
pub fn redirect_uris<I: Into<Cow<'a, str>>>(&mut self, uris: I) -> &mut Self {
|
|
|
|
self.redirect_uris = Some(uris.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Permission scope of the application.
|
|
|
|
///
|
|
|
|
/// IF none is specified, the default is Scopes::read_all()
|
|
|
|
pub fn scopes(&mut self, scopes: Scopes) -> &mut Self {
|
|
|
|
self.scopes = Some(scopes);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// URL to the homepage of your application.
|
|
|
|
pub fn website<I: Into<Cow<'a, str>>>(&mut self, website: I) -> &mut Self {
|
|
|
|
self.website = Some(website.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to convert this build into an `App`
|
|
|
|
///
|
|
|
|
/// Will fail if no `client_name` was provided
|
|
|
|
pub fn build(self) -> Result<App> {
|
|
|
|
Ok(App {
|
|
|
|
client_name: self
|
|
|
|
.client_name
|
2022-12-27 16:23:22 +00:00
|
|
|
.ok_or(Error::MissingField("client_name"))?
|
2022-11-27 14:44:43 +00:00
|
|
|
.into(),
|
|
|
|
redirect_uris: self
|
|
|
|
.redirect_uris
|
|
|
|
.unwrap_or_else(|| "urn:ietf:wg:oauth:2.0:oob".into())
|
|
|
|
.into(),
|
2022-12-27 14:52:27 +00:00
|
|
|
scopes: self.scopes.unwrap_or_else(Scopes::read_all),
|
2022-11-27 14:44:43 +00:00
|
|
|
website: self.website.map(|s| s.into()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryInto<App> for AppBuilder<'a> {
|
2022-11-29 23:50:29 +00:00
|
|
|
type Error = Error;
|
2022-11-27 14:44:43 +00:00
|
|
|
|
|
|
|
fn try_into(self) -> Result<App> {
|
2022-12-27 14:52:06 +00:00
|
|
|
self.build()
|
2022-11-27 14:44:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_app_builder() {
|
|
|
|
let builder = App::builder();
|
|
|
|
assert_eq!(builder, AppBuilder::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_app_scopes() {
|
|
|
|
let mut builder = App::builder();
|
|
|
|
builder.client_name("test").scopes(Scopes::all());
|
|
|
|
let app = builder.build().expect("Couldn't build App");
|
|
|
|
assert_eq!(app.scopes(), &Scopes::all());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_app_builder_all_methods() {
|
|
|
|
let mut builder = AppBuilder::new();
|
|
|
|
builder.client_name("foo-test");
|
|
|
|
builder.redirect_uris("http://example.com");
|
|
|
|
builder.scopes(Scopes::read_all() | Scopes::write_all());
|
|
|
|
builder.website("https://example.com");
|
|
|
|
let app = builder.build().expect("Couldn't build App");
|
|
|
|
assert_eq!(
|
|
|
|
app,
|
|
|
|
App {
|
|
|
|
client_name: "foo-test".to_string(),
|
|
|
|
redirect_uris: "http://example.com".to_string(),
|
|
|
|
scopes: Scopes::read_all() | Scopes::write_all(),
|
|
|
|
website: Some("https://example.com".to_string()),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_app_builder_build_fails_if_no_client_name_1() {
|
|
|
|
App::builder().build().expect("no client-name");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_app_builder_build_fails_if_no_client_name_2() {
|
|
|
|
let mut builder = App::builder();
|
|
|
|
builder
|
|
|
|
.website("https://example.com")
|
|
|
|
.redirect_uris("https://example.com")
|
|
|
|
.scopes(Scopes::all());
|
|
|
|
builder.build().expect("no client-name");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_app_try_into_app() {
|
|
|
|
let app = App {
|
|
|
|
client_name: "foo-test".to_string(),
|
|
|
|
redirect_uris: "http://example.com".to_string(),
|
|
|
|
scopes: Scopes::all(),
|
|
|
|
website: None,
|
|
|
|
};
|
|
|
|
let expected = app.clone();
|
2022-12-28 14:24:45 +00:00
|
|
|
#[allow(clippy::useless_conversion)]
|
2022-11-27 14:44:43 +00:00
|
|
|
let result = app.try_into().expect("Couldn't make App into App");
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_app_builder_try_into_app() {
|
|
|
|
let mut builder = App::builder();
|
|
|
|
builder
|
|
|
|
.client_name("foo-test")
|
|
|
|
.redirect_uris("http://example.com")
|
|
|
|
.scopes(Scopes::all());
|
|
|
|
let expected = App {
|
|
|
|
client_name: "foo-test".to_string(),
|
|
|
|
redirect_uris: "http://example.com".to_string(),
|
|
|
|
scopes: Scopes::all(),
|
|
|
|
website: None,
|
|
|
|
};
|
|
|
|
let result = builder
|
|
|
|
.try_into()
|
|
|
|
.expect("Couldn't make AppBuilder into App");
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
}
|
|
|
|
}
|