torrust_tracker_contrib_bencode/reference/
decode_opt.rs

1const DEFAULT_MAX_RECURSION: usize = 50;
2const DEFAULT_CHECK_KEY_SORT: bool = false;
3const DEFAULT_ENFORCE_FULL_DECODE: bool = true;
4
5/// Stores decoding options for modifying decode behavior.
6#[derive(Copy, Clone)]
7#[allow(clippy::module_name_repetitions)]
8pub struct BDecodeOpt {
9    max_recursion: usize,
10    check_key_sort: bool,
11    enforce_full_decode: bool,
12}
13
14impl BDecodeOpt {
15    /// Create a new `BDecodeOpt` object.
16    #[must_use]
17    pub fn new(max_recursion: usize, check_key_sort: bool, enforce_full_decode: bool) -> BDecodeOpt {
18        BDecodeOpt {
19            max_recursion,
20            check_key_sort,
21            enforce_full_decode,
22        }
23    }
24
25    /// Maximum limit allowed when decoding bencode.
26    #[must_use]
27    pub fn max_recursion(&self) -> usize {
28        self.max_recursion
29    }
30
31    /// Whether or not an error should be thrown for out of order dictionary keys.
32    #[must_use]
33    pub fn check_key_sort(&self) -> bool {
34        self.check_key_sort
35    }
36
37    /// Whether or not we enforce that the decoded bencode must make up all of the input
38    /// bytes or not.
39    ///
40    /// It may be useful to disable this if for example, the input bencode is prepended to
41    /// some payload and you would like to disassociate it. In this case, to find where the
42    /// rest of the payload starts that wasn't decoded, get the bencode buffer, and call `len()`.
43    #[must_use]
44    pub fn enforce_full_decode(&self) -> bool {
45        self.enforce_full_decode
46    }
47}
48
49impl Default for BDecodeOpt {
50    fn default() -> BDecodeOpt {
51        BDecodeOpt::new(DEFAULT_MAX_RECURSION, DEFAULT_CHECK_KEY_SORT, DEFAULT_ENFORCE_FULL_DECODE)
52    }
53}