Skip to content

Commit 23438fd

Browse files
committed
[packetline #178] refactor
1 parent d4c16a9 commit 23438fd

File tree

11 files changed

+76
-92
lines changed

11 files changed

+76
-92
lines changed

git-packetline/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44

55
* **renames / moves**
66
- `immutable::PacketLine` -> `PacketLineRef`
7+
- `immutable::Error` -> `ErrorRef`
8+
- `immutable::Text` -> `TextRef`
9+
- `immutable::Band` -> `BandRef`
710
- `immutable::DecodeBandError` -> `decode::band::Error`
8-
11+
- `pub immutable::` -> `line::`

git-packetline/src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ pub enum Channel {
2424
}
2525

2626
///
27-
pub mod immutable;
27+
pub mod line;
28+
2829
/// A borrowed packet line as it refers to a slice of data by reference.
2930
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
3031
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
@@ -39,6 +40,28 @@ pub enum PacketLineRef<'a> {
3940
ResponseEnd,
4041
}
4142

43+
/// A packet line representing an Error in a side-band channel.
44+
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
45+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
46+
pub struct ErrorRef<'a>(pub &'a [u8]);
47+
48+
/// A packet line representing text, which may include a trailing newline.
49+
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
50+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
51+
pub struct TextRef<'a>(pub &'a [u8]);
52+
53+
/// A band in a side-band channel.
54+
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
55+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
56+
pub enum BandRef<'a> {
57+
/// A band carrying data.
58+
Data(&'a [u8]),
59+
/// A band carrying user readable progress information.
60+
Progress(&'a [u8]),
61+
/// A band carrying user readable errors.
62+
Error(&'a [u8]),
63+
}
64+
4265
///
4366
pub mod read;
4467
#[doc(inline)]

git-packetline/src/immutable/async_io.rs renamed to git-packetline/src/line/async_io.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,30 @@ use std::io;
22

33
use futures_io::AsyncWrite;
44

5-
use crate::{
6-
encode,
7-
immutable::{Band, Error, Text},
8-
Channel, PacketLineRef,
9-
};
5+
use crate::{encode, BandRef, Channel, ErrorRef, PacketLineRef, TextRef};
106

11-
impl<'a> Band<'a> {
7+
impl<'a> BandRef<'a> {
128
/// Serialize this instance to `out`, returning the amount of bytes written.
139
///
1410
/// The data written to `out` can be decoded with [`Borrowed::decode_band()]`.
1511
pub async fn write_to(&self, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
1612
match self {
17-
Band::Data(d) => encode::band_to_write(Channel::Data, d, out),
18-
Band::Progress(d) => encode::band_to_write(Channel::Progress, d, out),
19-
Band::Error(d) => encode::band_to_write(Channel::Error, d, out),
13+
BandRef::Data(d) => encode::band_to_write(Channel::Data, d, out),
14+
BandRef::Progress(d) => encode::band_to_write(Channel::Progress, d, out),
15+
BandRef::Error(d) => encode::band_to_write(Channel::Error, d, out),
2016
}
2117
.await
2218
}
2319
}
2420

25-
impl<'a> Text<'a> {
21+
impl<'a> TextRef<'a> {
2622
/// Serialize this instance to `out`, appending a newline if there is none, returning the amount of bytes written.
2723
pub async fn write_to(&self, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
2824
encode::text_to_write(self.0, out).await
2925
}
3026
}
3127

32-
impl<'a> Error<'a> {
28+
impl<'a> ErrorRef<'a> {
3329
/// Serialize this line as error to `out`.
3430
///
3531
/// This includes a marker to allow decoding it outside of a side-band channel, returning the amount of bytes written.

git-packetline/src/immutable/blocking_io.rs renamed to git-packetline/src/line/blocking_io.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
use std::io;
22

3-
use crate::{
4-
encode,
5-
immutable::{Band, Error, Text},
6-
Channel, PacketLineRef,
7-
};
3+
use crate::{encode, BandRef, Channel, ErrorRef, PacketLineRef, TextRef};
84

9-
impl<'a> Band<'a> {
5+
impl<'a> BandRef<'a> {
106
/// Serialize this instance to `out`, returning the amount of bytes written.
117
///
128
/// The data written to `out` can be decoded with [`Borrowed::decode_band()]`.
139
pub fn write_to(&self, out: impl io::Write) -> io::Result<usize> {
1410
match self {
15-
Band::Data(d) => encode::band_to_write(Channel::Data, d, out),
16-
Band::Progress(d) => encode::band_to_write(Channel::Progress, d, out),
17-
Band::Error(d) => encode::band_to_write(Channel::Error, d, out),
11+
BandRef::Data(d) => encode::band_to_write(Channel::Data, d, out),
12+
BandRef::Progress(d) => encode::band_to_write(Channel::Progress, d, out),
13+
BandRef::Error(d) => encode::band_to_write(Channel::Error, d, out),
1814
}
1915
}
2016
}
2117

22-
impl<'a> Text<'a> {
18+
impl<'a> TextRef<'a> {
2319
/// Serialize this instance to `out`, appending a newline if there is none, returning the amount of bytes written.
2420
pub fn write_to(&self, out: impl io::Write) -> io::Result<usize> {
2521
encode::text_to_write(self.0, out)
2622
}
2723
}
2824

29-
impl<'a> Error<'a> {
25+
impl<'a> ErrorRef<'a> {
3026
/// Serialize this line as error to `out`.
3127
///
3228
/// This includes a marker to allow decoding it outside of a side-band channel, returning the amount of bytes written.

git-packetline/src/immutable/mod.rs renamed to git-packetline/src/line/mod.rs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bstr::BStr;
22

3-
use crate::{decode, Channel, PacketLineRef, ERR_PREFIX};
3+
use crate::{decode, BandRef, Channel, ErrorRef, PacketLineRef, TextRef, ERR_PREFIX};
44

55
impl<'a> PacketLineRef<'a> {
66
/// Return this instance as slice if it's [`Data`][PacketLine::Data].
@@ -20,68 +20,58 @@ impl<'a> PacketLineRef<'a> {
2020
///
2121
/// Note that this creates an unchecked error using the slice verbatim, which is useful to [serialize it][Error::write_to()].
2222
/// See [`check_error()`][PacketLine::check_error()] for a version that assures the error information is in the expected format.
23-
pub fn as_error(&self) -> Option<Error<'_>> {
24-
self.as_slice().map(Error)
23+
pub fn as_error(&self) -> Option<ErrorRef<'_>> {
24+
self.as_slice().map(ErrorRef)
2525
}
2626
/// Check this instance's [`as_slice()`][PacketLine::as_slice()] is a valid [`Error`] and return it.
2727
///
2828
/// This works for any data received in an error [channel][crate::Channel].
29-
pub fn check_error(&self) -> Option<Error<'_>> {
29+
pub fn check_error(&self) -> Option<ErrorRef<'_>> {
3030
self.as_slice().and_then(|data| {
3131
if data.len() >= ERR_PREFIX.len() && &data[..ERR_PREFIX.len()] == ERR_PREFIX {
32-
Some(Error(&data[ERR_PREFIX.len()..]))
32+
Some(ErrorRef(&data[ERR_PREFIX.len()..]))
3333
} else {
3434
None
3535
}
3636
})
3737
}
3838
/// Return this instance as text, with the trailing newline truncated if present.
39-
pub fn as_text(&self) -> Option<Text<'_>> {
39+
pub fn as_text(&self) -> Option<TextRef<'_>> {
4040
self.as_slice().map(Into::into)
4141
}
4242

4343
/// Interpret the data in this [`slice`][PacketLine::as_slice()] as [`Band`] according to the given `kind` of channel.
4444
///
4545
/// Note that this is only relevant in a side-band channel.
4646
/// See [`decode_band()`][PacketLine::decode_band()] in case `kind` is unknown.
47-
pub fn as_band(&self, kind: Channel) -> Option<Band<'_>> {
47+
pub fn as_band(&self, kind: Channel) -> Option<BandRef<'_>> {
4848
self.as_slice().map(|d| match kind {
49-
Channel::Data => Band::Data(d),
50-
Channel::Progress => Band::Progress(d),
51-
Channel::Error => Band::Error(d),
49+
Channel::Data => BandRef::Data(d),
50+
Channel::Progress => BandRef::Progress(d),
51+
Channel::Error => BandRef::Error(d),
5252
})
5353
}
5454

5555
/// Decode the band of this [`slice`][PacketLine::as_slice()], or panic if it is not actually a side-band line.
56-
pub fn decode_band(&self) -> Result<Band<'_>, decode::band::Error> {
56+
pub fn decode_band(&self) -> Result<BandRef<'_>, decode::band::Error> {
5757
let d = self.as_slice().ok_or(decode::band::Error::NonDataLine)?;
5858
Ok(match d[0] {
59-
1 => Band::Data(&d[1..]),
60-
2 => Band::Progress(&d[1..]),
61-
3 => Band::Error(&d[1..]),
59+
1 => BandRef::Data(&d[1..]),
60+
2 => BandRef::Progress(&d[1..]),
61+
3 => BandRef::Error(&d[1..]),
6262
band => return Err(decode::band::Error::InvalidSideBand(band)),
6363
})
6464
}
6565
}
6666

67-
/// A packet line representing an Error in a side-band channel.
68-
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
69-
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
70-
pub struct Error<'a>(pub &'a [u8]);
71-
72-
/// A packet line representing text, which may include a trailing newline.
73-
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
74-
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
75-
pub struct Text<'a>(pub &'a [u8]);
76-
77-
impl<'a> From<&'a [u8]> for Text<'a> {
67+
impl<'a> From<&'a [u8]> for TextRef<'a> {
7868
fn from(d: &'a [u8]) -> Self {
7969
let d = if d[d.len() - 1] == b'\n' { &d[..d.len() - 1] } else { d };
80-
Text(d)
70+
TextRef(d)
8171
}
8272
}
8373

84-
impl<'a> Text<'a> {
74+
impl<'a> TextRef<'a> {
8575
/// Return this instance's data.
8676
pub fn as_slice(&self) -> &[u8] {
8777
self.0
@@ -92,18 +82,6 @@ impl<'a> Text<'a> {
9282
}
9383
}
9484

95-
/// A band in a side-band channel.
96-
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
97-
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
98-
pub enum Band<'a> {
99-
/// A band carrying data.
100-
Data(&'a [u8]),
101-
/// A band carrying user readable progress information.
102-
Progress(&'a [u8]),
103-
/// A band carrying user readable errors.
104-
Error(&'a [u8]),
105-
}
106-
10785
#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
10886
mod async_io;
10987
#[cfg(feature = "blocking-io")]

git-packetline/src/read/sidebands/async_io.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ use std::{
77
use futures_io::{AsyncBufRead, AsyncRead};
88
use futures_lite::ready;
99

10-
use crate::{
11-
decode,
12-
immutable::{Band, Text},
13-
PacketLineRef, StreamingPeekableIter, U16_HEX_BYTES,
14-
};
10+
use crate::{decode, BandRef, PacketLineRef, StreamingPeekableIter, TextRef, U16_HEX_BYTES};
1511

1612
type ReadLineResult<'a> = Option<std::io::Result<Result<PacketLineRef<'a>, decode::Error>>>;
1713
/// An implementor of [`AsyncBufRead`] yielding packet lines on each call to [`read_line()`][AsyncBufRead::read_line()].
@@ -238,13 +234,13 @@ where
238234
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
239235
const ENCODED_BAND: usize = 1;
240236
match band {
241-
Band::Data(d) => break (U16_HEX_BYTES + ENCODED_BAND, d.len()),
242-
Band::Progress(d) => {
243-
let text = Text::from(d).0;
237+
BandRef::Data(d) => break (U16_HEX_BYTES + ENCODED_BAND, d.len()),
238+
BandRef::Progress(d) => {
239+
let text = TextRef::from(d).0;
244240
handle_progress(false, text);
245241
}
246-
Band::Error(d) => {
247-
let text = Text::from(d).0;
242+
BandRef::Error(d) => {
243+
let text = TextRef::from(d).0;
248244
handle_progress(true, text);
249245
}
250246
};

git-packetline/src/read/sidebands/blocking_io.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::{io, io::BufRead};
22

3-
use crate::{
4-
immutable::{Band, Text},
5-
PacketLineRef, StreamingPeekableIter, U16_HEX_BYTES,
6-
};
3+
use crate::{BandRef, PacketLineRef, StreamingPeekableIter, TextRef, U16_HEX_BYTES};
74

85
/// An implementor of [`BufRead`][io::BufRead] yielding packet lines on each call to [`read_line()`][io::BufRead::read_line()].
96
/// It's also possible to hide the underlying packet lines using the [`Read`][io::Read] implementation which is useful
@@ -116,13 +113,13 @@ where
116113
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
117114
const ENCODED_BAND: usize = 1;
118115
match band {
119-
Band::Data(d) => break (U16_HEX_BYTES + ENCODED_BAND, d.len()),
120-
Band::Progress(d) => {
121-
let text = Text::from(d).0;
116+
BandRef::Data(d) => break (U16_HEX_BYTES + ENCODED_BAND, d.len()),
117+
BandRef::Progress(d) => {
118+
let text = TextRef::from(d).0;
122119
handle_progress(false, text);
123120
}
124-
Band::Error(d) => {
125-
let text = Text::from(d).0;
121+
BandRef::Error(d) => {
122+
let text = TextRef::from(d).0;
126123
handle_progress(true, text);
127124
}
128125
};

git-packetline/tests/decode/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
mod streaming {
22
use git_packetline::{
33
decode::{self, streaming, Stream},
4-
immutable::Error,
5-
PacketLineRef,
4+
ErrorRef, PacketLineRef,
65
};
76

87
use crate::assert_err_display;
@@ -118,7 +117,7 @@ mod streaming {
118117
)?;
119118
assert_eq!(
120119
line.check_error().expect("error to be parsed here"),
121-
Error(b"the error")
120+
ErrorRef(b"the error")
122121
);
123122
Ok(())
124123
}

git-transport/src/client/async_io/request.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ impl<'a> RequestWriter<'a> {
7575
.write_to(self.writer.inner_mut())
7676
.await
7777
}
78-
MessageKind::Text(t) => {
79-
git_packetline::immutable::Text::from(t)
80-
.write_to(self.writer.inner_mut())
81-
.await
82-
}
78+
MessageKind::Text(t) => git_packetline::TextRef::from(t).write_to(self.writer.inner_mut()).await,
8379
}
8480
.map(|_| ())
8581
}

git-transport/src/client/blocking_io/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'a> RequestWriter<'a> {
5050
MessageKind::Flush => git_packetline::PacketLineRef::Flush.write_to(self.writer.inner_mut()),
5151
MessageKind::Delimiter => git_packetline::PacketLineRef::Delimiter.write_to(self.writer.inner_mut()),
5252
MessageKind::ResponseEnd => git_packetline::PacketLineRef::ResponseEnd.write_to(self.writer.inner_mut()),
53-
MessageKind::Text(t) => git_packetline::immutable::Text::from(t).write_to(self.writer.inner_mut()),
53+
MessageKind::Text(t) => git_packetline::TextRef::from(t).write_to(self.writer.inner_mut()),
5454
}
5555
.map(|_| ())
5656
}

0 commit comments

Comments
 (0)