Skip to content

Commit

Permalink
Merge client feature/module into gateway (#2947)
Browse files Browse the repository at this point in the history
The `client` and `gateway` features were very tightly coupled so it
makes the most sense to just merge them because there's no real path
towards separating them ergonomically.

A few breaking changes:
* Removed `ClientError` because it was unused.
* The `client` module is moved to be a submodule under the `gateway`
module.
* Moved sharding-specific gateway types into their own `sharding`
submodule for clarity's sake, but re-exported them.

I also adjusted the gateway documentation to hopefully make it as clear as
possible.

Note that the `Cargo.toml` changes are mostly superficial - in
particular, changing `gateway` to depend on `model` (instead of `http`
like how `client` did) isn't actually much of a change because trying to
use `gateway` with only a subset of `model` enabled will fail to
compile, and that was also the case for `client` previously.
  • Loading branch information
mkrasnitski authored Aug 22, 2024
1 parent b167b4d commit 9db92c1
Show file tree
Hide file tree
Showing 54 changed files with 391 additions and 571 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ jobs:
- name: no default features
features: " "
- name: no cache
features: builder client framework gateway model http utils rustls_backend
features: framework rustls_backend
- name: no gateway
features: model http rustls_backend
features: model rustls_backend
- name: chrono
features: chrono
- name: unstable API + typesize
Expand Down Expand Up @@ -204,7 +204,7 @@ jobs:

- name: Build docs
run: |
cargo doc --no-deps --features collector,voice,unstable
cargo doc --no-deps --features full,unstable
env:
RUSTDOCFLAGS: -D rustdoc::broken_intra_doc_links

Expand Down
17 changes: 4 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,9 @@ default_native_tls = ["default_no_backend", "native_tls_backend"]

# Serenity requires a backend, this picks all default features without a backend.
default_no_backend = [
"builder",
"cache",
"chrono",
"client",
"framework",
"gateway",
"model",
"http",
"utils",
]

# Enables builder structs to configure Discord HTTP requests. Without this feature, you have to
Expand All @@ -87,14 +81,11 @@ builder = ["tokio/fs"]
cache = ["fxhash", "dashmap"]
# Enables collectors, a utility feature that lets you await interaction events in code with
# zero setup, without needing to setup an InteractionCreate event listener.
collector = ["gateway", "model"]
# Wraps the gateway and http functionality into a single interface
# TODO: should this require "gateway"?
client = ["http"]
collector = ["gateway"]
# Enables the Framework trait which is an abstraction for old-style text commands.
framework = ["client", "model", "utils"]
framework = ["gateway"]
# Enables gateway support, which allows bots to listen for Discord events.
gateway = ["flate2"]
gateway = ["model", "flate2"]
# Enables HTTP, which enables bots to execute actions on Discord.
http = ["dashmap", "mime_guess", "percent-encoding"]
# Enables wrapper methods around HTTP requests on model types.
Expand All @@ -108,7 +99,7 @@ voice_model = ["serenity-voice-model"]
unstable = []
# Enables some utility functions that can be useful for bot creators.
utils = []
voice = ["client", "model"]
voice = ["gateway"]
# Enables unstable tokio features to give explicit names to internally spawned tokio tasks
tokio_task_builder = ["tokio/tracing"]
interactions_endpoint = ["ed25519-dalek"]
Expand Down
9 changes: 3 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#[cfg(all(
any(feature = "http", feature = "gateway"),
not(any(feature = "rustls_backend", feature = "native_tls_backend"))
))]
#[cfg(all(feature = "http", not(any(feature = "rustls_backend", feature = "native_tls_backend"))))]
compile_error!(
"You have the `http` or `gateway` feature enabled, either the `rustls_backend` or \
`native_tls_backend` feature must be selected to let Serenity use `http` or `gateway`.\n\
"You have the `http` feature enabled; either the `rustls_backend` or `native_tls_backend` \
feature must be enabled to let Serenity make requests over the network.\n\
- `rustls_backend` uses Rustls, a pure Rust TLS-implemenation.\n\
- `native_tls_backend` uses SChannel on Windows, Secure Transport on macOS, and OpenSSL on \
other platforms.\n\
Expand Down
2 changes: 1 addition & 1 deletion examples/e01_basic_ping_bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
2 changes: 1 addition & 1 deletion examples/e02_transparent_guild_sharding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
2 changes: 1 addition & 1 deletion examples/e03_struct_utilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
2 changes: 1 addition & 1 deletion examples/e04_message_builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
2 changes: 1 addition & 1 deletion examples/e05_sample_bot_structure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["my name <my@email.address>"]
edition = "2021"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "collector"] }
serenity = { path = "../../", default-features = false, features = ["collector", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
2 changes: 1 addition & 1 deletion examples/e06_env_logging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ tracing-subscriber = "0.3"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }

[dependencies.serenity]
features = ["client", "rustls_backend"]
features = ["gateway", "rustls_backend"]
path = "../../"
2 changes: 1 addition & 1 deletion examples/e07_shard_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ tokio = { version = "1.0", features = ["macros", "rt-multi-thread", "time"] }

[dependencies.serenity]
default-features = false
features = ["client", "gateway", "rustls_backend", "model"]
features = ["gateway", "model", "rustls_backend"]
path = "../../"
2 changes: 1 addition & 1 deletion examples/e08_create_message_builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "chrono"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "chrono", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
2 changes: 1 addition & 1 deletion examples/e09_collectors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies.serenity]
features = ["framework", "rustls_backend", "collector"]
features = ["collector", "framework", "rustls_backend"]
path = "../../"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/e10_gateway_intents/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
2 changes: 1 addition & 1 deletion examples/e12_parallel_loops/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "cache"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "cache", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
sys-info = "0.9"
chrono = { version = "0.4", default-features = false, features = ["clock"] }
2 changes: 1 addition & 1 deletion examples/e13_sqlite_database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "sqlite"] }
2 changes: 1 addition & 1 deletion examples/e14_message_components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "collector"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "collector", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
dotenv = { version = "0.15.0" }
2 changes: 1 addition & 1 deletion examples/e15_webhook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["rustls_backend", "model"] }
serenity = { path = "../../", default-features = false, features = ["model", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
2 changes: 1 addition & 1 deletion examples/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ authors = ["my name <my@email.address>"]
edition = "2018"

[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "cache", "collector"] }
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "cache", "collector", "rustls_backend"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
env_logger = "0.10.0"
5 changes: 2 additions & 3 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ impl Cache {
/// struct Handler;
///
/// #[serenity::async_trait]
/// # #[cfg(feature = "client")]
/// impl EventHandler for Handler {
/// async fn cache_ready(&self, ctx: Context, _: Vec<GuildId>) {
/// println!("{} unknown members", ctx.cache.unknown_members());
Expand Down Expand Up @@ -319,7 +318,7 @@ impl Cache {
/// }
/// ```
///
/// [`Context`]: crate::client::Context
/// [`Context`]: crate::gateway::client::Context
/// [`Shard`]: crate::gateway::Shard
pub fn guilds(&self) -> Vec<GuildId> {
let unavailable_guilds = self.unavailable_guilds();
Expand Down Expand Up @@ -405,7 +404,7 @@ impl Cache {
/// # }
/// ```
///
/// [`EventHandler::message`]: crate::client::EventHandler::message
/// [`EventHandler::message`]: crate::gateway::client::EventHandler::message
pub fn message(&self, channel_id: ChannelId, message_id: MessageId) -> Option<MessageRef<'_>> {
#[cfg(feature = "temp_cache")]
if let Some(message) = self.temp_messages.get(&message_id) {
Expand Down
25 changes: 0 additions & 25 deletions src/client/error.rs

This file was deleted.

14 changes: 0 additions & 14 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use reqwest::{header::InvalidHeaderValue, Error as ReqwestError};
#[cfg(feature = "gateway")]
use tokio_tungstenite::tungstenite::error::Error as TungsteniteError;

#[cfg(feature = "client")]
use crate::client::ClientError;
#[cfg(feature = "gateway")]
use crate::gateway::GatewayError;
#[cfg(feature = "http")]
Expand All @@ -24,9 +22,6 @@ use crate::model::ModelError;
pub type Result<T, E = Error> = StdResult<T, E>;

/// A common error enum returned by most of the library's functionality within a custom [`Result`].
///
/// The most common error types, the [`ClientError`] and [`GatewayError`] enums, are both wrapped
/// around this in the form of the [`Self::Client`] and [`Self::Gateway`] variants.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Expand All @@ -38,11 +33,6 @@ pub enum Error {
///
/// [`model`]: crate::model
Model(ModelError),
/// A [client] error.
///
/// [client]: crate::client
#[cfg(feature = "client")]
Client(ClientError),
/// An error from the [`gateway`] module.
///
/// [`gateway`]: crate::gateway
Expand Down Expand Up @@ -117,8 +107,6 @@ impl fmt::Display for Error {
Self::Io(inner) => fmt::Display::fmt(&inner, f),
Self::Json(inner) => fmt::Display::fmt(&inner, f),
Self::Model(inner) => fmt::Display::fmt(&inner, f),
#[cfg(feature = "client")]
Self::Client(inner) => fmt::Display::fmt(&inner, f),
#[cfg(feature = "gateway")]
Self::Gateway(inner) => fmt::Display::fmt(&inner, f),
#[cfg(feature = "http")]
Expand All @@ -136,8 +124,6 @@ impl StdError for Error {
Self::Io(inner) => Some(inner),
Self::Json(inner) => Some(inner),
Self::Model(inner) => Some(inner),
#[cfg(feature = "client")]
Self::Client(inner) => Some(inner),
#[cfg(feature = "gateway")]
Self::Gateway(inner) => Some(inner),
#[cfg(feature = "http")]
Expand Down
6 changes: 3 additions & 3 deletions src/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
//!
//! This is used in combination with [`ClientBuilder::framework`].
//!
//! [`ClientBuilder::framework`]: crate::client::ClientBuilder::framework
//! [`ClientBuilder::framework`]: crate::gateway::client::ClientBuilder::framework

use async_trait::async_trait;

use crate::client::{Client, Context, FullEvent};
use crate::gateway::client::{Client, Context, FullEvent};

/// A trait for defining your own framework for serenity to use.
///
/// Should you implement this trait, or define a `message` handler, depends on you. However, using
/// this will benefit you by abstracting the [`EventHandler`] away.
///
/// [`EventHandler`]: crate::client::EventHandler
/// [`EventHandler`]: crate::gateway::client::EventHandler
#[async_trait]
pub trait Framework: Send + Sync {
/// Called directly after the `Client` is created.
Expand Down
20 changes: 0 additions & 20 deletions src/gateway/bridge/event.rs

This file was deleted.

Loading

0 comments on commit 9db92c1

Please sign in to comment.