Skip to content

Commit c7e7813

Browse files
authored
Merge pull request #459 from tox-rs/nom
refactor(nom): update to the latest version
2 parents 1e600e2 + 2be2278 commit c7e7813

File tree

93 files changed

+1336
-1242
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1336
-1242
lines changed

tox_binary_io/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ license = "GPL-3.0+"
1515
edition = "2018"
1616

1717
[dependencies]
18-
nom = "5.1"
18+
nom = "7.0"
1919
cookie-factory = "0.3"
2020
crypto_box = { version = "0.7", optional = true }
2121

tox_binary_io/src/crypto.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
use std::convert::TryInto;
22

3-
use nom::{map, map_opt, named, take};
3+
use nom::IResult;
4+
use nom::bytes::streaming::take;
5+
use nom::combinator::{map, map_opt};
46

57
use crypto_box::{PublicKey, SecretKey, KEY_SIZE};
68

79
use super::FromBytes;
810

911

1012
impl FromBytes for PublicKey {
11-
// TODO: use map_from with new nom version
12-
named!(from_bytes<PublicKey>, map!(map_opt!(take!(KEY_SIZE), |pk: &[u8]| pk.try_into().ok()), |pk: [u8; KEY_SIZE]| pk.into()));
13+
fn from_bytes(input: &[u8]) -> IResult<&[u8], Self> {
14+
map(map_opt(take(KEY_SIZE), |pk: &[u8]| pk.try_into().ok()), |pk: [u8; KEY_SIZE]| pk.into())(input)
15+
}
1316
}
1417

1518
/* TODO
@@ -26,8 +29,9 @@ impl FromBytes for PublicKey {
2629
*/
2730

2831
impl FromBytes for SecretKey {
29-
// TODO: use map_from with new nom version
30-
named!(from_bytes<SecretKey>, map!(map_opt!(take!(KEY_SIZE), |sk: &[u8]| sk.try_into().ok()), |sk: [u8; KEY_SIZE]| sk.into()));
32+
fn from_bytes(input: &[u8]) -> IResult<&[u8], Self> {
33+
map(map_opt(take(KEY_SIZE), |sk: &[u8]| sk.try_into().ok()), |sk: [u8; KEY_SIZE]| sk.into())(input)
34+
}
3135
}
3236

3337
#[cfg(test)]

tox_binary_io/src/lib.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
pub use nom::IResult;
22
pub use cookie_factory::GenError;
33

4-
use nom::number::streaming::{le_u8, le_u16};
5-
use nom::{named, map, map_opt, take, count};
4+
use nom::bytes::streaming::take;
5+
use nom::combinator::{map, map_opt};
6+
use nom::multi::count;
7+
use nom::number::complete::{le_u8, le_u16};
68
use cookie_factory::{do_gen, gen_be_u8, gen_le_u16, gen_slice};
79

