Skip to content

Commit

Permalink
Generate ApplyTcpOptionsError types and impls
Browse files Browse the repository at this point in the history
  • Loading branch information
hulthe committed Apr 9, 2024
1 parent 2ec515f commit a1764d4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 71 deletions.
62 changes: 56 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ lazy_static = "1.4.0"
# pulled in when built as a library.
env_logger = { version = "0.11.3", optional = true }
cadence = { version = "1.0.0", optional = true }
thiserror = "1.0.58"
strum = { version = "0.26.2", features = ["derive"] }

[target.'cfg(target_os = "linux")'.dependencies]
nix = { version = "0.27.1", features = ["socket"] }
86 changes: 21 additions & 65 deletions src/tcp_options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[cfg(target_os = "linux")]
use nix::sys::socket::{getsockopt, setsockopt, sockopt};
use std::fmt;
use std::io;
use std::time::Duration;
use tokio::net::{TcpSocket, TcpStream};
Expand Down Expand Up @@ -36,80 +35,37 @@ pub struct TcpOptions {
}

/// Represents a failure to apply socket options to the TCP socket.
#[derive(Debug)]
pub struct ApplyTcpOptionsError(ApplyTcpOptionsErrorInternal);

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct ApplyTcpOptionsError(#[from] ApplyTcpOptionsErrorInternal);

#[derive(Debug, thiserror::Error, strum::EnumDiscriminants)]
#[strum_discriminants(vis(pub))]
#[strum_discriminants(name(ApplyTcpOptionsErrorKind))]
#[strum_discriminants(cfg_attr(all(), non_exhaustive))] // hacky way of bypassing strum limitations
enum ApplyTcpOptionsErrorInternal {
RecvBuffer(io::Error),
SendBuffer(io::Error),
#[cfg(target_os = "linux")]
Mark(nix::Error),
TcpNoDelay(io::Error),
}

/// A list specifying what failed when applying the TCP options.
#[derive(Debug, Copy, Clone)]
#[non_exhaustive]
pub enum ApplyTcpOptionsErrorKind {
/// Failed to get/set TCP_RCVBUF
RecvBuffer,
/// "Failed to get/set TCP_RCVBUF")]
#[error("Failed to get/set TCP_RCVBUF")]
RecvBuffer(#[source] io::Error),

/// Failed to get/set TCP_SNDBUF
SendBuffer,
/// "Failed to get/set TCP_SNDBUF")]
#[error("Failed to get/set TCP_SNDBUF")]
SendBuffer(#[source] io::Error),

/// Failed to get/set SO_MARK
/// "Failed to get/set SO_MARK")]
#[cfg(target_os = "linux")]
Mark,
#[error("Failed to get/set SO_MARK")]
Mark(#[source] nix::Error),

/// Failed to get/set TCP_NODELAY
TcpNoDelay,
/// "Failed to get/set TCP_NODELAY")]
#[error("Failed to get/set TCP_NODELAY")]
TcpNoDelay(#[source] io::Error),
}

impl ApplyTcpOptionsError {
/// Returns the kind of error that happened as an enum
pub fn kind(&self) -> ApplyTcpOptionsErrorKind {
use ApplyTcpOptionsErrorInternal::*;
match self.0 {
RecvBuffer(_) => ApplyTcpOptionsErrorKind::RecvBuffer,
SendBuffer(_) => ApplyTcpOptionsErrorKind::SendBuffer,
#[cfg(target_os = "linux")]
Mark(_) => ApplyTcpOptionsErrorKind::Mark,
TcpNoDelay(_) => ApplyTcpOptionsErrorKind::TcpNoDelay,
}
}
}

impl From<ApplyTcpOptionsErrorInternal> for ApplyTcpOptionsError {
fn from(value: ApplyTcpOptionsErrorInternal) -> Self {
Self(value)
}
}

impl fmt::Display for ApplyTcpOptionsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use ApplyTcpOptionsErrorInternal::*;
match self.0 {
RecvBuffer(_) => "Failed to get/set TCP_RCVBUF",
SendBuffer(_) => "Failed to get/set TCP_SNDBUF",
#[cfg(target_os = "linux")]
Mark(_) => "Failed to get/set SO_MARK",
TcpNoDelay(_) => "Failed to get/set TCP_NODELAY",
}
.fmt(f)
}
}

impl std::error::Error for ApplyTcpOptionsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use ApplyTcpOptionsErrorInternal::*;
match &self.0 {
RecvBuffer(e) => Some(e),
SendBuffer(e) => Some(e),
#[cfg(target_os = "linux")]
Mark(e) => Some(e),
TcpNoDelay(e) => Some(e),
}
ApplyTcpOptionsErrorKind::from(&self.0)
}
}

Expand Down

0 comments on commit a1764d4

Please sign in to comment.