base_web_multipart/part_headers.rs
1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use crate::{ContentDisposition, ContentTransferEncoding, ContentType};
5use base_message::FieldSection;
6
7/// The header fields of a part that this crate understands, as a [`FieldSection`] to interpret a [`PartHead`](crate::PartHead) with. Other fields are ignored.
8///
9/// Each is a singleton, so a second `Content-Type` or `Content-Disposition` is an error rather than a choice between them. A transfer encoding other than an identity one is an invalid value, since the content would otherwise be misread. Flatten this section into one of your own to read other fields too.
10///
11/// ```
12/// use base_web_multipart::{ParameterRules, PartHead, PartHeaders};
13/// use base_message::FieldSection;
14///
15/// let headers = PartHeaders::interpret([("Content-Disposition", "form-data; name=\"a\""), ("Content-Type", "text/plain")])?;
16/// let disposition = headers.content_disposition.as_ref().expect("a disposition");
17/// assert_eq!(disposition.name(ParameterRules::FormData)?.as_deref(), Some("a"));
18/// assert!(PartHeaders::interpret([("Content-Transfer-Encoding", "base64")]).is_err());
19/// # Ok::<(), Box<dyn std::error::Error>>(())
20/// ```
21#[derive(Clone, Debug, PartialEq, Eq, FieldSection)]
22#[non_exhaustive]
23pub struct PartHeaders<'a> {
24 /// The part's media type, if it has one.
25 #[field("content-type")]
26 pub content_type: Option<ContentType>,
27 /// The part's disposition, such as a form field's name and a file's name.
28 #[field("content-disposition")]
29 pub content_disposition: Option<ContentDisposition<'a>>,
30 /// The part's transfer encoding, which can only be an identity encoding.
31 #[field("content-transfer-encoding")]
32 pub content_transfer_encoding: Option<ContentTransferEncoding>,
33}