Skip to main content

base64_ng/v2/web/
one_shot_alloc.rs

1//! Fallible allocating WHATWG forgiving decode operations.
2
3use alloc::vec::Vec;
4
5use super::{ForgivingBase64, ForgivingError};
6
7impl ForgivingBase64 {
8    /// Decodes a web string into a newly allocated byte vector.
9    pub fn decode_to_vec(self, input: &str) -> Result<Vec<u8>, ForgivingError> {
10        self.decode_to_vec_with_limit(input, usize::MAX)
11    }
12
13    /// Decodes subject to an exact caller-selected output limit.
14    pub fn decode_to_vec_with_limit(
15        self,
16        input: &str,
17        max_output_len: usize,
18    ) -> Result<Vec<u8>, ForgivingError> {
19        let required = self.decoded_len(input)?;
20        if required > max_output_len {
21            return Err(ForgivingError::AllocationLimitExceeded {
22                required,
23                limit: max_output_len,
24            });
25        }
26        let mut output = Vec::new();
27        output
28            .try_reserve_exact(required)
29            .map_err(|_| ForgivingError::AllocationFailed {
30                requested: required,
31            })?;
32        output.resize(required, 0);
33        self.decode_into(input, &mut output)?;
34        Ok(output)
35    }
36}