Skip to content

Commit

Permalink
Use From<ReadError>
Browse files Browse the repository at this point in the history
Since `ReadError` provides `From<ReadError>` to convert to `io::Error`,
we can use it to simplify the code.
  • Loading branch information
msk committed May 17, 2024
1 parent 7d27ea8 commit 032da30
Showing 1 changed file with 1 addition and 76 deletions.
77 changes: 1 addition & 76 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,82 +52,7 @@ pub async fn recv_raw<'b>(recv: &mut RecvStream, buf: &mut Vec<u8>) -> io::Resul
fn from_read_exact_error_to_io_error(e: quinn::ReadExactError) -> io::Error {
match e {
quinn::ReadExactError::FinishedEarly(_) => io::Error::from(io::ErrorKind::UnexpectedEof),
quinn::ReadExactError::ReadError(e) => from_read_error_to_io_error(e),
}
}

fn from_read_error_to_io_error(e: quinn::ReadError) -> io::Error {
use quinn::ReadError;

match e {
ReadError::Reset(_) => io::Error::from(io::ErrorKind::ConnectionReset),
ReadError::ConnectionLost(e) => from_connection_error_to_io_error(e),
ReadError::ClosedStream => io::Error::new(io::ErrorKind::Other, "closed stream"),
ReadError::IllegalOrderedRead => {
io::Error::new(io::ErrorKind::InvalidInput, "illegal ordered read")
}
ReadError::ZeroRttRejected => {
io::Error::new(io::ErrorKind::ConnectionRefused, "0-RTT rejected")
}
}
}

fn from_connection_error_to_io_error(e: quinn::ConnectionError) -> io::Error {
use quinn::ConnectionError;

match e {
ConnectionError::VersionMismatch => io::Error::from(io::ErrorKind::ConnectionRefused),
ConnectionError::TransportError(e) => from_transport_error_to_io_error(e),
ConnectionError::ConnectionClosed(e) => io::Error::new(
io::ErrorKind::ConnectionAborted,
String::from_utf8_lossy(&e.reason),
),
ConnectionError::ApplicationClosed(e) => io::Error::new(
io::ErrorKind::ConnectionAborted,
String::from_utf8_lossy(&e.reason),
),
ConnectionError::Reset => io::Error::from(io::ErrorKind::ConnectionReset),
ConnectionError::TimedOut => io::Error::from(io::ErrorKind::TimedOut),
ConnectionError::LocallyClosed => {
io::Error::new(io::ErrorKind::Other, "connection locally closed")
}
ConnectionError::CidsExhausted => {
io::Error::new(io::ErrorKind::Other, "cid space exhausted")
}
}
}

fn from_transport_error_to_io_error(e: quinn_proto::TransportError) -> io::Error {
use quinn_proto::TransportErrorCode;

match e.code {
TransportErrorCode::CONNECTION_REFUSED => {
io::Error::new(io::ErrorKind::ConnectionRefused, e.reason)
}
TransportErrorCode::CONNECTION_ID_LIMIT_ERROR
| TransportErrorCode::CRYPTO_BUFFER_EXCEEDED
| TransportErrorCode::FINAL_SIZE_ERROR
| TransportErrorCode::FLOW_CONTROL_ERROR
| TransportErrorCode::FRAME_ENCODING_ERROR
| TransportErrorCode::INVALID_TOKEN
| TransportErrorCode::PROTOCOL_VIOLATION
| TransportErrorCode::STREAM_LIMIT_ERROR
| TransportErrorCode::STREAM_STATE_ERROR
| TransportErrorCode::TRANSPORT_PARAMETER_ERROR => {
io::Error::new(io::ErrorKind::InvalidData, e.reason)
}
TransportErrorCode::NO_VIABLE_PATH => {
// TODO: Use `io::ErrorKind::HostUnreachable` when it is stabilized
io::Error::new(io::ErrorKind::Other, e.reason)
}
_ => {
// * TransportErrorCode::AEAD_LIMIT_REACHED
// * TransportErrorCode::APPLICATION_ERROR
// * TransportErrorCode::INTERNAL_ERROR
// * TransportErrorCode::KEY_UPDATE_ERROR
// * TransportErrorCode::NO_ERROR
io::Error::new(io::ErrorKind::Other, e.reason)
}
quinn::ReadExactError::ReadError(e) => e.into(),

Check warning on line 55 in src/frame.rs

View check run for this annotation

Codecov / codecov/patch

src/frame.rs#L55

Added line #L55 was not covered by tests
}
}

Expand Down

0 comments on commit 032da30

Please sign in to comment.