1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::default::Default;

const DEFAULT_MAX_RECURSION: usize = 50;
const DEFAULT_CHECK_KEY_SORT: bool = false;
const DEFAULT_ENFORCE_FULL_DECODE: bool = true;

/// Stores decoding options for modifying decode behavior.
#[derive(Copy, Clone)]
#[allow(clippy::module_name_repetitions)]
pub struct BDecodeOpt {
    max_recursion: usize,
    check_key_sort: bool,
    enforce_full_decode: bool,
}

impl BDecodeOpt {
    /// Create a new `BDecodeOpt` object.
    #[must_use]
    pub fn new(max_recursion: usize, check_key_sort: bool, enforce_full_decode: bool) -> BDecodeOpt {
        BDecodeOpt {
            max_recursion,
            check_key_sort,
            enforce_full_decode,
        }
    }

    /// Maximum limit allowed when decoding bencode.
    #[must_use]
    pub fn max_recursion(&self) -> usize {
        self.max_recursion
    }

    /// Whether or not an error should be thrown for out of order dictionary keys.
    #[must_use]
    pub fn check_key_sort(&self) -> bool {
        self.check_key_sort
    }

    /// Whether or not we enforce that the decoded bencode must make up all of the input
    /// bytes or not.
    ///
    /// It may be useful to disable this if for example, the input bencode is prepended to
    /// some payload and you would like to disassociate it. In this case, to find where the
    /// rest of the payload starts that wasn't decoded, get the bencode buffer, and call len().
    #[must_use]
    pub fn enforce_full_decode(&self) -> bool {
        self.enforce_full_decode
    }
}

impl Default for BDecodeOpt {
    fn default() -> BDecodeOpt {
        BDecodeOpt::new(DEFAULT_MAX_RECURSION, DEFAULT_CHECK_KEY_SORT, DEFAULT_ENFORCE_FULL_DECODE)
    }
}