pub struct Encoder<'b> { /* private fields */ }Expand description
Streaming multipart/form-data encoder.
use nostd_multipart::{Encoder, Part};
let mut enc = Encoder::new(b"xyz").unwrap();
let mut out = [0u8; 128];
let part = Part { name: b"file", filename: Some(b"a.bin"), content_type: Some(b"application/octet-stream") };
let (n, done) = enc.write_part_header(&part, &mut out).unwrap();
assert!(done);
assert_eq!(&out[..n], &b"--xyz\r\nContent-Disposition: form-data; name=\"file\"; filename=\"a.bin\"\r\nContent-Type: application/octet-stream\r\n\r\n"[..]);
// ... write the file data yourself ...
let (n, done) = enc.write_close(&mut out);
assert!(done);
assert_eq!(&out[..n], b"\r\n--xyz--\r\n");Implementations§
Source§impl<'b> Encoder<'b>
impl<'b> Encoder<'b>
Sourcepub fn write_part_header(
&mut self,
part: &Part<'_>,
out: &mut [u8],
) -> Result<(usize, bool), Error>
pub fn write_part_header( &mut self, part: &Part<'_>, out: &mut [u8], ) -> Result<(usize, bool), Error>
Writes the next bytes of a part’s prefix (CRLF for parts after the first,
--boundary CRLF, headers, empty line) into out. Returns the number of
bytes written and whether the prefix is complete. If not complete, flush
out[..written] and call again with the same part.
§Errors
Error::Header if part.content_type contains CR or LF.
Sourcepub fn write_close(&mut self, out: &mut [u8]) -> (usize, bool)
pub fn write_close(&mut self, out: &mut [u8]) -> (usize, bool)
Writes the next bytes of the close delimiter CRLF --boundary-- CRLF
into out, resumably like Self::write_part_header.
Sourcepub fn content_length(&self, parts: &[(Part<'_>, u64)]) -> u64
pub fn content_length(&self, parts: &[(Part<'_>, u64)]) -> u64
Exact length of the complete body for parts (each with its data length),
as this encoder would produce it from the start. Saturates at u64::MAX.
Trait Implementations§
Auto Trait Implementations§
impl<'b> Freeze for Encoder<'b>
impl<'b> RefUnwindSafe for Encoder<'b>
impl<'b> Send for Encoder<'b>
impl<'b> Sync for Encoder<'b>
impl<'b> Unpin for Encoder<'b>
impl<'b> UnsafeUnpin for Encoder<'b>
impl<'b> UnwindSafe for Encoder<'b>
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
Mutably borrows from an owned value. Read more