Skip to content

Commit 06857d2

Browse files
committedAug 25, 2024
Merge torrust#1021: update deps including bencode contrib
2c4bdab chore: update to v1 of derive more (Cameron Garnham) c5a724e chore: update deps (Cameron Garnham) dcb7770 chore: update contrib bencode (Cameron Garnham) Pull request description: ACKs for top commit: da2ce7: ACK 2c4bdab Tree-SHA512: eabbf3f483aab2a9f596eb31f25606bc69929424f285287f2f26890af63cc5db3460693ffa87381140fb82a1b4cdaf4f3be82304c900126f0bcd2443495edade
2 parents f79a019 + 2c4bdab commit 06857d2

File tree

15 files changed

+204
-311
lines changed

15 files changed

+204
-311
lines changed
 

‎Cargo.lock

+83-85
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ chrono = { version = "0", default-features = false, features = ["clock"] }
4141
clap = { version = "4", features = ["derive", "env"] }
4242
crossbeam-skiplist = "0"
4343
dashmap = "6"
44-
derive_more = "0"
44+
derive_more = { version = "1", features = ["as_ref", "constructor", "from"] }
4545
figment = "0"
4646
futures = "0"
4747
futures-util = "0"
@@ -66,7 +66,7 @@ serde_bencode = "0"
6666
serde_bytes = "0"
6767
serde_json = { version = "1", features = ["preserve_order"] }
6868
serde_repr = "0"
69-
serde_with = { version = "3.9.0", features = ["json"] }
69+
serde_with = { version = "3", features = ["json"] }
7070
thiserror = "1"
7171
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
7272
torrust-tracker-clock = { version = "3.0.0-beta-develop", path = "packages/clock" }

‎contrib/bencode/Cargo.toml

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@ rust-version.workspace = true
1616
version.workspace = true
1717

1818
[dependencies]
19-
error-chain = "0"
19+
thiserror = "1"
2020

2121
[dev-dependencies]
2222
criterion = "0"
2323

24-
[[test]]
25-
name = "test"
26-
path = "test/mod.rs"
27-
2824
[[bench]]
2925
harness = false
3026
name = "bencode_benchmark"

‎contrib/bencode/src/access/convert.rs

+30-48
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::access::bencode::{BRefAccess, BRefAccessExt};
33
use crate::access::dict::BDictAccess;
44
use crate::access::list::BListAccess;
5-
use crate::{BencodeConvertError, BencodeConvertErrorKind};
5+
use crate::BencodeConvertError;
66

77
/// Trait for extended casting of bencode objects and converting conversion errors into application specific errors.
88
pub trait BConvertExt: BConvert {
@@ -12,12 +12,10 @@ pub trait BConvertExt: BConvert {
1212
B: BRefAccessExt<'a>,
1313
E: AsRef<[u8]>,
1414
{
15-
bencode.bytes_ext().ok_or(
16-
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
17-
key: error_key.as_ref().to_owned(),
18-
expected_type: "Bytes".to_owned(),
19-
})),
20-
)
15+
bencode.bytes_ext().ok_or(self.handle_error(BencodeConvertError::WrongType {
16+
key: error_key.as_ref().to_owned(),
17+
expected_type: "Bytes".to_owned(),
18+
}))
2119
}
2220

2321
/// See `BConvert::convert_str`.
@@ -26,12 +24,10 @@ pub trait BConvertExt: BConvert {
2624
B: BRefAccessExt<'a>,
2725
E: AsRef<[u8]>,
2826
{
29-
bencode.str_ext().ok_or(
30-
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
31-
key: error_key.as_ref().to_owned(),
32-
expected_type: "UTF-8 Bytes".to_owned(),
33-
})),
34-
)
27+
bencode.str_ext().ok_or(self.handle_error(BencodeConvertError::WrongType {
28+
key: error_key.as_ref().to_owned(),
29+
expected_type: "UTF-8 Bytes".to_owned(),
30+
}))
3531
}
3632

