Skip to content

Commit

Permalink
Break up the model module
Browse files Browse the repository at this point in the history
The `model` module has historically been one giant module re-exporting
all of the model types, which is somewhere around 100 types. This can be
a lot to look at for a new user and somewhat overwhelming, especially
with a large number of fine-grained imports from the module.

The module is now neatly split up into submodules, mostly like it has
been internally since the early versions of the library. The submodules
are:

- application
- channel
- error
- event
- gateway
- guild
- id
- invite
- misc
- permissions
- prelude
- user
- voice
- webhook

Each submodule contains types that are "owned" by the module. For
example, the `guild` submodule contains, but not limited to, Emoji,
AuditLogsEntry, Role, and Member. `channel` contains, but not limited
to, Attachment, Embed, Message, and Reaction.

Upgrade path:

Instead of glob importing the models via `use serenity::model::*;`,
instead glob import via the prelude:

```rust
use serenity::model::prelude::*;
```

Instead of importing from the root model module:

```rust
use serenity::model::{Guild, Message, OnlineStatus, Role, User};
```

instead import from the submodules like so:

```rust
use serenity::model::channel::Message;
use serenity::model::guild::{Guild, Role};
use serenity::model::user::{OnlineStatus, User};
```
  • Loading branch information
Zeyla Hellyer committed Dec 16, 2017
1 parent a2a07b9 commit bcd16dd
Show file tree
Hide file tree
Showing 75 changed files with 525 additions and 428 deletions.
3 changes: 2 additions & 1 deletion examples/01_basic_ping_bot/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
extern crate serenity;

use serenity::prelude::*;
use serenity::model::*;
use serenity::model::channel::Message;
use serenity::model::gateway::Ready;
use std::env;

struct Handler;
Expand Down
3 changes: 2 additions & 1 deletion examples/02_transparent_guild_sharding/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
extern crate serenity;

use serenity::prelude::*;
use serenity::model::*;
use serenity::model::channel::Message;
use serenity::model::gateway::Ready;
use std::env;

// Serenity implements transparent sharding in a way that you do not need to
Expand Down
3 changes: 2 additions & 1 deletion examples/03_struct_utilities/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
extern crate serenity;

use serenity::prelude::*;
use serenity::model::*;
use serenity::model::channel::Message;
use serenity::model::gateway::Ready;
use std::env;

struct Handler;
Expand Down
3 changes: 2 additions & 1 deletion examples/06_voice/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ extern crate serenity;

use serenity::client::CACHE;
use serenity::framework::StandardFramework;
use serenity::model::*;
use serenity::model::gateway::Ready;
use serenity::model::id::ChannelId;
use serenity::prelude::*;
use serenity::voice;
use serenity::Result as SerenityResult;
Expand Down
2 changes: 1 addition & 1 deletion examples/07_sample_bot_structure/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod commands;

use serenity::framework::StandardFramework;
use serenity::model::event::ResumedEvent;
use serenity::model::Ready;
use serenity::model::gateway::Ready;
use serenity::prelude::*;
use serenity::http;
use std::collections::HashSet;
Expand Down
2 changes: 1 addition & 1 deletion examples/08_env_logging/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate serenity;

use serenity::prelude::*;
use serenity::model::event::ResumedEvent;
use serenity::model::Ready;
use serenity::model::gateway::Ready;
use std::env;

