Skip to content

Commit 9d74ead

Browse files
committed
fix builders, test, fixedstring, type resolved variant of media items
1 parent 860b07c commit 9d74ead

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

examples/e01_basic_ping_bot/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ authors = ["my name <[email protected]>"]
55
edition.workspace = true
66

77
[dependencies]
8-
serde_json = "1.0.139"
98
serenity = { path = "../../", default-features = false, features = ["gateway", "model", "rustls_backend"] }
10-
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
11-
tracing-subscriber = "0.3.19"
9+
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }

src/builder/create_components.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ pub enum CreateComponent<'a> {
6565
ActionRow(CreateActionRow<'a>),
6666
/// A section, V2 component.
6767
Section(CreateSection<'a>),
68+
TextDisplay(CreateTextDisplay<'a>),
69+
Thumbnail(CreateThumbnail<'a>),
70+
MediaGallery(CreateMediaGallery<'a>),
71+
File(CreateFile<'a>),
72+
Separator(CreateSeparator),
73+
Container(CreateContainer<'a>),
6874
}
6975

7076
#[derive(Clone, Debug, Serialize)]
@@ -78,7 +84,6 @@ pub struct CreateSection<'a> {
7884
}
7985

8086
impl<'a> CreateSection<'a> {
81-
// TODO: change type
8287
pub fn new(
8388
components: impl Into<Cow<'a, [CreateSectionComponent<'a>]>>,
8489
accessory: CreateSectionAccessory<'a>,
@@ -190,18 +195,18 @@ impl<'a> CreateUnfurledMediaItem<'a> {
190195
pub struct CreateMediaGallery<'a> {
191196
#[serde(rename = "type")]
192197
kind: StaticU8<12>,
193-
items: Cow<'a, [CreateSectionComponent<'a>]>,
198+
items: Cow<'a, [CreateMediaGalleryItem<'a>]>,
194199
}
195200

196201
impl<'a> CreateMediaGallery<'a> {
197-
pub fn new(items: impl Into<Cow<'a, [CreateSectionComponent<'a>]>>) -> Self {
202+
pub fn new(items: impl Into<Cow<'a, [CreateMediaGalleryItem<'a>]>>) -> Self {
198203
CreateMediaGallery {
199204
kind: StaticU8::<12>,
200205
items: items.into(),
201206
}
202207
}
203208

204-
pub fn items(mut self, items: impl Into<Cow<'a, [CreateSectionComponent<'a>]>>) -> Self {
209+
pub fn items(mut self, items: impl Into<Cow<'a, [CreateMediaGalleryItem<'a>]>>) -> Self {
205210
self.items = items.into();
206211
self
207212
}
@@ -217,6 +222,31 @@ pub struct CreateMediaGalleryItem<'a> {
217222
spoiler: Option<bool>,
218223
}
219224

225+
impl<'a> CreateMediaGalleryItem<'a> {
226+
pub fn new(media: CreateUnfurledMediaItem<'a>) -> Self {
227+
CreateMediaGalleryItem {
228+
media,
229+
description: None,
230+
spoiler: None,
231+
}
232+
}
233+
234+
pub fn media(mut self, media: CreateUnfurledMediaItem<'a>) -> Self {
235+
self.media = media;
236+
self
237+
}
238+
239+
pub fn description(mut self, description: impl Into<Cow<'a, str>>) -> Self {
240+
self.description = Some(description.into());
241+
self
242+
}
243+
244+
pub fn spoiler(mut self, spoiler: bool) -> Self {
245+
self.spoiler = Some(spoiler);
246+
self
247+
}
248+
}
249+
220250
#[derive(Clone, Debug, Serialize)]
221251
#[must_use]
222252
pub struct CreateFile<'a> {

src/model/application/component.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use nonmax::NonMaxU32;
12
use serde::de::Error as DeError;
23
use serde::ser::{Serialize, Serializer};
34
use serde_json::value::RawValue;
@@ -52,10 +53,6 @@ pub enum Component {
5253

5354
// TODO: add something like this to every variant.
5455
// The component type, it will always be [`ComponentType::Thing`].
55-
// #[serde(rename = "type")]
56-
// pub kind: ComponentType,
57-
58-
// TODO: use fixedstring is places i missed when i find suitable lengths
5956

6057
impl<'de> Deserialize<'de> for Component {
6158
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
@@ -114,15 +111,50 @@ pub struct Section {
114111
#[non_exhaustive]
115112
pub struct Thumbnail {
116113
media: UnfurledMediaItem,
117-
description: Option<String>,
114+
description: Option<FixedString<u16>>,
118115
spoiler: Option<bool>,
119116
}
120117

118+
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
119+
#[derive(Clone, Debug, Deserialize, Serialize)]
120+
#[non_exhaustive]
121+
#[serde(untagged)]
122+
pub enum MediaItem {
123+
Resolved(ResolvedUnfurledMediaItem),
124+
Unresolved(UnfurledMediaItem),
125+
}
126+
121127
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
122128
#[derive(Clone, Debug, Deserialize, Serialize)]
123129
#[non_exhaustive]
124130
pub struct UnfurledMediaItem {
125-
url: String,
131+
url: FixedString<u16>,
132+
}
133+
134+
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
135+
#[derive(Clone, Debug, Deserialize, Serialize)]
136+
#[non_exhaustive]
137+
pub struct ResolvedUnfurledMediaItem {
138+
url: FixedString<u16>,
139+
proxy_url: FixedString<u16>,
140+
width: NonMaxU32,
141+
height: NonMaxU32,
142+
content_type: FixedString,
143+
loading_state: UnfurledMediaItemLoadingState,
144+
}
145+
146+
enum_number! {
147+
/// The loading state of the media item.
148+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
149+
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
150+
#[non_exhaustive]
151+
pub enum UnfurledMediaItemLoadingState {
152+
DiscordUnknown = 0,
153+
Loading = 1,
154+
LoadingSuccess = 2,
155+
LoadingNotFound = 3,
156+
_ => Unknown(u8),
157+
}
126158
}
127159

128160
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
@@ -147,7 +179,7 @@ pub enum SectionComponent {
147179
#[derive(Clone, Debug, Deserialize, Serialize)]
148180
#[non_exhaustive]
149181
pub struct TextDisplay {
150-
content: String,
182+
content: FixedString<u16>,
151183
}
152184

153185
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
@@ -162,7 +194,7 @@ pub struct MediaGallery {
162194
#[non_exhaustive]
163195
pub struct MediaGalleryItem {
164196
media: UnfurledMediaItem,
165-
description: Option<String>,
197+
description: Option<FixedString<u16>>,
166198
spoiler: Option<bool>,
167199
}
168200

0 commit comments

Comments
 (0)