3733
/// See `BConvert::lookup_and_convert_bytes`.
@@ -77,12 +73,10 @@ pub trait BConvert {
7773
B: BRefAccess,
7874
E: AsRef<[u8]>,
7975
{
80-
bencode.int().ok_or(
81-
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
82-
key: error_key.as_ref().to_owned(),
83-
expected_type: "Integer".to_owned(),
84-
})),
85-
)
76+
bencode.int().ok_or(self.handle_error(BencodeConvertError::WrongType {
77+
key: error_key.as_ref().to_owned(),
78+
expected_type: "Integer".to_owned(),
79+
}))
8680
}
8781

8882
/// Attempt to convert the given bencode value into bytes.
@@ -93,12 +87,10 @@ pub trait BConvert {
9387
B: BRefAccess,
9488
E: AsRef<[u8]>,
9589
{
96-
bencode.bytes().ok_or(
97-
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
98-
key: error_key.as_ref().to_owned(),
99-
expected_type: "Bytes".to_owned(),
100-
})),
101-
)
90+
bencode.bytes().ok_or(self.handle_error(BencodeConvertError::WrongType {
91+
key: error_key.as_ref().to_owned(),
92+
expected_type: "Bytes".to_owned(),
93+
}))
10294
}
10395

10496
/// Attempt to convert the given bencode value into a UTF-8 string.
@@ -109,12 +101,10 @@ pub trait BConvert {
109101
B: BRefAccess,
110102
E: AsRef<[u8]>,
111103
{
112-
bencode.str().ok_or(
113-
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
114-
key: error_key.as_ref().to_owned(),
115-
expected_type: "UTF-8 Bytes".to_owned(),
116-
})),
117-
)
104+
bencode.str().ok_or(self.handle_error(BencodeConvertError::WrongType {
105+
key: error_key.as_ref().to_owned(),
106+
expected_type: "UTF-8 Bytes".to_owned(),
107+
}))
118108
}
119109

120110
/// Attempt to convert the given bencode value into a list.
@@ -125,12 +115,10 @@ pub trait BConvert {
125115
B: BRefAccess,
126116
E: AsRef<[u8]>,
127117
{
128-
bencode.list().ok_or(
129-
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
130-
key: error_key.as_ref().to_owned(),
131-
expected_type: "List".to_owned(),
132-
})),
133-
)
118+
bencode.list().ok_or(self.handle_error(BencodeConvertError::WrongType {
119+
key: error_key.as_ref().to_owned(),
120+
expected_type: "List".to_owned(),
121+
}))
134122
}
135123

136124
/// Attempt to convert the given bencode value into a dictionary.
@@ -141,12 +129,10 @@ pub trait BConvert {
141129
B: BRefAccess,
142130
E: AsRef<[u8]>,
143131
{
144-
bencode.dict().ok_or(
145-
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
146-
key: error_key.as_ref().to_owned(),
147-
expected_type: "Dictionary".to_owned(),
148-
})),
149-
)
132+
bencode.dict().ok_or(self.handle_error(BencodeConvertError::WrongType {
133+
key: error_key.as_ref().to_owned(),
134+
expected_type: "Dictionary".to_owned(),
135+
}))
150136
}
151137

152138
/// Look up a value in a dictionary of bencoded values using the given key.
@@ -159,11 +145,7 @@ pub trait BConvert {
159145

160146
match dictionary.lookup(key_ref) {
161147
Some(n) => Ok(n),
162-
None => Err(
163-
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::MissingKey {
164-
key: key_ref.to_owned(),
165-
})),
166-
),
148+
None => Err(self.handle_error(BencodeConvertError::MissingKey { key: key_ref.to_owned() })),
167149
}
168150
}
169151

