egg_mode/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5//! A library for interacting with Twitter.
6//!
7//! [Repository](https://github.com/egg-mode-rs/egg-mode)
8//!
9//! egg-mode is a Twitter library that aims to make as few assumptions about the user's codebase as
10//! possible. Endpoints are exposed as bare functions where authentication details are passed in as
11//! arguments, rather than as builder functions of a root "service" manager. The only exceptions to
12//! this guideline are endpoints with many optional parameters, like posting a status update or
13//! updating the metadata of a list.
14//!
15//! # About the examples in this documentation
16//!
17//! There are a couple prerequisites to using egg-mode, which its examples also assume:
18//!
19//! * All methods that hit the twitter API are `async` and should be awaited with the `.await` syntax.
20//!   All such calls return a result type with the [`Error`] enum as their Error value.
21//!   The resulting future must be executed on a `tokio` executor.
22//!   For more information, check out the [Rust `async` book][rust-futures] and the
23//!   [Tokio documentation guides][].
24//!
25//! * Twitter tracks API use through "tokens" which are managed by Twitter and processed separately
26//!   for each "authenticated user" you wish to connect to your app. egg-mode represents these
27//!   through the [`Token`] type, which each function that connects to the API uses to authenticate
28//!   the call.  egg-mode's [authentication overview][auth] describes how you can obtain one of
29//!   these, but each example outside of the authentication documentation brings in a `Token`
30//!   "offscreen", to avoid distracting from the rest of the example.
31//!
32//! [`Token`]: auth/enum.Token.html
33//! [auth]: auth/index.html
34//! [`Error`]: error/enum.Error.html
35//! [tokio]: https://tokio.rs
36//! [rust-futures]: https://rust-lang.github.io/async-book/
37//! [Tokio documentation guides]: https://tokio.rs/docs/overview
38//!
39//! To load the profile information of a single user:
40//!
41//! ```rust,no_run
42//! # use egg_mode::Token;
43//! # #[tokio::main]
44//! # async fn main() {
45//! # let token: Token = unimplemented!();
46//! let rustlang = egg_mode::user::show("rustlang", &token).await.unwrap();
47//!
48//! println!("{} (@{})", rustlang.name, rustlang.screen_name);
49//! # }
50//! ```
51//!
52//! To post a new tweet:
53//!
54//! ```rust,no_run
55//! # use egg_mode::Token;
56//! use egg_mode::tweet::DraftTweet;
57//! # #[tokio::main]
58//! # async fn main() {
59//! # let token: Token = unimplemented!();
60//!
61//! let post = DraftTweet::new("Hey Twitter!").send(&token).await.unwrap();
62//! # }
63//! ```
64//!
65//! # Crate features
66//!
67//! While all of egg-mode's features are available by default, it allows you to configure how it
68//! connects to Twitter and how it uses HTTPS. The crate's Cargo features are the following:
69//!
70//! * `native_tls`: On by default. With this feature on, egg-mode uses `native-tls` to access your
71//!   operating system's native TLS functionality to access Twitter.
72//! * `rustls`: Off by default. With this feature on, egg-mode instead uses the `rustls` TLS stack
73//!   to access Twitter. `rustls` will still use your operating system's native root certificates
74//!   to verify the connection.
75//! * `rustls_webpki`: Off by default. With this feature on, egg-mode will also use `rustls` to
76//!   connect, but it will also use the `webpki-roots` crate to include a set of compiled-in root
77//!   certificates to verify the connection, instead of using your operating system's root
78//!   certificates.
79//!
80//! Keep in mind that these features are mutually exclusive - if you enable more than one, a
81//! compile error will result. If you need to use `rustls` or `rustls_webpki`, remember to set
82//! `default-features = false` in your Cargo.toml.
83//!
84//! # Types and Functions
85//!
86//! All of the main content of egg-mode is in submodules, but there are a few things here in the
87//! crate root. To wit, it contains items related to authentication and a couple items that all the
88//! submodules use.
89//!
90//! ## `Response<T>`
91//!
92//! Every method that calls Twitter and carries rate-limit information wraps its return value in a
93//! [`Response`][] struct, that transmits this information to your app. From there, you can handle
94//! the rate-limit information to hold off on that kind of request, or simply grab its `response`
95//! field to get the output of whatever method you called. `Response` also implements `Deref`, so
96//! for the most part you can access fields of the final result without having to grab the
97//! `response` field directly.
98//!
99//! [`Response`]: struct.Response.html
100//!
101//! ## `Token`
102//!
103//! While the complete process of authenticating with Twitter involves functions and types in [the
104//! `auth` module][auth], the [`Token`] type is central enough to the operation of egg-mode that
105//! it's re-exported at the crate root. The inner [`KeyPair`] type is also re-exported here, to aid
106//! existing code that used the type, and to aid the authentication process, which requires
107//! manually creating one at the very beginning.
108//!
109//! # Modules
110//!
111//! As there are many actions available in the Twitter API, egg-mode divides them roughly into
112//! several modules by their shared purpose. Here's a sort of high-level overview, in rough order
113//! from "most important" to "less directly used":
114//!
115//! ## Primary actions
116//!
117//! These could be considered the "core" actions within the Twitter API that egg-mode has made
118//! available.
119//!
120//! * `auth`: This module contains all the functions required to fully authenticate with Twitter.
121//!   The module docs contain the complete overview of how to use egg-mode to properly sign your
122//!   API calls so that Twitter will accept them.
123//! * `tweet`: This module lets you act on tweets. Here you can find actions to load a user's
124//!   timeline, post a new tweet, or like and retweet individual posts.
125//! * `user`: This module lets you act on users, be it by following or unfollowing them, loading
126//!   their profile information, blocking or muting them, or showing the relationship between two
127//!   users.
128//! * `search`: Due to the complexity of searching for tweets, it gets its own module.
129//! * `direct`: Here you can work with a user's Direct Messages, either by loading DMs they've sent
130//!   or received, or by sending new ones.
131//! * `list`: This module lets you act on lists, from creating and deleting them, adding and
132//!   removing users, or loading the posts made by their members.
133//! * `media`: This module lets you upload images, GIFs, and videos to Twitter so you can attach
134//!   them to tweets.
135//!
136//! ## Secondary actions
137//!
138//! These modules still contain direct actions for Twitter, but they can be considered as having
139//! more of a helper role than something you might use directly.
140//!
141//! * `place`: Here are actions that look up physical locations that can be attached to tweets, as
142//!   well at the `Place` struct that appears on tweets with locations attached.
143//! * `service`: These are some miscellaneous methods that show information about the Twitter
144//!   service as a whole, like loading the maximum length of t.co URLs or loading the current Terms
145//!   of Service or Privacy Policy.
146//!
147//! ## Helper structs
148//!
149//! These modules contain some implementations that wrap some pattern seen in multiple "action"
150//! modules.
151//!
152//! * `cursor`: This contains a helper trait and some helper structs that allow effective cursoring
153//!   through certain collections of results from Twitter.
154//! * `entities`: Whenever some text can be returned that may contain links, hashtags, media, or
155//!   user mentions, its metadata is parsed into something that lives in this module.
156//! * `error`: Any interaction with Twitter may result in an error condition, be it from finding a
157//!   tweet or user that doesn't exist or the network connection being unavailable. All the error
158//!   types are aggregated into an enum in this module.
159
160#![warn(missing_docs)]
161#![warn(unused_extern_crates)]
162#![warn(unused_qualifications)]
163
164#[macro_use]
165mod common;
166pub mod account;
167pub mod auth;
168pub mod cursor;
169pub mod direct;
170pub mod entities;
171pub mod error;
172mod links;
173pub mod list;
174pub mod media;
175pub mod place;
176pub mod raw;
177pub mod search;
178pub mod service;
179pub mod stream;
180pub mod trend;
181pub mod tweet;
182pub mod user;
183
184pub use crate::auth::{KeyPair, Token};
185pub use crate::common::{RateLimit, Response, ResponseIter};