diff options
| author | Zeyla Hellyer <[email protected]> | 2017-06-04 14:06:42 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-06-04 14:38:24 -0700 |
| commit | 0dda329a7507adadab04c9296c763d3cad7331b6 (patch) | |
| tree | 49783cadcd1704195721bf0232a48e10619fd50c /src/http/mod.rs | |
| parent | Make http::AttachmentType only use borrowed values (diff) | |
| download | serenity-0dda329a7507adadab04c9296c763d3cad7331b6.tar.xz serenity-0dda329a7507adadab04c9296c763d3cad7331b6.zip | |
Make http::send_files accept a slice of bytes
Sometimes a user might be working with a buffer of bytes in-memory and
not writing to the disk, so `http::AttachmentType` should accept an
&[u8].
Diffstat (limited to 'src/http/mod.rs')
| -rw-r--r-- | src/http/mod.rs | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs index a2575a0..7a3b729 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -47,7 +47,7 @@ use std::default::Default; use std::fmt::Write as FmtWrite; use std::fs::File; use std::io::{ErrorKind as IoErrorKind, Read}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; use ::constants; use ::internal::prelude::*; @@ -1447,8 +1447,11 @@ pub fn send_files<'a, T>(channel_id: u64, files: Vec<T>, map: JsonMap) for file in files { match file.into() { + AttachmentType::Bytes((mut bytes, filename)) => { + request.write_stream(&file_num, &mut bytes, Some(filename), None)?; + }, AttachmentType::File((mut f, filename)) => { - request.write_stream(&file_num, &mut f, Some(&filename), None)?; + request.write_stream(&file_num, &mut f, Some(filename), None)?; }, AttachmentType::Path(p) => { request.write_file(&file_num, &p)?; @@ -1605,16 +1608,22 @@ fn verify(expected_status_code: u16, mut response: HyperResponse) -> Result<()> Err(Error::Http(HttpError::InvalidRequest(response.status))) } -use std::path::Path; - /// Enum that allows a user to pass a `Path` or a `File` type to `send_files` pub enum AttachmentType<'a> { + /// Indicates that the `AttachmentType` is a byte slice with a filename. + Bytes((&'a [u8], &'a str)), /// Indicates that the `AttachmentType` is a `File` File((&'a File, &'a str)), /// Indicates that the `AttachmentType` is a `Path` Path(&'a Path), } +impl<'a> From<(&'a [u8], &'a str)> for AttachmentType<'a> { + fn from(params: (&'a [u8], &'a str)) -> AttachmentType { + AttachmentType::Bytes(params) + } +} + impl<'a> From<&'a str> for AttachmentType<'a> { fn from(s: &'a str) -> AttachmentType { AttachmentType::Path(Path::new(s)) |