struct Handler;
Expand Down
15 changes: 8 additions & 7 deletions src/builder/create_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use chrono::{DateTime, TimeZone};
use internal::prelude::*;
use model::Embed;
use model::channel::Embed;
use serde_json::Value;
use std::collections::HashMap;
use std::default::Default;
Expand Down Expand Up @@ -203,9 +203,9 @@ impl CreateEmbed {
/// Passing a string timestamp:
///
/// ```rust,no_run
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// use serenity::prelude::*;
/// use serenity::model::channel::Message;
///
/// struct Handler;
///
/// impl EventHandler for Handler {
Expand All @@ -229,9 +229,10 @@ impl CreateEmbed {
/// Note: this example isn't efficient and is for demonstrative purposes.
///
/// ```rust,no_run
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// use serenity::prelude::*;
/// use serenity::model::guild::Member;
/// use serenity::model::id::GuildId;
///
/// struct Handler;
///
/// impl EventHandler for Handler {
Expand Down
10 changes: 5 additions & 5 deletions src/builder/create_invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::default::Default;
///
/// ```rust,no_run
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// # use serenity::model::prelude::*;
///
/// struct Handler;
///
Expand Down Expand Up @@ -77,7 +77,7 @@ impl CreateInvite {
///
/// ```rust,no_run
/// # use serenity::CACHE;
/// # use serenity::model::ChannelId;
/// # use serenity::model::id::ChannelId;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl CreateInvite {
///
/// ```rust,no_run
/// # use serenity::CACHE;
/// # use serenity::model::ChannelId;
/// # use serenity::model::id::ChannelId;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
Expand Down Expand Up @@ -141,7 +141,7 @@ impl CreateInvite {
///
/// ```rust,no_run
/// # use serenity::CACHE;
/// # use serenity::model::ChannelId;
/// # use serenity::model::id::ChannelId;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
Expand Down Expand Up @@ -172,7 +172,7 @@ impl CreateInvite {
///
/// ```rust,no_run
/// # use serenity::CACHE;
/// # use serenity::model::ChannelId;
/// # use serenity::model::id::ChannelId;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
Expand Down
4 changes: 2 additions & 2 deletions src/builder/create_message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use internal::prelude::*;
use model::ReactionType;
use model::channel::ReactionType;
use std::collections::HashMap;
use std::fmt::Display;
use super::CreateEmbed;
Expand All @@ -23,7 +23,7 @@ use utils;
/// Sending a message with a content of `"test"` and applying text-to-speech:
///
/// ```rust,no_run
/// use serenity::model::ChannelId;
/// use serenity::model::id::ChannelId;
///
/// let channel_id = ChannelId(7);
///
Expand Down
10 changes: 5 additions & 5 deletions src/builder/edit_guild.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use internal::prelude::*;
use model::{ChannelId, Region, UserId, VerificationLevel};
use model::prelude::*;
use std::collections::HashMap;

/// A builder to optionally edit certain fields of a [`Guild`]. This is meant
Expand Down Expand Up @@ -56,7 +56,7 @@ impl EditGuild {
/// from the cwd and encode it in base64 to send to Discord.
///
/// ```rust,no_run
/// # use serenity::model::GuildId;
/// # use serenity::model::id::GuildId;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
Expand Down Expand Up @@ -112,12 +112,12 @@ impl EditGuild {
/// Setting the region to [`Region::UsWest`]:
///
/// ```rust,no_run
/// # use serenity::model::GuildId;
/// # use serenity::model::id::GuildId;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
/// # let mut guild = GuildId(0).get()?;
/// use serenity::model::Region;
/// use serenity::model::guild::Region;
///
/// // assuming a `guild` has already been bound
///
Expand Down Expand Up @@ -163,7 +163,7 @@ impl EditGuild {
/// Setting the verification level to [`High`][`VerificationLevel::High`]:
///
/// ```rust,ignore
/// use serenity::model::VerificationLevel;
/// use serenity::model::guild::VerificationLevel;
///
/// // assuming a `guild` has already been bound
///
Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_member.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use internal::prelude::*;
use model::{ChannelId, RoleId};
use model::id::{ChannelId, RoleId};
use std::collections::HashMap;

/// A builder which edits the properties of a [`Member`], to be used in
Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl EditProfile {
///
/// ```rust,no_run
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// # use serenity::model::prelude::*;
/// #
/// # struct Handler;
///
Expand Down
5 changes: 3 additions & 2 deletions src/builder/edit_role.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use internal::prelude::*;
use std::collections::HashMap;
use model::{Permissions, Role};
use model::guild::Role;
use model::Permissions;

/// A builer to create or edit a [`Role`] for use via a number of model methods.
///
Expand All @@ -20,7 +21,7 @@ use model::{Permissions, Role};
/// Create a hoisted, mentionable role named `"a test role"`:
///
/// ```rust,no_run
/// # use serenity::model::{ChannelId, GuildId};
/// # use serenity::model::id::{ChannelId, GuildId};
/// # let (channel_id, guild_id) = (ChannelId(1), GuildId(2));
/// #
/// // assuming a `channel_id` and `guild_id` has been bound
Expand Down
2 changes: 1 addition & 1 deletion src/builder/execute_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::default::Default;
///
/// ```rust,no_run
/// use serenity::http;
/// use serenity::model::Embed;
/// use serenity::model::channel::Embed;
/// use serenity::utils::Colour;
///
/// let id = 245037420704169985;
Expand Down
4 changes: 2 additions & 2 deletions src/builder/get_messages.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use model::MessageId;
use model::id::MessageId;
use std::collections::HashMap;

/// Builds a request for a request to the API to retrieve messages.
Expand Down Expand Up @@ -29,8 +29,8 @@ use std::collections::HashMap;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
/// use serenity::model::{ChannelId, MessageId};
/// use serenity::builder::GetMessages;
/// use serenity::model::id::{ChannelId, MessageId};
///
/// let retriever = GetMessages::default()
/// .after(MessageId(158339864557912064))
Expand Down
8 changes: 4 additions & 4 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//! [`CACHE`]: ../struct.CACHE.html
//! [`http`]: ../http/index.html

use model::*;
use model::prelude::*;
use parking_lot::RwLock;
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -168,8 +168,8 @@ impl Cache {
/// used to determine how many members have not yet been received.
///
/// ```rust,no_run
/// # use serenity::model::prelude::*;
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// # #[cfg(feature = "client")]
/// # fn main() {
Expand Down Expand Up @@ -264,8 +264,8 @@ impl Cache {
/// ```rust,no_run
/// # #[cfg(feature = "client")]
/// # fn main() {
/// # use serenity::model::prelude::*;
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// use serenity::CACHE;
///
Expand Down Expand Up @@ -379,8 +379,8 @@ impl Cache {
/// ```rust,no_run
/// # #[cfg(feature = "client")]
/// # fn main() {
/// # use serenity::model::prelude::*;
/// # use serenity::prelude::*;
/// # use serenity::model::*;
/// #
/// use serenity::CACHE;
///
Expand Down
10 changes: 5 additions & 5 deletions src/client/bridge/gateway/shard_messenger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use model::{Game, GuildId, OnlineStatus};
use model::prelude::*;
use super::{ShardClientMessage, ShardRunnerMessage};
use std::sync::mpsc::{SendError, Sender};
use websocket::message::OwnedMessage;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl ShardMessenger {
/// #
/// # let mut shard = Shard::new(mutex.clone(), mutex, [0, 1])?;
/// #
/// use serenity::model::GuildId;
/// use serenity::model::id::GuildId;
///
/// let guild_ids = vec![GuildId(81384788765712384)];
///
Expand Down Expand Up @@ -92,7 +92,7 @@ impl ShardMessenger {
/// #
/// # let mut shard = Shard::new(mutex.clone(), mutex, [0, 1])?;
/// #
/// use serenity::model::GuildId;
/// use serenity::model::id::GuildId;
///
/// let guild_ids = vec![GuildId(81384788765712384)];
///
Expand Down Expand Up @@ -146,7 +146,7 @@ impl ShardMessenger {
/// #
/// # let mut shard = Shard::new(mutex.clone(), mutex, [0, 1]).unwrap();
/// #
/// use serenity::model::Game;
/// use serenity::model::gateway::Game;
///
/// shard.set_game(Some(Game::playing("Heroes of the Storm")));
/// # Ok(())
Expand Down Expand Up @@ -227,7 +227,7 @@ impl ShardMessenger {
/// #
/// # let mut shard = Shard::new(mutex.clone(), mutex, [0, 1]).unwrap();
/// #
/// use serenity::model::OnlineStatus;
/// use serenity::model::user::OnlineStatus;
///
/// shard.set_status(OnlineStatus::DoNotDisturb);
/// # Ok(())
Expand Down
4 changes: 3 additions & 1 deletion src/client/bridge/gateway/shard_runner_message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use model::{Game, GuildId, OnlineStatus};
use model::gateway::Game;
use model::user::OnlineStatus;
use model::id::GuildId;
use websocket::message::OwnedMessage;

/// A message to send from a shard over a WebSocket.
Expand Down
Loading

0 comments on commit bcd16dd

Please sign in to comment.