Skip to content

Commit

Permalink
Merge torrust#1021: update deps including bencode contrib
Browse files Browse the repository at this point in the history
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
  • Loading branch information
da2ce7 committed Aug 25, 2024
2 parents f79a019 + 2c4bdab commit 06857d2
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 311 deletions.
168 changes: 83 additions & 85 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ chrono = { version = "0", default-features = false, features = ["clock"] }
clap = { version = "4", features = ["derive", "env"] }
crossbeam-skiplist = "0"
dashmap = "6"
derive_more = "0"
derive_more = { version = "1", features = ["as_ref", "constructor", "from"] }
figment = "0"
futures = "0"
futures-util = "0"
Expand All @@ -66,7 +66,7 @@ serde_bencode = "0"
serde_bytes = "0"
serde_json = { version = "1", features = ["preserve_order"] }
serde_repr = "0"
serde_with = { version = "3.9.0", features = ["json"] }
serde_with = { version = "3", features = ["json"] }
thiserror = "1"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
torrust-tracker-clock = { version = "3.0.0-beta-develop", path = "packages/clock" }
Expand Down
6 changes: 1 addition & 5 deletions contrib/bencode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ rust-version.workspace = true
version.workspace = true

[dependencies]
error-chain = "0"
thiserror = "1"

[dev-dependencies]
criterion = "0"

[[test]]
name = "test"
path = "test/mod.rs"

[[bench]]
harness = false
name = "bencode_benchmark"
78 changes: 30 additions & 48 deletions contrib/bencode/src/access/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::access::bencode::{BRefAccess, BRefAccessExt};
use crate::access::dict::BDictAccess;
use crate::access::list::BListAccess;
use crate::{BencodeConvertError, BencodeConvertErrorKind};
use crate::BencodeConvertError;

/// Trait for extended casting of bencode objects and converting conversion errors into application specific errors.
pub trait BConvertExt: BConvert {
Expand All @@ -12,12 +12,10 @@ pub trait BConvertExt: BConvert {
B: BRefAccessExt<'a>,
E: AsRef<[u8]>,
{
bencode.bytes_ext().ok_or(
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "Bytes".to_owned(),
})),
)
bencode.bytes_ext().ok_or(self.handle_error(BencodeConvertError::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "Bytes".to_owned(),
}))
}

/// See `BConvert::convert_str`.
Expand All @@ -26,12 +24,10 @@ pub trait BConvertExt: BConvert {
B: BRefAccessExt<'a>,
E: AsRef<[u8]>,
{
bencode.str_ext().ok_or(
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "UTF-8 Bytes".to_owned(),
})),
)
bencode.str_ext().ok_or(self.handle_error(BencodeConvertError::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "UTF-8 Bytes".to_owned(),
}))
}

/// See `BConvert::lookup_and_convert_bytes`.
Expand Down Expand Up @@ -77,12 +73,10 @@ pub trait BConvert {
B: BRefAccess,
E: AsRef<[u8]>,
{
bencode.int().ok_or(
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "Integer".to_owned(),
})),
)
bencode.int().ok_or(self.handle_error(BencodeConvertError::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "Integer".to_owned(),
}))
}

/// Attempt to convert the given bencode value into bytes.
Expand All @@ -93,12 +87,10 @@ pub trait BConvert {
B: BRefAccess,
E: AsRef<[u8]>,
{
bencode.bytes().ok_or(
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "Bytes".to_owned(),
})),
)
bencode.bytes().ok_or(self.handle_error(BencodeConvertError::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "Bytes".to_owned(),
}))
}

/// Attempt to convert the given bencode value into a UTF-8 string.
Expand All @@ -109,12 +101,10 @@ pub trait BConvert {
B: BRefAccess,
E: AsRef<[u8]>,
{
bencode.str().ok_or(
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "UTF-8 Bytes".to_owned(),
})),
)
bencode.str().ok_or(self.handle_error(BencodeConvertError::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "UTF-8 Bytes".to_owned(),
}))
}

