form_data/
form.rs

1#![allow(clippy::module_name_repetitions)]
2
3use std::sync::{Arc, Mutex};
4
5use crate::{Error, Limits, Result, State};
6
7/// `FormData`
8pub struct FormData<T> {
9    pub(crate) state: Arc<Mutex<State<T>>>,
10}
11
12impl<T> FormData<T> {
13    /// Creates new `FormData` with boundary.
14    #[must_use]
15    pub fn new(t: T, boundary: &str) -> Self {
16        Self {
17            state: Arc::new(Mutex::new(State::new(
18                t,
19                boundary.as_bytes(),
20                Limits::default(),
21            ))),
22        }
23    }
24
25    /// Creates new `FormData` with boundary and limits.
26    #[must_use]
27    pub fn with_limits(t: T, boundary: &str, limits: Limits) -> Self {
28        Self {
29            state: Arc::new(Mutex::new(State::new(t, boundary.as_bytes(), limits))),
30        }
31    }
32
33    /// Gets the state.
34    #[must_use]
35    pub fn state(&self) -> Arc<Mutex<State<T>>> {
36        self.state.clone()
37    }
38
39    /// Sets Buffer max size for reading.
40    pub fn set_max_buf_size(&self, max: usize) -> Result<()> {
41        self.state
42            .try_lock()
43            .map_err(|e| Error::TryLockError(e.to_string()))?
44            .limits_mut()
45            .buffer_size = max;
46
47        Ok(())
48    }
49}