diff --git a/src/apps.rs b/src/apps.rs index 98fae0d..e3ce035 100644 --- a/src/apps.rs +++ b/src/apps.rs @@ -3,8 +3,10 @@ use std::borrow::Cow; use serde::Serialize; use try_from::TryInto; -use crate::errors::{Error, Result}; -use crate::scopes::Scopes; +use crate::{ + errors::{Error, Result}, + scopes::Scopes, +}; /// Represents an application that can be registered with a mastodon instance #[derive(Clone, Debug, Default, Serialize, PartialEq)] diff --git a/src/data.rs b/src/data.rs index 1015725..f311d10 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,5 +1,5 @@ +use serde::{Deserialize, Serialize}; use std::borrow::Cow; -use serde::{Serialize, Deserialize}; /// Raw data about mastodon app. Save `Data` using `serde` to prevent needing /// to authenticate on every run. diff --git a/src/entities/account.rs b/src/entities/account.rs index a686b17..b66a37a 100644 --- a/src/entities/account.rs +++ b/src/entities/account.rs @@ -1,9 +1,12 @@ //! A module containing everything relating to a account returned from the api. -use chrono::prelude::*; -use serde::{Deserialize, Serialize}; -use serde::de::{self, Unexpected}; use crate::status_builder; +use chrono::prelude::*; +use serde::{ + de::{self, Unexpected}, + Deserialize, + Serialize, +}; use std::path::PathBuf; /// A struct representing an Account. diff --git a/src/entities/filter.rs b/src/entities/filter.rs index 1f7232b..96ecbb8 100644 --- a/src/entities/filter.rs +++ b/src/entities/filter.rs @@ -1,4 +1,4 @@ -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; /// Represents a single Filter #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] diff --git a/src/entities/instance.rs b/src/entities/instance.rs index 3d2e464..7f3f26e 100644 --- a/src/entities/instance.rs +++ b/src/entities/instance.rs @@ -1,6 +1,6 @@ //! Module containing everything related to an instance. -use serde::Deserialize; use super::account::Account; +use serde::Deserialize; /// A struct containing info of an instance. #[derive(Debug, Clone, Deserialize, PartialEq)] diff --git a/src/entities/itemsiter.rs b/src/entities/itemsiter.rs index ece70de..796a3c3 100644 --- a/src/entities/itemsiter.rs +++ b/src/entities/itemsiter.rs @@ -1,5 +1,4 @@ -use crate::http_send::HttpSend; -use crate::page::Page; +use crate::{http_send::HttpSend, page::Page}; use serde::Deserialize; /// Abstracts away the `next_page` logic into a single stream of items diff --git a/src/entities/notification.rs b/src/entities/notification.rs index 9ad0d88..f263e91 100644 --- a/src/entities/notification.rs +++ b/src/entities/notification.rs @@ -1,8 +1,8 @@ //! Module containing all info about notifications. -use serde::Deserialize; use super::{account::Account, status::Status}; use chrono::prelude::*; +use serde::Deserialize; /// A struct containing info about a notification. #[derive(Debug, Clone, Deserialize, PartialEq)] diff --git a/src/entities/push.rs b/src/entities/push.rs index b277c8d..e393824 100644 --- a/src/entities/push.rs +++ b/src/entities/push.rs @@ -27,8 +27,8 @@ pub struct Subscription { } pub(crate) mod add_subscription { - use serde::Serialize; use super::Alerts; + use serde::Serialize; #[derive(Debug, Clone, PartialEq, Serialize, Default)] pub(crate) struct Form { @@ -55,8 +55,8 @@ pub(crate) mod add_subscription { } pub(crate) mod update_data { - use serde::Serialize; use super::Alerts; + use serde::Serialize; #[derive(Debug, Clone, PartialEq, Serialize, Default)] pub(crate) struct Data { diff --git a/src/entities/status.rs b/src/entities/status.rs index 95f307b..087acfa 100644 --- a/src/entities/status.rs +++ b/src/entities/status.rs @@ -1,9 +1,8 @@ //! Module containing all info relating to a status. use super::prelude::*; +use crate::{entities::card::Card, status_builder::Visibility}; use chrono::prelude::*; -use crate::entities::card::Card; -use crate::status_builder::Visibility; use serde::Deserialize; /// A status from the instance. diff --git a/src/errors.rs b/src/errors.rs index 49a53b6..1e186df 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,6 +1,10 @@ -use std::{error, fmt, io::Error as IoError}; use serde::Deserialize; +use std::{error, fmt, io::Error as IoError}; +#[cfg(feature = "toml")] +use ::toml::de::Error as TomlDeError; +#[cfg(feature = "toml")] +use ::toml::ser::Error as TomlSerError; #[cfg(feature = "env")] use envy::Error as EnvyError; use hyper_old_types::Error as HeaderParseError; @@ -8,12 +12,8 @@ use reqwest::{header::ToStrError as HeaderStrError, Error as HttpError, StatusCo use serde_json::Error as SerdeError; use serde_qs::Error as SerdeQsError; use serde_urlencoded::ser::Error as UrlEncodedError; -#[cfg(feature = "toml")] -use ::toml::de::Error as TomlDeError; -#[cfg(feature = "toml")] -use ::toml::ser::Error as TomlSerError; -use url::ParseError as UrlError; use tungstenite::error::Error as WebSocketError; +use url::ParseError as UrlError; /// Convience type over `std::result::Result` with `Error` as the error type. pub type Result = ::std::result::Result; @@ -94,9 +94,7 @@ impl error::Error for Error { Error::SerdeQs(ref e) => e, Error::WebSocket(ref e) => e, - Error::Client(..) | Error::Server(..) => { - return None - }, + Error::Client(..) | Error::Server(..) => return None, Error::ClientIdRequired => return None, Error::ClientSecretRequired => return None, Error::AccessTokenRequired => return None, diff --git a/src/helpers/cli.rs b/src/helpers/cli.rs index eac3a39..25c1e03 100644 --- a/src/helpers/cli.rs +++ b/src/helpers/cli.rs @@ -1,9 +1,6 @@ use std::io::{self, BufRead, Write}; -use crate::errors::Result; -use crate::http_send::HttpSend; -use crate::registration::Registered; -use crate::Mastodon; +use crate::{errors::Result, http_send::HttpSend, registration::Registered, Mastodon}; /// Finishes the authentication process for the given `Registered` object, /// using the command-line diff --git a/src/helpers/env.rs b/src/helpers/env.rs index 856d650..7bac385 100644 --- a/src/helpers/env.rs +++ b/src/helpers/env.rs @@ -1,7 +1,6 @@ use envy; -use crate::data::Data; -use crate::Result; +use crate::{data::Data, Result}; /// Attempts to deserialize a Data struct from the environment pub fn from_env() -> Result { diff --git a/src/helpers/json.rs b/src/helpers/json.rs index c3c885f..70871f2 100644 --- a/src/helpers/json.rs +++ b/src/helpers/json.rs @@ -6,8 +6,7 @@ use std::{ use serde_json; -use crate::data::Data; -use crate::Result; +use crate::{data::Data, Result}; /// Attempts to deserialize a Data struct from a string pub fn from_str(s: &str) -> Result { diff --git a/src/helpers/toml.rs b/src/helpers/toml.rs index 11f4822..5b562bf 100644 --- a/src/helpers/toml.rs +++ b/src/helpers/toml.rs @@ -4,8 +4,7 @@ use std::{ path::Path, }; -use crate::data::Data; -use crate::Result; +use crate::{data::Data, Result}; /// Attempts to deserialize a Data struct from a string pub fn from_str(s: &str) -> Result { diff --git a/src/http_send.rs b/src/http_send.rs index 77207da..46ce679 100644 --- a/src/http_send.rs +++ b/src/http_send.rs @@ -1,6 +1,6 @@ +use crate::Result; use reqwest::{Client, Request, RequestBuilder, Response}; use std::fmt::Debug; -use crate::Result; /// Abstracts away the process of turning an HTTP request into an HTTP response pub trait HttpSend: Clone + Debug { diff --git a/src/lib.rs b/src/lib.rs index 413a02c..3b08ae0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,34 +71,34 @@ )] #![allow(broken_intra_doc_links)] -use std::{ - borrow::Cow, - io::BufRead, - ops, -}; +use std::{borrow::Cow, io::BufRead, ops}; use reqwest::{Client, RequestBuilder, Response}; use tap_reader::Tap; use tungstenite::client::AutoStream; -use crate::entities::prelude::*; -use crate::http_send::{HttpSend, HttpSender}; -use crate::page::Page; +use crate::{ + entities::prelude::*, + http_send::{HttpSend, HttpSender}, + page::Page, +}; -pub use crate::data::Data; -pub use crate::errors::{ApiError, Error, Result}; -pub use isolang::Language; -pub use crate::mastodon_client::{MastodonClient, MastodonUnauthenticated}; -pub use crate::media_builder::MediaBuilder; -pub use crate::registration::Registration; -pub use crate::requests::{ - AddFilterRequest, - AddPushRequest, - StatusesRequest, - UpdateCredsRequest, - UpdatePushRequest, +pub use crate::{ + data::Data, + errors::{ApiError, Error, Result}, + mastodon_client::{MastodonClient, MastodonUnauthenticated}, + media_builder::MediaBuilder, + registration::Registration, + requests::{ + AddFilterRequest, + AddPushRequest, + StatusesRequest, + UpdateCredsRequest, + UpdatePushRequest, + }, + status_builder::{NewStatus, StatusBuilder}, }; -pub use crate::status_builder::{NewStatus, StatusBuilder}; +pub use isolang::Language; /// Registering your App pub mod apps; @@ -129,14 +129,16 @@ pub mod status_builder; mod macros; /// Automatically import the things you need pub mod prelude { - pub use crate::scopes::Scopes; - pub use crate::Data; - pub use crate::Mastodon; - pub use crate::MastodonClient; - pub use crate::NewStatus; - pub use crate::Registration; - pub use crate::StatusBuilder; - pub use crate::StatusesRequest; + pub use crate::{ + scopes::Scopes, + Data, + Mastodon, + MastodonClient, + NewStatus, + Registration, + StatusBuilder, + StatusesRequest, + }; } /// Your mastodon application client, handles all requests to and from Mastodon. @@ -473,7 +475,8 @@ impl MastodonClient for Mastodon { "https" => "wss", x => return Err(Error::Other(format!("Bad URL scheme: {}", x))), }; - url.set_scheme(new_scheme).map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; + url.set_scheme(new_scheme) + .map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; let client = tungstenite::connect(url.as_str())?.0; @@ -492,7 +495,8 @@ impl MastodonClient for Mastodon { "https" => "wss", x => return Err(Error::Other(format!("Bad URL scheme: {}", x))), }; - url.set_scheme(new_scheme).map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; + url.set_scheme(new_scheme) + .map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; let client = tungstenite::connect(url.as_str())?.0; @@ -511,7 +515,8 @@ impl MastodonClient for Mastodon { "https" => "wss", x => return Err(Error::Other(format!("Bad URL scheme: {}", x))), }; - url.set_scheme(new_scheme).map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; + url.set_scheme(new_scheme) + .map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; let client = tungstenite::connect(url.as_str())?.0; @@ -531,7 +536,8 @@ impl MastodonClient for Mastodon { "https" => "wss", x => return Err(Error::Other(format!("Bad URL scheme: {}", x))), }; - url.set_scheme(new_scheme).map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; + url.set_scheme(new_scheme) + .map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; let client = tungstenite::connect(url.as_str())?.0; @@ -551,7 +557,8 @@ impl MastodonClient for Mastodon { "https" => "wss", x => return Err(Error::Other(format!("Bad URL scheme: {}", x))), }; - url.set_scheme(new_scheme).map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; + url.set_scheme(new_scheme) + .map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; let client = tungstenite::connect(url.as_str())?.0; @@ -571,7 +578,8 @@ impl MastodonClient for Mastodon { "https" => "wss", x => return Err(Error::Other(format!("Bad URL scheme: {}", x))), }; - url.set_scheme(new_scheme).map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; + url.set_scheme(new_scheme) + .map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; let client = tungstenite::connect(url.as_str())?.0; @@ -590,7 +598,8 @@ impl MastodonClient for Mastodon { "https" => "wss", x => return Err(Error::Other(format!("Bad URL scheme: {}", x))), }; - url.set_scheme(new_scheme).map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; + url.set_scheme(new_scheme) + .map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; let client = tungstenite::connect(url.as_str())?.0; @@ -631,7 +640,8 @@ impl MastodonClient for Mastodon { } #[derive(Debug)] -/// WebSocket newtype so that EventStream can be implemented without coherency issues +/// WebSocket newtype so that EventStream can be implemented without coherency +/// issues pub struct WebSocket(tungstenite::protocol::WebSocket); /// A type that streaming events can be read from @@ -684,12 +694,12 @@ impl EventReader { fn make_event(&self, lines: &[String]) -> Result { let event; let data; - if let Some(event_line) = lines - .iter() - .find(|line| line.starts_with("event:")) - { + if let Some(event_line) = lines.iter().find(|line| line.starts_with("event:")) { event = event_line[6..].trim().to_string(); - data = lines.iter().find(|line| line.starts_with("data:")).map(|x| x[5..].trim().to_string()); + data = lines + .iter() + .find(|line| line.starts_with("data:")) + .map(|x| x[5..].trim().to_string()); } else { use serde::Deserialize; #[derive(Deserialize)] @@ -816,7 +826,8 @@ impl MastodonUnauth { "https" => "wss", x => return Err(Error::Other(format!("Bad URL scheme: {}", x))), }; - url.set_scheme(new_scheme).map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; + url.set_scheme(new_scheme) + .map_err(|_| Error::Other("Bad URL scheme!".to_string()))?; let client = tungstenite::connect(url.as_str())?.0; diff --git a/src/mastodon_client.rs b/src/mastodon_client.rs index 98d2cc3..a81bd4e 100644 --- a/src/mastodon_client.rs +++ b/src/mastodon_client.rs @@ -1,18 +1,20 @@ use std::borrow::Cow; -use crate::entities::prelude::*; -use crate::errors::Result; -use crate::http_send::{HttpSend, HttpSender}; -use crate::media_builder::MediaBuilder; -use crate::page::Page; -use crate::requests::{ - AddFilterRequest, - AddPushRequest, - StatusesRequest, - UpdateCredsRequest, - UpdatePushRequest, +use crate::{ + entities::prelude::*, + errors::Result, + http_send::{HttpSend, HttpSender}, + media_builder::MediaBuilder, + page::Page, + requests::{ + AddFilterRequest, + AddPushRequest, + StatusesRequest, + UpdateCredsRequest, + UpdatePushRequest, + }, + status_builder::NewStatus, }; -use crate::status_builder::NewStatus; /// Represents the set of methods that a Mastodon Client can do, so that /// implementations might be swapped out for testing @@ -197,11 +199,11 @@ pub trait MastodonClient { fn new_status(&self, status: NewStatus) -> Result { unimplemented!("This method was not implemented"); } -/// GET /api/v1/timelines/public?local=true + /// GET /api/v1/timelines/public?local=true fn get_local_timeline(&self) -> Result> { unimplemented!("This method was not implemented"); } -/// GET /api/v1/timelines/public?local=false + /// GET /api/v1/timelines/public?local=false fn get_federated_timeline(&self) -> Result> { unimplemented!("This method was not implemented"); } diff --git a/src/media_builder.rs b/src/media_builder.rs index eab9c94..b4c7e92 100644 --- a/src/media_builder.rs +++ b/src/media_builder.rs @@ -1,5 +1,5 @@ -use std::borrow::Cow; use serde::Serialize; +use std::borrow::Cow; /// A builder pattern struct for constructing a media attachment. #[derive(Debug, Default, Clone, Serialize)] @@ -21,6 +21,7 @@ impl MediaBuilder { focus: None, } } + /// Set an alt text description for the attachment. pub fn description(mut self, description: Cow<'static, str>) -> Self { self.description = Some(description); diff --git a/src/registration.rs b/src/registration.rs index d2984d5..0adb1cd 100644 --- a/src/registration.rs +++ b/src/registration.rs @@ -5,14 +5,16 @@ use serde::Deserialize; use try_from::TryInto; use url::percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET}; -use crate::apps::{App, AppBuilder}; -use crate::http_send::{HttpSend, HttpSender}; -use crate::scopes::Scopes; -use crate::Data; -use crate::Error; -use crate::Mastodon; -use crate::MastodonBuilder; -use crate::Result; +use crate::{ + apps::{App, AppBuilder}, + http_send::{HttpSend, HttpSender}, + scopes::Scopes, + Data, + Error, + Mastodon, + MastodonBuilder, + Result, +}; const DEFAULT_REDIRECT_URI: &'static str = "urn:ietf:wg:oauth:2.0:oob"; diff --git a/src/requests/filter.rs b/src/requests/filter.rs index bdbe54c..63f9790 100644 --- a/src/requests/filter.rs +++ b/src/requests/filter.rs @@ -1,6 +1,6 @@ use crate::entities::filter::FilterContext; -use std::time::Duration; use serde::Serialize; +use std::time::Duration; /// Form used to create a filter /// diff --git a/src/requests/push.rs b/src/requests/push.rs index 8540d04..c16d437 100644 --- a/src/requests/push.rs +++ b/src/requests/push.rs @@ -1,5 +1,7 @@ -use crate::entities::push::{add_subscription, update_data}; -use crate::errors::Result; +use crate::{ + entities::push::{add_subscription, update_data}, + errors::Result, +}; use serde::Serialize; /// Container for the key & auth strings for an AddPushRequest diff --git a/src/requests/statuses.rs b/src/requests/statuses.rs index 8c8e9dc..d9474fa 100644 --- a/src/requests/statuses.rs +++ b/src/requests/statuses.rs @@ -1,7 +1,7 @@ use crate::errors::Error; +use serde::Serialize; use serde_qs; use std::{borrow::Cow, convert::Into}; -use serde::Serialize; mod bool_qs_serialize { use serde::Serializer; diff --git a/src/requests/update_credentials.rs b/src/requests/update_credentials.rs index 4d1aff9..f3e7c7e 100644 --- a/src/requests/update_credentials.rs +++ b/src/requests/update_credentials.rs @@ -3,9 +3,11 @@ use std::{ path::{Path, PathBuf}, }; -use crate::entities::account::{Credentials, MetadataField, UpdateSource}; -use crate::errors::Result; -use crate::status_builder; +use crate::{ + entities::account::{Credentials, MetadataField, UpdateSource}, + errors::Result, + status_builder, +}; /// Builder to pass to the Mastodon::update_credentials method /// @@ -202,8 +204,10 @@ impl UpdateCredsRequest { #[cfg(test)] mod tests { use super::*; - use crate::entities::account::{Credentials, MetadataField, UpdateSource}; - use crate::status_builder::Visibility; + use crate::{ + entities::account::{Credentials, MetadataField, UpdateSource}, + status_builder::Visibility, + }; #[test] fn test_update_creds_request_new() { diff --git a/src/scopes.rs b/src/scopes.rs index 1fd8a87..0a7a0a6 100644 --- a/src/scopes.rs +++ b/src/scopes.rs @@ -7,8 +7,12 @@ use std::{ }; use crate::errors::Error; -use serde::{Deserialize, Deserializer, Serialize}; -use serde::de::{self, Visitor}; +use serde::{ + de::{self, Visitor}, + Deserialize, + Deserializer, + Serialize, +}; /// Represents a set of OAuth scopes /// @@ -62,15 +66,18 @@ impl<'de> Visitor<'de> for DeserializeScopesVisitor { } fn visit_str(self, v: &str) -> Result - where E: de::Error + where + E: de::Error, { Scopes::from_str(v).map_err(de::Error::custom) } } impl<'de> Deserialize<'de> for Scopes { - fn deserialize(deserializer: D) -> Result>::Error> where - D: Deserializer<'de> { + fn deserialize(deserializer: D) -> Result>::Error> + where + D: Deserializer<'de>, + { deserializer.deserialize_str(DeserializeScopesVisitor) } } @@ -762,7 +769,7 @@ mod tests { let expected = format!("\"{}\"", b); assert_eq!(&ser, &expected); - let des : Scopes = serde_json::from_str(&ser).expect("Couldn't deserialize Scopes"); + let des: Scopes = serde_json::from_str(&ser).expect("Couldn't deserialize Scopes"); assert_eq!(&des, a); } }