/// Attempt to convert the given bencode value into a list.
Expand All @@ -125,12 +115,10 @@ pub trait BConvert {
B: BRefAccess,
E: AsRef<[u8]>,
{
bencode.list().ok_or(
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "List".to_owned(),
})),
)
bencode.list().ok_or(self.handle_error(BencodeConvertError::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "List".to_owned(),
}))
}

/// Attempt to convert the given bencode value into a dictionary.
Expand All @@ -141,12 +129,10 @@ pub trait BConvert {
B: BRefAccess,
E: AsRef<[u8]>,
{
bencode.dict().ok_or(
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "Dictionary".to_owned(),
})),
)
bencode.dict().ok_or(self.handle_error(BencodeConvertError::WrongType {
key: error_key.as_ref().to_owned(),
expected_type: "Dictionary".to_owned(),
}))
}

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

match dictionary.lookup(key_ref) {
Some(n) => Ok(n),
None => Err(
self.handle_error(BencodeConvertError::from_kind(BencodeConvertErrorKind::MissingKey {
key: key_ref.to_owned(),
})),
),
None => Err(self.handle_error(BencodeConvertError::MissingKey { key: key_ref.to_owned() })),
}
}

Expand Down
6 changes: 2 additions & 4 deletions contrib/bencode/src/access/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ pub trait BDictAccess<K, V> {

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

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

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

fn lookup(&self, key: &[u8]) -> Option<&V> {
Expand Down
8 changes: 0 additions & 8 deletions contrib/bencode/src/access/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ impl<'a, V: 'a> IndexMut<usize> for &'a mut dyn BListAccess<V> {
}
}

