torrust_tracker_contrib_bencode/access/
bencode.rs

1use crate::access::dict::BDictAccess;
2use crate::access::list::BListAccess;
3
4/// Abstract representation of a `BencodeRef` object.
5pub enum RefKind<'a, K, V> {
6    /// Bencode Integer.
7    Int(i64),
8    /// Bencode Bytes.
9    Bytes(&'a [u8]),
10    /// Bencode List.
11    List(&'a dyn BListAccess<V>),
12    /// Bencode Dictionary.
13    Dict(&'a dyn BDictAccess<K, V>),
14}
15
16/// Trait for read access to some bencode type.
17pub trait BRefAccess: Sized {
18    type BKey;
19    type BType: BRefAccess<BKey = Self::BKey>;
20
21    /// Access the bencode as a `BencodeRefKind`.
22    fn kind(&self) -> RefKind<'_, Self::BKey, Self::BType>;
23
24    /// Attempt to access the bencode as a `str`.
25    fn str(&self) -> Option<&str>;
26
27    /// Attempt to access the bencode as an `i64`.
28    fn int(&self) -> Option<i64>;
29
30    /// Attempt to access the bencode as an `[u8]`.
31    fn bytes(&self) -> Option<&[u8]>;
32
33    /// Attempt to access the bencode as an `BListAccess`.
34    fn list(&self) -> Option<&dyn BListAccess<Self::BType>>;
35
36    /// Attempt to access the bencode as an `BDictAccess`.
37    fn dict(&self) -> Option<&dyn BDictAccess<Self::BKey, Self::BType>>;
38}
39
40/// Trait for extended read access to some bencode type.
41///
42/// Use this trait when you want to make sure that the lifetime of
43/// the underlying buffers is tied to the lifetime of the backing
44/// bencode buffer.
45pub trait BRefAccessExt<'a>: BRefAccess {
46    /// Attempt to access the bencode as a `str`.
47    fn str_ext(&self) -> Option<&'a str>;
48
49    /// Attempt to access the bencode as an `[u8]`.
50    fn bytes_ext(&self) -> Option<&'a [u8]>;
51}
52
53impl<'a, T> BRefAccess for &'a T
54where
55    T: BRefAccess,
56{
57    type BKey = T::BKey;
58    type BType = T::BType;
59
60    fn kind(&self) -> RefKind<'_, Self::BKey, Self::BType> {
61        (*self).kind()
62    }
63
64    fn str(&self) -> Option<&str> {
65        (*self).str()
66    }
67
68    fn int(&self) -> Option<i64> {
69        (*self).int()
70    }
71
72    fn bytes(&self) -> Option<&[u8]> {
73        (*self).bytes()
74    }
75
76    fn list(&self) -> Option<&dyn BListAccess<Self::BType>> {
77        (*self).list()
78    }
79
80    fn dict(&self) -> Option<&dyn BDictAccess<Self::BKey, Self::BType>> {
81        (*self).dict()
82    }
83}
84
85impl<'a: 'b, 'b, T> BRefAccessExt<'a> for &'b T
86where
87    T: BRefAccessExt<'a>,
88{
89    fn str_ext(&self) -> Option<&'a str> {
90        (*self).str_ext()
91    }
92
93    fn bytes_ext(&self) -> Option<&'a [u8]> {
94        (*self).bytes_ext()
95    }
96}
97
98/// Abstract representation of a `BencodeMut` object.
99pub enum MutKind<'a, K, V> {
100    /// Bencode Integer.
101    Int(i64),
102    /// Bencode Bytes.
103    Bytes(&'a [u8]),
104    /// Bencode List.
105    List(&'a mut dyn BListAccess<V>),
106    /// Bencode Dictionary.
107    Dict(&'a mut dyn BDictAccess<K, V>),
108}
109
110/// Trait for write access to some bencode type.
111pub trait BMutAccess: Sized + BRefAccess {
112    /// Access the bencode as a `BencodeMutKind`.
113    fn kind_mut(&mut self) -> MutKind<'_, Self::BKey, Self::BType>;
114
115    /// Attempt to access the bencode as a mutable `BListAccess`.
116    fn list_mut(&mut self) -> Option<&mut dyn BListAccess<Self::BType>>;
117
118    /// Attempt to access the bencode as a mutable `BDictAccess`.
119    fn dict_mut(&mut self) -> Option<&mut dyn BDictAccess<Self::BKey, Self::BType>>;
120}