form_data/
field.rs

1use std::{
2    fmt,
3    sync::{Arc, Mutex},
4};
5
6use crate::State;
7
8/// Field
9pub struct Field<T> {
10    /// The payload size of Field.
11    pub length: usize,
12    /// The index of Field.
13    pub index: usize,
14    /// The name of Field.
15    pub name: String,
16    /// The filename of Field, optinal.
17    pub filename: Option<String>,
18    /// The `content_type` of Field, optinal.
19    pub content_type: Option<mime::Mime>,
20    /// The extras headers of Field, optinal.
21    pub headers: Option<http::HeaderMap>,
22    pub(crate) state: Option<Arc<Mutex<State<T>>>>,
23}
24
25impl<T> Field<T> {
26    /// Creates an empty field.
27    #[must_use]
28    pub fn empty() -> Self {
29        Self {
30            index: 0,
31            length: 0,
32            name: String::new(),
33            filename: None,
34            content_type: None,
35            headers: None,
36            state: None,
37        }
38    }
39
40    /// Gets mutable headers.
41    #[must_use]
42    pub fn headers_mut(&mut self) -> &mut Option<http::HeaderMap> {
43        &mut self.headers
44    }
45
46    /// Gets mutable state.
47    #[must_use]
48    pub fn state_mut(&mut self) -> &mut Option<Arc<Mutex<State<T>>>> {
49        &mut self.state
50    }
51
52    /// Gets the status of state.
53    #[must_use]
54    pub fn consumed(&self) -> bool {
55        self.state.is_none()
56    }
57}
58
59impl<T> fmt::Debug for Field<T> {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        f.debug_struct("Field")
62            .field("name", &self.name)
63            .field("filename", &self.filename)
64            .field("content_type", &self.content_type)
65            .field("index", &self.index)
66            .field("length", &self.length)
67            .field("headers", &self.headers)
68            .field("consumed", &self.state.is_none())
69            .finish()
70    }
71}