Skip to content

Commit e5769d1

Browse files
committed
[git-packetline] refactor
1 parent fef3c9f commit e5769d1

File tree

16 files changed

+240
-235
lines changed

16 files changed

+240
-235
lines changed

git-packetline/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ pub mod immutable;
2929
pub use immutable::PacketLine;
3030

3131
///
32-
pub mod provider;
32+
pub mod read;
3333
#[doc(inline)]
34-
pub use provider::Provider;
34+
pub use read::StreamingPeekReader;
3535

3636
///
3737
pub mod write;

git-packetline/src/provider/mod.rs renamed to git-packetline/src/read/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use crate::{decode, PacketLine, MAX_LINE_LEN, U16_HEX_BYTES};
22
use bstr::ByteSlice;
33
use std::io;
44

5-
mod read;
6-
pub use read::ReadWithSidebands;
5+
mod sidebands;
6+
pub use sidebands::WithSidebands;
77

88
/// Read pack lines one after another, without consuming more than needed from the underlying
99
/// [`Read`][io::Read]. [`Flush`][PacketLine::Flush] lines cause the reader to stop producing lines forever,
1010
/// leaving [`Read`][io::Read] at the start of whatever comes next.
11-
pub struct Provider<T> {
11+
pub struct StreamingPeekReader<T> {
1212
inner: T,
1313
peek_buf: Vec<u8>,
1414
fail_on_err_lines: bool,
@@ -18,13 +18,13 @@ pub struct Provider<T> {
1818
stopped_at: Option<PacketLine<'static>>,
1919
}
2020

21-
impl<T> Provider<T>
21+
impl<T> StreamingPeekReader<T>
2222
where
2323
T: io::Read,
2424
{
2525
/// Return a new instance from `read` which will stop decoding packet lines when receiving one of the given `delimiters`.
2626
pub fn new(read: T, delimiters: &'static [PacketLine<'static>]) -> Self {
27-
Provider {
27+
StreamingPeekReader {
2828
inner: read,
2929
buf: vec![0; MAX_LINE_LEN],
3030
peek_buf: Vec::new(),
@@ -192,22 +192,22 @@ where
192192
/// being true in case the `text` is to be interpreted as error.
193193
///
194194
/// _Please note_ that side bands need to be negotiated with the server.
195-
pub fn as_read_with_sidebands<F: FnMut(bool, &[u8])>(&mut self, handle_progress: F) -> ReadWithSidebands<'_, T, F> {
196-
ReadWithSidebands::with_progress_handler(self, handle_progress)
195+
pub fn as_read_with_sidebands<F: FnMut(bool, &[u8])>(&mut self, handle_progress: F) -> WithSidebands<'_, T, F> {
196+
WithSidebands::with_progress_handler(self, handle_progress)
197197
}
198198

199199
/// Same as [`as_read_with_sidebands(…)`][Provider::as_read_with_sidebands()], but for channels without side band support.
200200
///
201201
/// The type parameter `F` needs to be configured for this method to be callable using the 'turbofish' operator.
202202
/// Use [`as_read()`][Provider::as_read()].
203-
pub fn as_read_without_sidebands<F: FnMut(bool, &[u8])>(&mut self) -> ReadWithSidebands<'_, T, F> {
204-
ReadWithSidebands::without_progress_handler(self)
203+
pub fn as_read_without_sidebands<F: FnMut(bool, &[u8])>(&mut self) -> WithSidebands<'_, T, F> {
204+
WithSidebands::without_progress_handler(self)
205205
}
206206

207207
/// Same as [`as_read_with_sidebands(…)`][Provider::as_read_with_sidebands()], but for channels without side band support.
208208
///
209209
/// Due to the preconfigured function type this method can be called without 'turbofish'.
210-
pub fn as_read(&mut self) -> ReadWithSidebands<'_, T, fn(bool, &[u8])> {
211-
ReadWithSidebands::new(self)
210+
pub fn as_read(&mut self) -> WithSidebands<'_, T, fn(bool, &[u8])> {
211+
WithSidebands::new(self)
212212
}
213213
}

git-packetline/src/provider/read.rs renamed to git-packetline/src/read/sidebands.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
immutable::{Band, Text},
3-
PacketLine, Provider, MAX_DATA_LEN,
3+
PacketLine, StreamingPeekReader, MAX_DATA_LEN,
44
};
55
use std::io;
66

@@ -12,18 +12,18 @@ use std::io;
1212
/// Reading from this intermediary copies bytes 3 times:
1313
/// OS -> (parent) line provider buffer -> our buffer -> caller's output buffer
1414
/// which won't make this very efficient for huge bandwidths.
15-
pub struct ReadWithSidebands<'a, T, F>
15+
pub struct WithSidebands<'a, T, F>
1616
where
1717
T: io::Read,
1818
{
19-
parent: &'a mut Provider<T>,
19+
parent: &'a mut StreamingPeekReader<T>,
2020
handle_progress: Option<F>,
2121
buf: Vec<u8>,
2222
pos: usize,
2323
cap: usize,
2424
}
2525

