Browse Source

Make proto::core::value module private.

wip
Titouan Rigoudy 4 years ago
parent
commit
a4a2655fbb
7 changed files with 40 additions and 29 deletions
  1. +7
    -3
      proto/src/core/mod.rs
  2. +20
    -0
      proto/src/core/testing.rs
  3. +3
    -16
      proto/src/core/value.rs
  4. +1
    -1
      proto/src/core/worker.rs
  5. +3
    -3
      proto/src/peer/message.rs
  6. +3
    -3
      proto/src/server/request.rs
  7. +3
    -3
      proto/src/server/response.rs

+ 7
- 3
proto/src/core/mod.rs View File

@ -1,12 +1,16 @@
mod frame;
mod prefix;
#[cfg(test)]
pub mod testing;
mod u32;
mod user;
// TODO: Remove `pub` qualifier, depend on re-exports.
pub mod value;
mod value;
mod worker;
pub use frame::{FrameReader, FrameWriter};
pub use user::{User, UserStatus};
pub use value::{ValueDecode, ValueEncode};
pub use value::{
ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncodeError,
ValueEncoder,
};
pub use worker::{Worker, WorkerError};

+ 20
- 0
proto/src/core/testing.rs View File

@ -0,0 +1,20 @@
//! Shared testing utilities for protocol code.
use std::fmt;
use bytes::BytesMut;
use crate::core::{ValueDecode, ValueDecoder, ValueEncode, ValueEncoder};
/// Encodes `input`, decodes it, asserts equality of `input` and the result.
pub fn roundtrip<T>(input: T)
where
T: fmt::Debug + Eq + PartialEq + ValueEncode + ValueDecode,
{
let mut bytes = BytesMut::new();
ValueEncoder::new(&mut bytes).encode(&input).unwrap();
let output = ValueDecoder::new(&bytes).decode::<T>().unwrap();
assert_eq!(output, input);
}

+ 3
- 16
proto/src/core/value.rs View File

@ -506,32 +506,19 @@ impl<T: ValueEncode> ValueEncode for Vec<T> {
#[cfg(test)]
pub mod tests {
use std::fmt;
use std::net;
use std::u16;
use std::u32;
use bytes::{BufMut, BytesMut};
use super::{
ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncoder,
};
use crate::core::testing::roundtrip;
use super::{ValueDecodeError, ValueDecoder, ValueEncoder};
// Declared here because assert_eq!(bytes, &[]) fails to infer types.
const EMPTY_BYTES: &'static [u8] = &[];
pub fn roundtrip<T>(input: T)
where
T: fmt::Debug + Eq + PartialEq + ValueEncode + ValueDecode,
{
let mut bytes = BytesMut::new();
ValueEncoder::new(&mut bytes).encode(&input).unwrap();
let output = ValueDecoder::new(&bytes).decode::<T>().unwrap();
assert_eq!(output, input);
}
// A few integers and their corresponding byte encodings.
const U32_ENCODINGS: [(u32, [u8; 4]); 8] = [
(0, [0, 0, 0, 0]),


+ 1
- 1
proto/src/core/worker.rs View File

@ -7,8 +7,8 @@ use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
use tokio::net::TcpStream;
use tokio::sync::mpsc;
use crate::core::{FrameReader, FrameWriter};
use crate::core::value::{ValueDecode, ValueEncode};
use crate::core::{FrameReader, FrameWriter};
/// An error that arose while exchanging messages over a `Channel`.
#[derive(Debug, Error)]


+ 3
- 3
proto/src/peer/message.rs View File

@ -1,4 +1,4 @@
use crate::core::value::{
use crate::core::{
ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncodeError,
ValueEncoder,
};
@ -99,8 +99,8 @@ impl ValueDecode for PeerInit {
mod tests {
use bytes::BytesMut;
use crate::core::value::tests::roundtrip;
use crate::core::value::{ValueDecodeError, ValueDecoder};
use crate::core::testing::roundtrip;
use crate::core::{ValueDecodeError, ValueDecoder};
use super::*;


+ 3
- 3
proto/src/server/request.rs View File

@ -1,4 +1,4 @@
use crate::core::value::{
use crate::core::{
ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncodeError,
ValueEncoder,
};
@ -496,8 +496,8 @@ impl ValueDecode for UserStatusRequest {
mod tests {
use bytes::BytesMut;
use crate::core::value::tests::roundtrip;
use crate::core::value::{ValueDecodeError, ValueDecoder};
use crate::core::testing::roundtrip;
use crate::core::{ValueDecodeError, ValueDecoder};
use super::*;


+ 3
- 3
proto/src/server/response.rs View File

@ -2,7 +2,7 @@ use std::net;
use log::{debug, warn};
use crate::core::value::{
use crate::core::{
ValueDecode, ValueDecodeError, ValueDecoder, ValueEncode, ValueEncodeError,
ValueEncoder,
};
@ -1118,8 +1118,8 @@ mod tests {
use bytes::BytesMut;
use crate::core::value::tests::roundtrip;
use crate::core::value::{ValueDecodeError, ValueDecoder};
use crate::core::testing::roundtrip;
use crate::core::{ValueDecodeError, ValueDecoder};
use super::*;


Loading…
Cancel
Save