‎contrib/bencode/src/access/dict.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ pub trait BDictAccess<K, V> {
2121

2222
impl<'a, V> BDictAccess<&'a [u8], V> for BTreeMap<&'a [u8], V> {
2323
fn to_list(&self) -> Vec<(&&'a [u8], &V)> {
24-
#[allow(clippy::map_identity)]
25-
self.iter().map(|(k, v)| (k, v)).collect()
24+
self.iter().collect()
2625
}
2726

2827
fn lookup(&self, key: &[u8]) -> Option<&V> {
@@ -44,8 +43,7 @@ impl<'a, V> BDictAccess<&'a [u8], V> for BTreeMap<&'a [u8], V> {
4443

4544
impl<'a, V> BDictAccess<Cow<'a, [u8]>, V> for BTreeMap<Cow<'a, [u8]>, V> {
4645
fn to_list(&self) -> Vec<(&Cow<'a, [u8]>, &V)> {
47-
#[allow(clippy::map_identity)]
48-
self.iter().map(|(k, v)| (k, v)).collect()
46+
self.iter().collect()
4947
}
5048

5149
fn lookup(&self, key: &[u8]) -> Option<&V> {

‎contrib/bencode/src/access/list.rs

-8
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ impl<'a, V: 'a> IndexMut<usize> for &'a mut dyn BListAccess<V> {
4545
}
4646
}
4747

48-
impl<'a, V: 'a> dyn BListAccess<V> {
49-
pub fn iter(&'a self) -> impl Iterator<Item = &'a V> {
50-
self.into_iter()
51-
}
52-
}
53-
54-
#[allow(unknown_lints)]
55-
#[allow(clippy::into_iter_without_iter)]
5648
impl<'a, V: 'a> IntoIterator for &'a dyn BListAccess<V> {
5749
type Item = &'a V;
5850
type IntoIter = BListIter<'a, V>;

‎contrib/bencode/src/error.rs

+46-97
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,52 @@
1-
#![allow(unknown_lints)]
2-
#![allow(clippy::iter_without_into_iter)]
3-
use error_chain::error_chain;
1+
use thiserror::Error;
42

5-
error_chain! {
6-
types {
7-
BencodeParseError, BencodeParseErrorKind, BencodeParseResultExt, BencodeParseResult;
8-
}
3+
#[allow(clippy::module_name_repetitions)]
4+
#[derive(Error, Debug)]
5+
pub enum BencodeParseError {
6+
#[error("Incomplete Number Of Bytes At {pos}")]
7+
BytesEmpty { pos: usize },
98

10-
errors {
11-
BytesEmpty {
12-
pos: usize
13-
} {
14-
description("Incomplete Number Of Bytes")
15-
display("Incomplete Number Of Bytes At {:?}", pos)
16-
}
17-
InvalidByte {
18-
pos: usize
19-
} {
20-
description("Invalid Byte Found")
21-
display("Invalid Byte Found At {:?}", pos)
22-
}
23-
InvalidIntNoDelimiter {
24-
pos: usize
25-
} {
26-
description("Invalid Integer Found With No Delimiter")
27-
display("Invalid Integer Found With No Delimiter At {:?}", pos)
28-
}
29-
InvalidIntNegativeZero {
30-
pos: usize
31-
} {
32-
description("Invalid Integer Found As Negative Zero")
33-
display("Invalid Integer Found As Negative Zero At {:?}", pos)
34-
}
35-
InvalidIntZeroPadding {
36-
pos: usize
37-
} {
38-
description("Invalid Integer Found With Zero Padding")
39-
display("Invalid Integer Found With Zero Padding At {:?}", pos)
40-
}
41-
InvalidIntParseError {
42-
pos: usize
43-
} {
44-
description("Invalid Integer Found To Fail Parsing")
45-
display("Invalid Integer Found To Fail Parsing At {:?}", pos)
46-
}
47-
InvalidKeyOrdering {
48-
pos: usize,
49-
key: Vec<u8>
50-
} {
51-
description("Invalid Dictionary Key Ordering Found")
52-
display("Invalid Dictionary Key Ordering Found At {:?} For Key {:?}", pos, key)
53-
}
54-
InvalidKeyDuplicates {
55-
pos: usize,
56-
key: Vec<u8>
57-
} {
58-
description("Invalid Dictionary Duplicate Keys Found")
59-
display("Invalid Dictionary Key Found At {:?} For Key {:?}", pos, key)
60-
}
61-
InvalidLengthNegative {
62-
pos: usize
63-
} {
64-
description("Invalid Byte Length Found As Negative")
65-
display("Invalid Byte Length Found As Negative At {:?}", pos)
66-
}
67-
InvalidLengthOverflow {
68-
pos: usize
69-
} {
70-
description("Invalid Byte Length Found To Overflow Buffer Length")
71-
display("Invalid Byte Length Found To Overflow Buffer Length At {:?}", pos)
72-
}
73-
InvalidRecursionExceeded {
74-
pos: usize,
75-
max: usize
76-
} {
77-
description("Invalid Recursion Limit Exceeded")
78-
display("Invalid Recursion Limit Exceeded At {:?} For Limit {:?}", pos, max)
79-
}
80-
}
9+
#[error("Invalid Byte Found At {pos}")]
10+
InvalidByte { pos: usize },
11+
12+
#[error("Invalid Integer Found With No Delimiter At {pos}")]
13+
InvalidIntNoDelimiter { pos: usize },
14+
15+
#[error("Invalid Integer Found As Negative Zero At {pos}")]
16+
InvalidIntNegativeZero { pos: usize },
17+
18+
#[error("Invalid Integer Found With Zero Padding At {pos}")]
19+
InvalidIntZeroPadding { pos: usize },
20+
21+
#[error("Invalid Integer Found To Fail Parsing At {pos}")]
22+
InvalidIntParseError { pos: usize },
23+
24+
#[error("Invalid Dictionary Key Ordering Found At {pos} For Key {key:?}")]
25+
InvalidKeyOrdering { pos: usize, key: Vec<u8> },
26+
27+
#[error("Invalid Dictionary Key Found At {pos} For Key {key:?}")]
28+
InvalidKeyDuplicates { pos: usize, key: Vec<u8> },
29+
30+
#[error("Invalid Byte Length Found As Negative At {pos}")]
31+
InvalidLengthNegative { pos: usize },
32+
33+
#[error("Invalid Byte Length Found To Overflow Buffer Length At {pos}")]
34+
InvalidLengthOverflow { pos: usize },
35+
36+
#[error("Invalid Recursion Limit Exceeded At {pos} For Limit {max}")]
37+
InvalidRecursionExceeded { pos: usize, max: usize },
8138
}
8239

83-
error_chain! {
84-
types {
85-
BencodeConvertError, BencodeConvertErrorKind, BencodeConvertResultExt, BencodeConvertResult;
86-
}
40+
pub type BencodeParseResult<T> = Result<T, BencodeParseError>;
8741

88-
errors {
89-
MissingKey {
90-
key: Vec<u8>
91-
} {
92-
description("Missing Key In Bencode")
93-
display("Missing Key In Bencode For {:?}", key)
94-
}
95-
WrongType {
96-
key: Vec<u8>,
97-
expected_type: String
98-
} {
99-
description("Wrong Type In Bencode")
100-
display("Wrong Type In Bencode For {:?} Expected Type {}", key, expected_type)
101-
}
102-
}
42+
#[allow(clippy::module_name_repetitions)]
43+
#[derive(Error, Debug)]
44+
pub enum BencodeConvertError {
45+
#[error("Missing Key In Bencode For {key:?}")]
46+
MissingKey { key: Vec<u8> },
47+
48+
#[error("Wrong Type In Bencode For {key:?} Expected Type {expected_type}")]
49+
WrongType { key: Vec<u8>, expected_type: String },
10350
}
51+
52+
pub type BencodeConvertResult<T> = Result<T, BencodeConvertError>;

‎contrib/bencode/src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//! ```rust
88
//! extern crate bencode;
99
//!
10-
//! use std::default::Default;
1110
//! use bencode::{BencodeRef, BRefAccess, BDecodeOpt};
1211
//!
1312
//! fn main() {
@@ -63,10 +62,7 @@ pub use crate::access::bencode::{BMutAccess, BRefAccess, MutKind, RefKind};
6362
pub use crate::access::convert::BConvert;
6463
pub use crate::access::dict::BDictAccess;
6564
pub use crate::access::list::BListAccess;
66-
pub use crate::error::{
67-
BencodeConvertError, BencodeConvertErrorKind, BencodeConvertResult, BencodeParseError, BencodeParseErrorKind,
68-
BencodeParseResult,
69-
};
65+
pub use crate::error::{BencodeConvertError, BencodeConvertResult, BencodeParseError, BencodeParseResult};
7066
pub use crate::mutable::bencode_mut::BencodeMut;
7167
pub use crate::reference::bencode_ref::BencodeRef;
7268
pub use crate::reference::decode_opt::BDecodeOpt;

‎contrib/bencode/src/mutable/encode.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::iter::Extend;
2+
13
use crate::access::bencode::{BRefAccess, RefKind};
24
use crate::access::dict::BDictAccess;
35
use crate::access::list::BListAccess;

0 commit comments

Comments
 (0)