Skip to content

Commit

Permalink
feat: add git_packetline::read::Error to represent ERR lines (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 19, 2021
1 parent fb750b6 commit 454c840
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
11 changes: 9 additions & 2 deletions git-packetline/src/read/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,16 @@ where
return (true, stopped_at, None);
} else if fail_on_err_lines {
if let Some(err) = line.check_error() {
let err = err.0.as_bstr().to_string();
let err = err.0.as_bstr().to_owned();
buf.clear();
return (true, None, Some(Err(io::Error::new(io::ErrorKind::Other, err))));
return (
true,
None,
Some(Err(io::Error::new(
io::ErrorKind::Other,
crate::read::Error { message: err },
))),
);
}
}
let len = line
Expand Down
11 changes: 9 additions & 2 deletions git-packetline/src/read/blocking_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ where
return (true, stopped_at, None);
} else if fail_on_err_lines {
if let Some(err) = line.check_error() {
let err = err.0.as_bstr().to_string();
let err = err.0.as_bstr().to_owned();
buf.clear();
return (true, None, Some(Err(io::Error::new(io::ErrorKind::Other, err))));
return (
true,
None,
Some(Err(io::Error::new(
io::ErrorKind::Other,
crate::read::Error { message: err },
))),
);
}
}
let len = line
Expand Down
22 changes: 22 additions & 0 deletions git-packetline/src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ type ExhaustiveOutcome<'a> = (
Option<std::io::Result<Result<PacketLineRef<'a>, crate::decode::Error>>>, // actual method result
);

mod error {
use bstr::BString;
use std::fmt::{Debug, Display, Formatter};

/// The error representing an ERR packet line, as possibly wrapped into an `std::io::Error` in
/// [`read_line(…)`][super::StreamingPeekableIter::read_line()].
#[derive(Debug)]
pub struct Error {
/// The contents of the ERR line, with `ERR` portion stripped.
pub message: BString,
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.message, f)
}
}

impl std::error::Error for Error {}
}
pub use error::Error;

impl<T> StreamingPeekableIter<T> {
/// Return a new instance from `read` which will stop decoding packet lines when receiving one of the given `delimiters`.
pub fn new(read: T, delimiters: &'static [PacketLineRef<'static>]) -> Self {
Expand Down
9 changes: 7 additions & 2 deletions git-packetline/tests/read/sideband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,15 @@ async fn handling_of_err_lines() {
let mut buf = [0u8; 2];
let mut reader = rd.as_read();
let res = reader.read(buf.as_mut()).await;
let err = res.unwrap_err();
assert_eq!(err.to_string(), "e", "it respects errors and passes them on");
assert_eq!(
res.unwrap_err().to_string(),
err.into_inner()
.expect("inner err")
.downcast::<git_packetline::read::Error>()
.expect("it's this type")
.message,
"e",
"it respects errors and passes them on"
);
let res = reader.read(buf.as_mut()).await;
assert_eq!(
Expand Down

0 comments on commit 454c840

Please sign in to comment.