810
use std::{convert::TryInto, net::{
@@ -19,7 +21,7 @@ mod crypto;
1921
/// The trait provides method to deserialize struct from raw bytes
2022
pub trait FromBytes: Sized {
2123
/// Deserialize struct using `nom` from raw bytes
22-
fn from_bytes(i: &[u8]) -> IResult<&[u8], Self>;
24+
fn from_bytes(input: &[u8]) -> IResult<&[u8], Self>;
2325
}
2426

2527
/// The trait provides method to serialize struct into raw bytes
@@ -38,9 +40,11 @@ impl ToBytes for IpAddr {
3840
}
3941

4042
impl FromBytes for Ipv4Addr {
41-
named!(from_bytes<Ipv4Addr>, map!(count!(le_u8, 4),
42-
|v| Ipv4Addr::new(v[0], v[1], v[2], v[3])
43-
));
43+
fn from_bytes(input: &[u8]) -> IResult<&[u8], Self> {
44+
map(count(le_u8, 4),
45+
|v| Ipv4Addr::new(v[0], v[1], v[2], v[3])
46+
)(input)
47+
}
4448
}
4549

4650
impl<const N: usize> ToBytes for [u8; N] {
@@ -50,7 +54,9 @@ impl<const N: usize> ToBytes for [u8; N] {
5054
}
5155

5256
impl <const N: usize> FromBytes for [u8; N] {
53-
named!(from_bytes<[u8; N]>, map_opt!(take!(N), |bytes: &[u8]| bytes.try_into().ok()));
57+
fn from_bytes(input: &[u8]) -> IResult<&[u8], Self> {
58+
map_opt(take(N), |bytes: &[u8]| bytes.try_into().ok())(input)
59+
}
5460
}
5561

5662
impl ToBytes for Ipv4Addr {
@@ -66,9 +72,11 @@ impl ToBytes for Ipv4Addr {
6672
}
6773

6874
impl FromBytes for Ipv6Addr {
69-
named!(from_bytes<Ipv6Addr>, map!(count!(le_u16, 8),
70-
|v| Ipv6Addr::new(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
71-
));
75+
fn from_bytes(i: &[u8]) -> IResult<&[u8], Self> {
76+
map(count(le_u16, 8),
77+
|v| Ipv6Addr::new(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
78+
)(i)
79+
}
7280
}
7381

7482
impl ToBytes for Ipv6Addr {

tox_core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ tox_packet = { version = "0.1.1", path = "../tox_packet" }
2424
bytes = "1.0"
2525
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
2626
log = "0.4"
27-
nom = "5.1"
27+
nom = "7.0"
2828
cookie-factory = "0.3"
2929
get_if_addrs = "0.5"
3030
thiserror = "1.0"

tox_core/src/dht/codec.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::stats::*;
1010
use bytes::BytesMut;
1111
use cookie_factory::GenError;
1212
use thiserror::Error;
13-
use nom::{error::ErrorKind, Err};
13+
use nom::error::Error as NomError;
1414
use tokio_util::codec::{Decoder, Encoder};
1515

1616
/// A serialized `Packet` should be not longer than 2048 bytes.
@@ -29,7 +29,7 @@ pub enum DecodeError {
2929
#[error("Deserialize Packet error: {:?}, packet: {:?}", error, packet)]
3030
Deserialize {
3131
/// Parsing error.
32-
error: nom::Err<(Vec<u8>, ErrorKind)>,
32+
error: nom::Err<NomError<Vec<u8>>>,
3333
/// Received packet.
3434
packet: Vec<u8>,
3535
},
@@ -43,8 +43,8 @@ impl DecodeError {
4343
DecodeError::TooBigPacket { len }
4444
}
4545

46-
pub(crate) fn deserialize(e: Err<(&[u8], ErrorKind)>, packet: Vec<u8>) -> DecodeError {
47-
DecodeError::Deserialize { error: e.to_owned(), packet }
46+
pub(crate) fn deserialize(e: nom::Err<NomError<&[u8]>>, packet: Vec<u8>) -> DecodeError {
47+
DecodeError::Deserialize { error: e.map(|e| NomError::new(e.input.to_vec(), e.code)), packet }
4848
}
4949
}
5050

@@ -151,6 +151,7 @@ mod tests {
151151
use rand::thread_rng;
152152
use tox_packet::onion::*;
153153
use crypto_box::{SalsaBox, SecretKey, aead::{AeadCore, generic_array::typenum::marker_traits::Unsigned}};
154+
use nom::{Err, error::ErrorKind as NomErrorKind};
154155

155156
const ONION_RETURN_1_PAYLOAD_SIZE: usize = ONION_RETURN_1_SIZE - xsalsa20poly1305::NONCE_SIZE;
156157
const ONION_RETURN_2_PAYLOAD_SIZE: usize = ONION_RETURN_2_SIZE - xsalsa20poly1305::NONCE_SIZE;
@@ -327,7 +328,7 @@ mod tests {
327328

328329
let res = codec.decode(&mut buf);
329330
// not enough bytes to decode EncryptedPacket
330-
assert!(matches!(res, Err(DecodeError::Deserialize { error: Err::Error((_, ErrorKind::Alt)), packet: _ })));
331+
assert!(matches!(res, Err(DecodeError::Deserialize { error: Err::Error(NomError { input: _, code: NomErrorKind::Tag }), packet: _ })));
331332
}
332333

333334
#[test]

tox_core/src/dht/daemon_state.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@ use crate::dht::kbucket::*;
1515
use crate::dht::ktree::*;
1616

1717
use thiserror::Error;
18-
use nom::{Err, error::ErrorKind as NomErrorKind};
18+
use nom::{Err, error::Error as NomError};
1919

2020
/// An error that can occur while serializing/deserializing object.
21-
#[derive(Clone, Debug, Eq, PartialEq, Error)]
21+
#[derive(Debug, PartialEq, Error)]
2222
pub enum DeserializeError {
2323
/// Error indicates that object can't be parsed.
2424
#[error("Deserialize object error: {:?}, data: {:?}", error, data)]
2525
Deserialize {
2626
/// Parsing error.
27-
error: nom::Err<(Vec<u8>, NomErrorKind)>,
27+
error: nom::Err<NomError<Vec<u8>>>,
2828
/// Object serialized data.
2929
data: Vec<u8>,
3030
},
3131
}
3232

3333
impl DeserializeError {
34-
pub(crate) fn deserialize(e: Err<(&[u8], NomErrorKind)>, data: Vec<u8>) -> DeserializeError {
35-
DeserializeError::Deserialize { error: e.to_owned(), data }
34+
pub(crate) fn deserialize(e: Err<NomError<&[u8]>>, data: Vec<u8>) -> DeserializeError {
35+
DeserializeError::Deserialize { error: e.map(|e| NomError::new(e.input.to_vec(), e.code)), data }
3636
}
3737
}
3838

@@ -96,6 +96,7 @@ mod tests {
9696

9797
use futures::channel::mpsc;
9898
use futures::StreamExt;
99+
use nom::error::ErrorKind as NomErrorKind;
99100

100101
macro_rules! unpack {
101102
($variable:expr, $variant:path) => (
@@ -138,14 +139,14 @@ mod tests {
138139
let error = res.err().unwrap();
139140
let mut input = vec![2, 1, 2, 3, 4, 4, 210];
140141
input.extend_from_slice(&pk_org.as_bytes()[..crypto_box::KEY_SIZE - 1]);
141-
assert_eq!(error, DeserializeError::Deserialize { error: Err::Error((
142+
assert_eq!(error, DeserializeError::Deserialize { error: Err::Error(NomError::new(
142143
input, NomErrorKind::Eof)), data: serialized_vec[..serialized_len - 1].to_vec() });
143144

144145
// test with serialized data corrupted
145146
let serialized_vec = [42; 10];
146147
let res = DaemonState::deserialize_old(&alice, &serialized_vec).await;
147148
let error = res.err().unwrap();
148-
assert_eq!(error, DeserializeError::Deserialize { error: Err::Error((
149+
assert_eq!(error, DeserializeError::Deserialize { error: Err::Error(NomError::new(
149150
vec![42; 10], NomErrorKind::Tag)), data: serialized_vec.to_vec() });
150151

151152
// test with empty close list

tox_core/src/friend_connection/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,8 @@ mod tests {
10101010
let join = future::join(run_future, packets_future);
10111011

10121012
futures::select! {
1013-
_ = join.fuse() => (),
1014-
_ = connection_status_future.fuse() => (),
1013+
_ = join.fuse() => {},
1014+
_ = connection_status_future.fuse() => {},
10151015
}
10161016
}
10171017
}

tox_core/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ Repo: https://github.com/tox-rs/tox
1414
#[macro_use]
1515
extern crate log;
1616
#[macro_use]
17-
extern crate nom;
18-
#[macro_use]
1917
extern crate cookie_factory;
2018

2119
pub mod io_tokio;

tox_core/src/net_crypto/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl PacketsArrayError {
6262
}
6363

6464
/// Error that can happen when calling `handle_*` of packet.
65-
#[derive(Clone, Debug, Eq, PartialEq, Error)]
65+
#[derive(Debug, PartialEq, Error)]
6666
pub enum HandlePacketError {
6767
/// Error indicates that getting payload of received packet error.
6868
#[error("Get payload of received packet error")]

tox_core/src/onion/client/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use tox_packet::dht::GetPayloadError;
55
use crate::{dht::server::errors::PingError, relay::client::ConnectionError};
66

77
/// Error that can happen when handling `OnionAnnounceResponse` packet.
8-
#[derive(Clone, Debug, Eq, PartialEq, Error)]
8+
#[derive(Debug, PartialEq, Error)]
99
pub enum HandleAnnounceResponseError {
1010
/// Invalid request ID when handling OnionAnnounceResponse.
1111
#[error("Invalid request ID when handling OnionAnnounceResponse")]

0 commit comments

Comments
 (0)