26-
impl<'a, T, F> Drop for ReadWithSidebands<'a, T, F>
26+
impl<'a, T, F> Drop for WithSidebands<'a, T, F>
2727
where
2828
T: io::Read,
2929
{
@@ -32,13 +32,13 @@ where
3232
}
3333
}
3434

35-
impl<'a, T> ReadWithSidebands<'a, T, fn(bool, &[u8])>
35+
impl<'a, T> WithSidebands<'a, T, fn(bool, &[u8])>
3636
where
3737
T: io::Read,
3838
{
3939
/// Create a new instance with the given provider as `parent`.
40-
pub fn new(parent: &'a mut Provider<T>) -> Self {
41-
ReadWithSidebands {
40+
pub fn new(parent: &'a mut StreamingPeekReader<T>) -> Self {
41+
WithSidebands {
4242
parent,
4343
handle_progress: None,
4444
buf: vec![0; MAX_DATA_LEN],
@@ -48,7 +48,7 @@ where
4848
}
4949
}
5050

51-
impl<'a, T, F> ReadWithSidebands<'a, T, F>
51+
impl<'a, T, F> WithSidebands<'a, T, F>
5252
where
5353
T: io::Read,
5454
F: FnMut(bool, &[u8]),
@@ -57,8 +57,8 @@ where
5757
///
5858
/// Progress or error information will be passed to the given `handle_progress(is_error, text)` function, with `is_error: bool`
5959
/// being true in case the `text` is to be interpreted as error.
60-
pub fn with_progress_handler(parent: &'a mut Provider<T>, handle_progress: F) -> Self {
61-
ReadWithSidebands {
60+
pub fn with_progress_handler(parent: &'a mut StreamingPeekReader<T>, handle_progress: F) -> Self {
61+
WithSidebands {
6262
parent,
6363
handle_progress: Some(handle_progress),
6464
buf: vec![0; MAX_DATA_LEN],
@@ -68,8 +68,8 @@ where
6868
}
6969

7070
/// Create a new instance without a progress handler.
71-
pub fn without_progress_handler(parent: &'a mut Provider<T>) -> Self {
72-
ReadWithSidebands {
71+
pub fn without_progress_handler(parent: &'a mut StreamingPeekReader<T>) -> Self {
72+
WithSidebands {
7373
parent,
7474
handle_progress: None,
7575
buf: vec![0; MAX_DATA_LEN],
@@ -105,7 +105,7 @@ where
105105
}
106106
}
107107

108-
impl<'a, T, F> io::BufRead for ReadWithSidebands<'a, T, F>
108+
impl<'a, T, F> io::BufRead for WithSidebands<'a, T, F>
109109
where
110110
T: io::Read,
111111
F: FnMut(bool, &[u8]),
@@ -173,7 +173,7 @@ where
173173
}
174174
}
175175

176-
impl<'a, T, F> io::Read for ReadWithSidebands<'a, T, F>
176+
impl<'a, T, F> io::Read for WithSidebands<'a, T, F>
177177
where
178178
T: io::Read,
179179
F: FnMut(bool, &[u8]),

git-packetline/tests/packet_line/decode.rs renamed to git-packetline/tests/decode/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
mod streaming {
2-
use crate::packet_line::assert_err_display;
2+
use crate::assert_err_display;
33
use bstr::ByteSlice;
4-
use git_packetline::immutable::Error;
54
use git_packetline::{
65
decode::{self, streaming, Stream},
6+
immutable::Error,
77
Channel, PacketLine,
88
};
99

git-packetline/tests/packet_line/encode.rs renamed to git-packetline/tests/encode/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod data_to_write {
2-
use crate::packet_line::assert_err_display;
2+
use crate::assert_err_display;
33
use bstr::ByteSlice;
44
use git_packetline::encode::{
55
data_to_write, delim_to_write, error_to_write, flush_to_write, response_end_to_write, text_to_write,

git-packetline/tests/packet_line/mod.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

git-packetline/tests/packet_line/reader/mod.rs

Lines changed: 0 additions & 179 deletions
This file was deleted.

git-packetline/tests/packetline.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
pub type Result = std::result::Result<(), Box<dyn std::error::Error>>;
22

3-
mod packet_line;
3+
fn assert_err_display<T: std::fmt::Debug, E: std::error::Error>(
4+
res: std::result::Result<T, E>,
5+
expected: impl AsRef<str>,
6+
) {
7+
match res {
8+
Ok(v) => assert!(false, "Expected error '{}', got value {:?}", expected.as_ref(), v),
9+
Err(err) => assert_eq!(err.to_string(), expected.as_ref()),
10+
}
11+
}
12+
13+
mod decode;
14+
mod encode;
15+
mod read;
16+
mod write;

0 commit comments

Comments
 (0)