Skip to content

Commit

Permalink
Merge pull request #3 from GnomedDev/symphonia-clippy
Browse files Browse the repository at this point in the history
Fix clippy::pedantic warnings
  • Loading branch information
FelixMcFelix authored Jun 10, 2022
2 parents bbd84c7 + cab6b96 commit 633ac83
Show file tree
Hide file tree
Showing 52 changed files with 639 additions and 653 deletions.
9 changes: 8 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Default for Config {
#[cfg(feature = "driver")]
preallocated_tracks: 1,
#[cfg(feature = "driver")]
driver_retry: Default::default(),
driver_retry: Retry::default(),
#[cfg(feature = "driver")]
driver_timeout: Some(Duration::from_secs(10)),
#[cfg(feature = "driver")]
Expand All @@ -138,36 +138,42 @@ impl Default for Config {
#[cfg(feature = "driver")]
impl Config {
/// Sets this `Config`'s chosen cryptographic tagging scheme.
#[must_use]
pub fn crypto_mode(mut self, crypto_mode: CryptoMode) -> Self {
self.crypto_mode = crypto_mode;
self
}

/// Sets this `Config`'s received packet decryption/decoding behaviour.
#[must_use]
pub fn decode_mode(mut self, decode_mode: DecodeMode) -> Self {
self.decode_mode = decode_mode;
self
}

/// Sets this `Config`'s audio mixing channel count.
#[must_use]
pub fn mix_mode(mut self, mix_mode: MixMode) -> Self {
self.mix_mode = mix_mode;
self
}

/// Sets this `Config`'s number of tracks to preallocate.
#[must_use]
pub fn preallocated_tracks(mut self, preallocated_tracks: usize) -> Self {
self.preallocated_tracks = preallocated_tracks;
self
}

/// Sets this `Config`'s timeout for establishing a voice connection.
#[must_use]
pub fn driver_timeout(mut self, driver_timeout: Option<Duration>) -> Self {
self.driver_timeout = driver_timeout;
self
}

/// Sets this `Config`'s voice connection retry configuration.
#[must_use]
pub fn driver_retry(mut self, driver_retry: Retry) -> Self {
self.driver_retry = driver_retry;
self
Expand All @@ -184,6 +190,7 @@ impl Config {
#[cfg(feature = "gateway")]
impl Config {
/// Sets this `Config`'s timeout for joining a voice channel.
#[must_use]
pub fn gateway_timeout(mut self, gateway_timeout: Option<Duration>) -> Self {
self.gateway_timeout = gateway_timeout;
self
Expand Down
48 changes: 24 additions & 24 deletions src/driver/connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,41 +94,41 @@ impl From<Elapsed> for Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to connect to Discord RTP server: ")?;
use Error::*;
match self {
AttemptDiscarded => write!(f, "connection attempt was aborted/discarded"),
Crypto(e) => e.fmt(f),
CryptoModeInvalid => write!(f, "server changed negotiated encryption mode"),
CryptoModeUnavailable => write!(f, "server did not offer chosen encryption mode"),
EndpointUrl => write!(f, "endpoint URL received from gateway was invalid"),
ExpectedHandshake => write!(f, "voice initialisation protocol was violated"),
IllegalDiscoveryResponse => write!(f, "IP discovery/NAT punching response was invalid"),
IllegalIp => write!(f, "IP discovery/NAT punching response had bad IP value"),
Io(e) => e.fmt(f),
Json(e) => e.fmt(f),
InterconnectFailure(e) => write!(f, "failed to contact other task ({:?})", e),
Ws(e) => write!(f, "websocket issue ({:?}).", e),
TimedOut => write!(f, "connection attempt timed out"),
Self::AttemptDiscarded => write!(f, "connection attempt was aborted/discarded"),
Self::Crypto(e) => e.fmt(f),
Self::CryptoModeInvalid => write!(f, "server changed negotiated encryption mode"),
Self::CryptoModeUnavailable => write!(f, "server did not offer chosen encryption mode"),
Self::EndpointUrl => write!(f, "endpoint URL received from gateway was invalid"),
Self::ExpectedHandshake => write!(f, "voice initialisation protocol was violated"),
Self::IllegalDiscoveryResponse =>
write!(f, "IP discovery/NAT punching response was invalid"),
Self::IllegalIp => write!(f, "IP discovery/NAT punching response had bad IP value"),
Self::Io(e) => e.fmt(f),
Self::Json(e) => e.fmt(f),
Self::InterconnectFailure(e) => write!(f, "failed to contact other task ({:?})", e),
Self::Ws(e) => write!(f, "websocket issue ({:?}).", e),
Self::TimedOut => write!(f, "connection attempt timed out"),
}
}
}

impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Error::AttemptDiscarded => None,
Error::AttemptDiscarded
| Error::CryptoModeInvalid
| Error::CryptoModeUnavailable
| Error::EndpointUrl
| Error::ExpectedHandshake
| Error::IllegalDiscoveryResponse
| Error::IllegalIp
| Error::InterconnectFailure(_)
| Error::Ws(_)
| Error::TimedOut => None,
Error::Crypto(e) => e.source(),
Error::CryptoModeInvalid => None,
Error::CryptoModeUnavailable => None,
Error::EndpointUrl => None,
Error::ExpectedHandshake => None,
Error::IllegalDiscoveryResponse => None,
Error::IllegalIp => None,
Error::Io(e) => e.source(),
Error::Json(e) => e.source(),
Error::InterconnectFailure(_) => None,
Error::Ws(_) => None,
Error::TimedOut => None,
}
}
}
Expand Down
62 changes: 30 additions & 32 deletions src/driver/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use xsalsa20poly1305::{
TAG_SIZE,
};

/// Variants of the XSalsa20Poly1305 encryption scheme.
/// Variants of the `XSalsa20Poly1305` encryption scheme.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CryptoMode {
Expand All @@ -35,57 +35,58 @@ pub enum CryptoMode {

impl From<CryptoState> for CryptoMode {
fn from(val: CryptoState) -> Self {
use CryptoState::*;
match val {
Normal => CryptoMode::Normal,
Suffix => CryptoMode::Suffix,
Lite(_) => CryptoMode::Lite,
CryptoState::Normal => Self::Normal,
CryptoState::Suffix => Self::Suffix,
CryptoState::Lite(_) => Self::Lite,
}
}
}

impl CryptoMode {
/// Returns the name of a mode as it will appear during negotiation.
#[must_use]
pub fn to_request_str(self) -> &'static str {
use CryptoMode::*;
match self {
Normal => "xsalsa20_poly1305",
Suffix => "xsalsa20_poly1305_suffix",
Lite => "xsalsa20_poly1305_lite",
Self::Normal => "xsalsa20_poly1305",
Self::Suffix => "xsalsa20_poly1305_suffix",
Self::Lite => "xsalsa20_poly1305_lite",
}
}

/// Returns the number of bytes each nonce is stored as within
/// a packet.
#[must_use]
pub fn nonce_size(self) -> usize {
use CryptoMode::*;
match self {
Normal => RtpPacket::minimum_packet_size(),
Suffix => NONCE_SIZE,
Lite => 4,
Self::Normal => RtpPacket::minimum_packet_size(),
Self::Suffix => NONCE_SIZE,
Self::Lite => 4,
}
}

/// Returns the number of bytes occupied by the encryption scheme
/// which fall before the payload.
pub fn payload_prefix_len(self) -> usize {
#[must_use]
pub fn payload_prefix_len() -> usize {
TAG_SIZE
}

/// Returns the number of bytes occupied by the encryption scheme
/// which fall after the payload.
#[must_use]
pub fn payload_suffix_len(self) -> usize {
use CryptoMode::*;
match self {
Normal => 0,
Suffix | Lite => self.nonce_size(),
Self::Normal => 0,
Self::Suffix | Self::Lite => self.nonce_size(),
}
}

/// Calculates the number of additional bytes required compared
/// to an unencrypted payload.
#[must_use]
pub fn payload_overhead(self) -> usize {
self.payload_prefix_len() + self.payload_suffix_len()
Self::payload_prefix_len() + self.payload_suffix_len()
}

/// Extracts the byte slice in a packet used as the nonce, and the remaining mutable
Expand All @@ -95,10 +96,9 @@ impl CryptoMode {
header: &'a [u8],
body: &'a mut [u8],
) -> Result<(&'a [u8], &'a mut [u8]), CryptoError> {
use CryptoMode::*;
match self {
Normal => Ok((header, body)),
Suffix | Lite => {
Self::Normal => Ok((header, body)),
Self::Suffix | Self::Lite => {
let len = body.len();
if len < self.payload_suffix_len() {
Err(CryptoError)
Expand Down Expand Up @@ -135,7 +135,7 @@ impl CryptoMode {
&nonce
};

let body_start = self.payload_prefix_len();
let body_start = Self::payload_prefix_len();
let body_tail = self.payload_suffix_len();

if body_start > body_remaining.len() {
Expand Down Expand Up @@ -183,7 +183,7 @@ impl CryptoMode {
}
}

/// State used in nonce generation for the XSalsa20Poly1305 encryption variants
/// State used in nonce generation for the `XSalsa20Poly1305` encryption variants
/// in [`CryptoMode`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
Expand All @@ -206,11 +206,10 @@ pub enum CryptoState {

impl From<CryptoMode> for CryptoState {
fn from(val: CryptoMode) -> Self {
use CryptoMode::*;
match val {
Normal => CryptoState::Normal,
Suffix => CryptoState::Suffix,
Lite => CryptoState::Lite(Wrapping(rand::random::<u32>())),
CryptoMode::Normal => CryptoState::Normal,
CryptoMode::Suffix => CryptoState::Suffix,
CryptoMode::Lite => CryptoState::Lite(Wrapping(rand::random::<u32>())),
}
}
}
Expand All @@ -225,12 +224,11 @@ impl CryptoState {
let mode = self.kind();
let endpoint = payload_end + mode.payload_suffix_len();

use CryptoState::*;
match self {
Suffix => {
Self::Suffix => {
rand::thread_rng().fill(&mut packet.payload_mut()[payload_end..endpoint]);
},
Lite(mut i) => {
Self::Lite(mut i) => {
(&mut packet.payload_mut()[payload_end..endpoint])
.write_u32::<NetworkEndian>(i.0)
.expect(
Expand All @@ -245,8 +243,8 @@ impl CryptoState {
}

/// Returns the underlying (stateless) type of the active crypto mode.
pub fn kind(&self) -> CryptoMode {
CryptoMode::from(*self)
pub fn kind(self) -> CryptoMode {
CryptoMode::from(self)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/driver/decode_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum DecodeMode {

impl DecodeMode {
/// Returns whether this mode will decrypt received packets.
#[must_use]
pub fn should_decrypt(self) -> bool {
self != DecodeMode::Pass
}
Expand Down
20 changes: 8 additions & 12 deletions src/driver/mix_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,30 @@ pub enum MixMode {

impl MixMode {
pub(crate) const fn to_opus(self) -> Channels {
use MixMode::*;
match self {
Mono => Channels::Mono,
Stereo => Channels::Stereo,
Self::Mono => Channels::Mono,
Self::Stereo => Channels::Stereo,
}
}

pub(crate) const fn sample_count_in_frame(self) -> usize {
use MixMode::*;
match self {
Mono => MONO_FRAME_SIZE,
Stereo => STEREO_FRAME_SIZE,
Self::Mono => MONO_FRAME_SIZE,
Self::Stereo => STEREO_FRAME_SIZE,
}
}

pub(crate) const fn channels(self) -> usize {
use MixMode::*;
match self {
Mono => 1,
Stereo => 2,
Self::Mono => 1,
Self::Stereo => 2,
}
}

pub(crate) const fn symph_layout(self) -> Layout {
use MixMode::*;
match self {
Mono => Layout::Mono,
Stereo => Layout::Stereo,
Self::Mono => Layout::Mono,
Self::Stereo => Layout::Stereo,
}
}
}
Expand Down
Loading

0 comments on commit 633ac83

Please sign in to comment.