Skip to content

Commit b3cd5d6

Browse files
mkrasnitskiGnomedDev
authored andcommitted
Fix public doctests that are marked ignore (#2976)
Doctests on public items should be maintained accurate as the API changes, so having them be type-checked makes the most sense. I removed the doctest for the `CacheUpdate` trait as it was really out of date and not really possible to fix up.
1 parent 33caa0e commit b3cd5d6

File tree

10 files changed

+62
-144
lines changed

10 files changed

+62
-144
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ docs.
4141

4242
A basic ping-pong bot looks like:
4343

44-
```rust,ignore
44+
```rust
4545
use std::env;
4646

4747
use serenity::async_trait;

src/cache/cache_update.rs

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,98 +5,10 @@ use super::Cache;
55
/// This may be implemented on a type and used to update the cache via [`Cache::update`].
66
///
77
/// **Info**: You may not access the fields of the cache, as they are public for the crate only.
8-
///
9-
/// # Examples
10-
///
11-
/// Creating a custom struct implementation to update the cache with:
12-
///
13-
/// ```rust,ignore
14-
/// use std::collections::hash_map::Entry;
15-
///
16-
/// use serde_json::json;
17-
/// use serenity::cache::{Cache, CacheUpdate};
18-
/// use serenity::model::id::UserId;
19-
/// use serenity::model::user::User;
20-
///
21-
/// // For example, an update to the user's record in the database was
22-
/// // published to a pubsub channel.
23-
/// struct DatabaseUserUpdate {
24-
/// user_avatar: Option<String>,
25-
/// user_discriminator: u16,
26-
/// user_id: UserId,
27-
/// user_is_bot: bool,
28-
/// user_name: String,
29-
/// }
30-
///
31-
/// #[serenity::async_trait]
32-
/// impl CacheUpdate for DatabaseUserUpdate {
33-
/// // A copy of the old user's data, if it existed in the cache.
34-
/// type Output = User;
35-
///
36-
/// async fn update(&mut self, cache: &Cache) -> Option<Self::Output> {
37-
/// // If an entry for the user already exists, update its fields.
38-
/// match cache.users.entry(self.user_id) {
39-
/// Entry::Occupied(entry) => {
40-
/// let user = entry.get();
41-
/// let old_user = user.clone();
42-
///
43-
/// user.bot = self.user_is_bot;
44-
/// user.discriminator = self.user_discriminator;
45-
/// user.id = self.user_id;
46-
///
47-
/// if user.avatar != self.user_avatar {
48-
/// user.avatar = self.user_avatar.clone();
49-
/// }
50-
///
51-
/// if user.name != self.user_name {
52-
/// user.name = self.user_name.clone();
53-
/// }
54-
///
55-
/// // Return the old copy for the user's sake.
56-
/// Some(old_user)
57-
/// },
58-
/// Entry::Vacant(entry) => {
59-
/// // We can convert a [`Value`] to a User for test
60-
/// // purposes.
61-
/// let user = from_value::<User>(json!({
62-
/// "id": self.user_id,
63-
/// "avatar": self.user_avatar.clone(),
64-
/// "bot": self.user_is_bot,
65-
/// "discriminator": self.user_discriminator,
66-
/// "username": self.user_name.clone(),
67-
/// })).expect("Error making user");
68-
///
69-
/// entry.insert(user);
70-
///
71-
/// // There was no old copy, so return None.
72-
/// None
73-
/// },
74-
/// }
75-
/// }
76-
/// }
77-
///
78-
/// # async fn run() {
79-
/// // Create an instance of the cache.
80-
/// let mut cache = Cache::new();
81-
///
82-
/// // This is a sample pubsub message that you might receive from your
83-
/// // database.
84-
/// let mut update_message = DatabaseUserUpdate {
85-
/// user_avatar: None,
86-
/// user_discriminator: 6082,
87-
/// user_id: UserId::new(379740138303127564),
88-
/// user_is_bot: true,
89-
/// user_name: "TofuBot".to_owned(),
90-
/// };
91-
///
92-
/// // Update the cache with the message.
93-
/// cache.update(&mut update_message).await;
94-
/// # }
95-
/// ```
968
pub trait CacheUpdate {
979
/// The return type of an update.
9810
///
99-
/// If there is nothing to return, specify this type as an unit (`()`).
11+
/// If there is nothing to return, specify this type to be the unit `()`.
10012
type Output;
10113

10214
/// Updates the cache with the implementation.

src/gateway/sharding/shard_messenger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ impl ShardMessenger {
136136
///
137137
/// Set the current user as playing `"Heroes of the Storm"` and being online:
138138
///
139-
/// ```rust,ignore
139+
/// ```rust,no_run
140140
/// # use serenity::gateway::Shard;
141-
/// # async fn run(shard: Shard) -> Result<(), Box<dyn std::error::Error>> {
141+
/// # async fn run(mut shard: Shard) -> Result<(), Box<dyn std::error::Error>> {
142142
/// use serenity::gateway::ActivityData;
143143
/// use serenity::model::user::OnlineStatus;
144144
///

src/model/channel/guild_channel.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,18 @@ impl GuildChannel {
224224
///
225225
/// Create an invite that can only be used 5 times:
226226
///
227-
/// ```rust,ignore
228-
/// let builder = CreateBuilder::default().max_uses(5);
229-
/// let invite = channel.create_invite(&context, builder).await;
227+
/// ```rust,no_run
228+
/// # use serenity::builder::CreateInvite;
229+
/// # use serenity::http::Http;
230+
/// # use serenity::model::channel::GuildChannel;
231+
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
232+
/// # let channel: GuildChannel = unimplemented!();
233+
/// # let http: Http = unimplemented!();
234+
///
235+
/// let builder = CreateInvite::new().max_uses(5);
236+
/// let invite = channel.create_invite(&http, builder).await;
237+
/// # Ok(())
238+
/// # }
230239
/// ```
231240
///
232241
/// # Errors

src/model/guild/member.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,18 @@ impl Member {
270270
///
271271
/// # Examples
272272
///
273-
/// Kick a member from its guild:
274-
///
275-
/// ```rust,ignore
273+
/// Kick a member from the guild:
274+
///
275+
/// ```rust,no_run
276+
/// # use serenity::http::Http;
277+
/// # use serenity::model::guild::Member;
278+
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
279+
/// # let http: Http = unimplemented!();
280+
/// # let member: Member = unimplemented!();
276281
/// // assuming a `member` has already been bound
277-
/// match member.kick(None).await {
278-
/// Ok(()) => println!("Successfully kicked member"),
279-
/// Err(Error::Model(ModelError::GuildNotFound)) => {
280-
/// println!("Couldn't determine guild of member");
281-
/// },
282-
/// _ => {},
283-
/// }
282+
/// member.kick(&http, None).await?;
283+
/// # Ok(())
284+
/// # }
284285
/// ```
285286
///
286287
/// # Errors
@@ -325,14 +326,6 @@ impl Member {
325326

326327
/// Returns the guild-level permissions for the member.
327328
///
328-
/// # Examples
329-
///
330-
/// ```rust,ignore
331-
/// // assuming there's a `member` variable gotten from anything.
332-
/// println!("The permission bits for the member are: {}",
333-
/// member.permissions(&cache).expect("permissions").bits());
334-
/// ```
335-
///
336329
/// # Errors
337330
///
338331
/// Returns a [`ModelError::GuildNotFound`] if the guild the member's in could not be
@@ -441,13 +434,6 @@ impl Member {
441434
impl fmt::Display for Member {
442435
/// Mentions the user so that they receive a notification.
443436
///
444-
/// # Examples
445-
///
446-
/// ```rust,ignore
447-
/// // assumes a `member` has already been bound
448-
/// println!("{} is a member!", member);
449-
/// ```
450-
///
451437
/// This is in the format of `<@USER_ID>`.
452438
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
453439
fmt::Display::fmt(&self.user.mention(), f)

src/model/guild/mod.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,18 @@ impl Guild {
401401
///
402402
/// Ban a member and remove all messages they've sent in the last 4 days:
403403
///
404-
/// ```rust,ignore
405-
/// // assumes a `user` and `guild` have already been bound
406-
/// let _ = guild.ban(user, 4, None);
404+
/// ```rust,no_run
405+
/// # use serenity::http::Http;
406+
/// # use serenity::model::guild::Guild;
407+
/// # use serenity::model::id::UserId;
408+
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
409+
/// # let http: Http = unimplemented!();
410+
/// # let guild: Guild = unimplemented!();
411+
/// # let user_id: UserId = unimplemented!();
412+
/// // assumes a `user_id` and `guild` have already been bound
413+
/// guild.ban(&http, user_id, 4, None).await?;
414+
/// # Ok(())
415+
/// # }
407416
/// ```
408417
///
409418
/// # Errors
@@ -524,10 +533,14 @@ impl Guild {
524533
///
525534
/// Create a guild called `"test"` in the [US West region] with no icon:
526535
///
527-
/// ```rust,ignore
528-
/// use serenity::model::Guild;
529-
///
530-
/// let _guild = Guild::create_guild(&http, "test", None).await;
536+
/// ```rust,no_run
537+
/// # use serenity::http::Http;
538+
/// use serenity::model::guild::Guild;
539+
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
540+
/// # let http: Http = unimplemented!();
541+
/// let guild = Guild::create(&http, "test", None).await;
542+
/// # Ok(())
543+
/// # }
531544
/// ```
532545
///
533546
/// # Errors

src/model/guild/partial_guild.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,18 @@ impl PartialGuild {
270270
///
271271
/// Ban a member and remove all messages they've sent in the last 4 days:
272272
///
273-
/// ```rust,ignore
274-
/// // assumes a `user` and `guild` have already been bound
275-
/// let _ = guild.ban(user, 4);
273+
/// ```rust,no_run
274+
/// # use serenity::http::Http;
275+
/// # use serenity::model::guild::PartialGuild;
276+
/// # use serenity::model::id::UserId;
277+
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
278+
/// # let http: Http = unimplemented!();
279+
/// # let guild: PartialGuild = unimplemented!();
280+
/// # let user_id: UserId = unimplemented!();
281+
/// // assumes a `user_id` and `guild` have already been bound
282+
/// guild.ban(&http, user_id, 4, None).await?;
283+
/// # Ok(())
284+
/// # }
276285
/// ```
277286
///
278287
/// # Errors

src/model/user.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use crate::model::utils::avatar_url;
2727
/// ```rust,ignore
2828
/// use std::num::NonZeroU16;
2929
///
30+
/// use serde::{Deserialize, Serialize};
31+
///
3032
/// #[derive(Deserialize, Serialize)]
3133
/// struct A {
3234
/// #[serde(with = "discriminator")]
@@ -458,15 +460,6 @@ impl User {
458460
/// Check if a user has a [`Role`]. This will retrieve the [`Guild`] from the [`Cache`] if it
459461
/// is available, and then check if that guild has the given [`Role`].
460462
///
461-
/// # Examples
462-
///
463-
/// Check if a guild has a [`Role`] by Id:
464-
///
465-
/// ```rust,ignore
466-
/// // Assumes a 'guild_id' and `role_id` have already been bound
467-
/// let _ = message.author.has_role(guild_id, role_id);
468-
/// ```
469-
///
470463
/// [`Cache`]: crate::cache::Cache
471464
///
472465
/// # Errors

src/model/webhook.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,6 @@ impl Webhook {
470470

471471
/// Returns the url of the webhook.
472472
///
473-
/// ```rust,ignore
474-
/// assert_eq!(hook.url(), "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV")
475-
/// ```
476-
///
477473
/// # Errors
478474
///
479475
/// Returns an [`Error::Model`] if the [`Self::token`] is [`None`].

src/utils/message_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl MessageBuilder {
183183
///
184184
/// Pushing a Rust codeblock:
185185
///
186-
/// ```rust,ignore
186+
/// ```rust,no_run
187187
/// use serenity::utils::MessageBuilder;
188188
///
189189
/// let code = r#"

0 commit comments

Comments
 (0)