Skip to main content

Encoder

Struct Encoder 

Source
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>

Source

pub fn new(boundary: &'b [u8]) -> Result<Self, Error>

Creates an encoder for boundary (rule 1).

§Errors

Error::Boundary if the boundary is invalid.

Source

pub const fn boundary(&self) -> &'b [u8]

The boundary.

Source

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.

Source

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.

Source

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§

Source§

impl<'b> Clone for Encoder<'b>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'b> Debug for Encoder<'b>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.