pub struct ImageData { /* private fields */ }Expand description
An image sent inside a JSON request body, as an
image data URI
(data:image/png;base64,iVBORw0…).
Used by every request field that takes an image: avatars, banners, guild, role and group
DM icons, emojis, … The image is kept as [Bytes] and only base64-encoded while the body
is serialized, straight into the output without an intermediate string.
discord.js counterpart: the BufferResolvable | Base64Resolvable image options (resolved
by DataResolver.resolveImage).
§Examples
use djs_model::util::ImageData;
// A (truncated) PNG: the media type is read from the file signature.
let png = ImageData::new(&b"\x89PNG\r\n\x1a\n"[..]).unwrap();
assert_eq!(png.content_type(), "image/png");
assert_eq!(png.to_string(), "data:image/png;base64,iVBORw0KGgo=");
let svg = ImageData::with_content_type("<svg/>", "image/svg+xml");
assert_eq!(serde_json::to_string(&svg).unwrap(), r#""data:image/svg+xml;base64,PHN2Zy8+""#);
assert!(ImageData::new("not an image").is_err());Implementations§
Source§impl ImageData
impl ImageData
Sourcepub fn new(data: impl Into<Bytes>) -> Result<Self, ImageDataError>
pub fn new(data: impl Into<Bytes>) -> Result<Self, ImageDataError>
An image whose media type is detected from its file signature: PNG, JPEG, GIF, WebP or AVIF.
data can be anything that converts into [Bytes] without copying: a Vec<u8>, a
&'static [u8], …
discord.js counterpart: BufferResolvable and Base64Resolvable, as image options take them.
§Errors
ImageDataError when data does not start with one of those signatures; use
with_content_type for other formats.
Sourcepub fn with_content_type(
data: impl Into<Bytes>,
content_type: impl Into<CompactString>,
) -> Self
pub fn with_content_type( data: impl Into<Bytes>, content_type: impl Into<CompactString>, ) -> Self
An image of the given media type (e.g. image/png), which is not checked.
discord.js counterpart: BufferResolvable and Base64Resolvable, as image options take them.
Sourcepub const fn data(&self) -> &Bytes
pub const fn data(&self) -> &Bytes
The image file’s contents.
discord.js counterpart: none (discord.js turns images into data URIs internally).
Sourcepub fn content_type(&self) -> &str
pub fn content_type(&self) -> &str
The image’s media type (e.g. image/png).
discord.js counterpart: none (discord.js detects the type internally).
Trait Implementations§
impl Eq for ImageData
impl StructuralPartialEq for ImageData
Auto Trait Implementations§
impl !Freeze for ImageData
impl RefUnwindSafe for ImageData
impl Send for ImageData
impl Sync for ImageData
impl Unpin for ImageData
impl UnsafeUnpin for ImageData
impl UnwindSafe for ImageData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()] Read more§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString]. Read more