impl<'a, V: 'a> dyn BListAccess<V> {
pub fn iter(&'a self) -> impl Iterator<Item = &'a V> {
self.into_iter()
}
}

#[allow(unknown_lints)]
#[allow(clippy::into_iter_without_iter)]
impl<'a, V: 'a> IntoIterator for &'a dyn BListAccess<V> {
type Item = &'a V;
type IntoIter = BListIter<'a, V>;
Expand Down
143 changes: 46 additions & 97 deletions contrib/bencode/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,103 +1,52 @@
#![allow(unknown_lints)]
#![allow(clippy::iter_without_into_iter)]
use error_chain::error_chain;
use thiserror::Error;

error_chain! {
types {
BencodeParseError, BencodeParseErrorKind, BencodeParseResultExt, BencodeParseResult;
}
#[allow(clippy::module_name_repetitions)]
#[derive(Error, Debug)]
pub enum BencodeParseError {
#[error("Incomplete Number Of Bytes At {pos}")]
BytesEmpty { pos: usize },

errors {
BytesEmpty {
pos: usize
} {
description("Incomplete Number Of Bytes")
display("Incomplete Number Of Bytes At {:?}", pos)
}
InvalidByte {
pos: usize
} {
description("Invalid Byte Found")
display("Invalid Byte Found At {:?}", pos)
}
InvalidIntNoDelimiter {
pos: usize
} {
description("Invalid Integer Found With No Delimiter")
display("Invalid Integer Found With No Delimiter At {:?}", pos)
}
InvalidIntNegativeZero {
pos: usize
} {
description("Invalid Integer Found As Negative Zero")
display("Invalid Integer Found As Negative Zero At {:?}", pos)
}
InvalidIntZeroPadding {
pos: usize
} {
description("Invalid Integer Found With Zero Padding")
display("Invalid Integer Found With Zero Padding At {:?}", pos)
}
InvalidIntParseError {
pos: usize
} {
description("Invalid Integer Found To Fail Parsing")
display("Invalid Integer Found To Fail Parsing At {:?}", pos)
}
InvalidKeyOrdering {
pos: usize,
key: Vec<u8>
} {
description("Invalid Dictionary Key Ordering Found")
display("Invalid Dictionary Key Ordering Found At {:?} For Key {:?}", pos, key)
}
InvalidKeyDuplicates {
pos: usize,
key: Vec<u8>
} {
description("Invalid Dictionary Duplicate Keys Found")
display("Invalid Dictionary Key Found At {:?} For Key {:?}", pos, key)
}
InvalidLengthNegative {
pos: usize
} {
description("Invalid Byte Length Found As Negative")
display("Invalid Byte Length Found As Negative At {:?}", pos)
}
InvalidLengthOverflow {
pos: usize
} {
description("Invalid Byte Length Found To Overflow Buffer Length")
display("Invalid Byte Length Found To Overflow Buffer Length At {:?}", pos)
}
InvalidRecursionExceeded {
pos: usize,
max: usize
} {
description("Invalid Recursion Limit Exceeded")
display("Invalid Recursion Limit Exceeded At {:?} For Limit {:?}", pos, max)
}
}
#[error("Invalid Byte Found At {pos}")]
InvalidByte { pos: usize },

#[error("Invalid Integer Found With No Delimiter At {pos}")]
InvalidIntNoDelimiter { pos: usize },

#[error("Invalid Integer Found As Negative Zero At {pos}")]
InvalidIntNegativeZero { pos: usize },

#[error("Invalid Integer Found With Zero Padding At {pos}")]
InvalidIntZeroPadding { pos: usize },

#[error("Invalid Integer Found To Fail Parsing At {pos}")]
InvalidIntParseError { pos: usize },

#[error("Invalid Dictionary Key Ordering Found At {pos} For Key {key:?}")]
InvalidKeyOrdering { pos: usize, key: Vec<u8> },

#[error("Invalid Dictionary Key Found At {pos} For Key {key:?}")]
InvalidKeyDuplicates { pos: usize, key: Vec<u8> },

#[error("Invalid Byte Length Found As Negative At {pos}")]
InvalidLengthNegative { pos: usize },

#[error("Invalid Byte Length Found To Overflow Buffer Length At {pos}")]
InvalidLengthOverflow { pos: usize },

#[error("Invalid Recursion Limit Exceeded At {pos} For Limit {max}")]
InvalidRecursionExceeded { pos: usize, max: usize },
}

error_chain! {
types {
BencodeConvertError, BencodeConvertErrorKind, BencodeConvertResultExt, BencodeConvertResult;
}
pub type BencodeParseResult<T> = Result<T, BencodeParseError>;

errors {
MissingKey {
key: Vec<u8>
} {
description("Missing Key In Bencode")
display("Missing Key In Bencode For {:?}", key)
}
WrongType {
key: Vec<u8>,
expected_type: String
} {
description("Wrong Type In Bencode")
display("Wrong Type In Bencode For {:?} Expected Type {}", key, expected_type)
}
}
#[allow(clippy::module_name_repetitions)]
#[derive(Error, Debug)]
pub enum BencodeConvertError {
#[error("Missing Key In Bencode For {key:?}")]
MissingKey { key: Vec<u8> },

#[error("Wrong Type In Bencode For {key:?} Expected Type {expected_type}")]
WrongType { key: Vec<u8>, expected_type: String },
}

pub type BencodeConvertResult<T> = Result<T, BencodeConvertError>;
6 changes: 1 addition & 5 deletions contrib/bencode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! ```rust
//! extern crate bencode;
//!
//! use std::default::Default;
//! use bencode::{BencodeRef, BRefAccess, BDecodeOpt};
//!
//! fn main() {
Expand Down Expand Up @@ -63,10 +62,7 @@ pub use crate::access::bencode::{BMutAccess, BRefAccess, MutKind, RefKind};
pub use crate::access::convert::BConvert;
pub use crate::access::dict::BDictAccess;
pub use crate::access::list::BListAccess;
pub use crate::error::{
BencodeConvertError, BencodeConvertErrorKind, BencodeConvertResult, BencodeParseError, BencodeParseErrorKind,
BencodeParseResult,
};
pub use crate::error::{BencodeConvertError, BencodeConvertResult, BencodeParseError, BencodeParseResult};
pub use crate::mutable::bencode_mut::BencodeMut;
pub use crate::reference::bencode_ref::BencodeRef;
pub use crate::reference::decode_opt::BDecodeOpt;
Expand Down
2 changes: 2 additions & 0 deletions contrib/bencode/src/mutable/encode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::iter::Extend;

use crate::access::bencode::{BRefAccess, RefKind};
use crate::access::dict::BDictAccess;
use crate::access::list::BListAccess;
Expand Down
Loading

0 comments on commit 06857d2

Please sign in to comment.