Skip to content

Commit

Permalink
refactor: use ok_or_else for lazy error creation
Browse files Browse the repository at this point in the history
Replace ok_or with ok_or_else where error messages are static strings to avoid
unnecessary error allocations. Also extract HTTPS_PORT constant for better
readability.

This is a minor optimization that defers error creation until actually needed
during error handling paths.
  • Loading branch information
roderickvd committed Jan 19, 2025
1 parent 31754a6 commit b8a12eb
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl Config {
let re = Regex::new(r"https:\/\/.+\/app-web.*\.js").unwrap();
let url = re
.find(&source)
.ok_or(Error::not_found("unable to find app-web source"))?;
.ok_or_else(|| Error::not_found("unable to find app-web source"))?;

// Get the app-web source.
let url = url.as_str();
Expand All @@ -222,11 +222,11 @@ impl Config {
let re = Regex::new(r"0x61%2C(0x[0-9a-f]{2}%2C){6}0x67").unwrap();
let a = re
.find(&source)
.ok_or(Error::not_found("unable to find first half of secret key"))?;
.ok_or_else(|| Error::not_found("unable to find first half of secret key"))?;
let re = Regex::new(r"0x31%2C(0x[0-9a-f]{2}%2C){6}0x34").unwrap();
let b = re
.find(&source)
.ok_or(Error::not_found("unable to find second half of secret key"))?;
.ok_or_else(|| Error::not_found("unable to find second half of secret key"))?;

let a = Self::convert_half(a.as_str())?;
let b = Self::convert_half(b.as_str())?;
Expand Down
2 changes: 1 addition & 1 deletion src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl Decoder {
.format;
let default_track = demuxer
.default_track()
.ok_or(Error::not_found("default track not found"))?;
.ok_or_else(|| Error::not_found("default track not found"))?;

let codec_params = &default_track.codec_params;
let decoder = codecs.make(codec_params, &DecoderOptions::default())?;
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,11 @@ async fn run(args: Args) -> Result<ShutdownSignal> {
let email = secrets
.get("email")
.and_then(|email| email.as_str())
.ok_or(Error::unauthenticated("email not found"))?;
.ok_or_else(|| Error::unauthenticated("email not found"))?;
let password = secrets
.get("password")
.and_then(|password| password.as_str())
.ok_or(Error::unauthenticated("password not found"))?;
.ok_or_else(|| Error::unauthenticated("password not found"))?;

Credentials::Login {
email: email.to_string(),
Expand Down
6 changes: 3 additions & 3 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ impl Player {
let sources = self
.sources
.as_mut()
.ok_or(Error::unavailable("audio sources not available"))?;
.ok_or_else(|| Error::unavailable("audio sources not available"))?;

if track.handle().is_none() {
let download = tokio::time::timeout(Self::NETWORK_TIMEOUT, async {
Expand Down Expand Up @@ -930,7 +930,7 @@ impl Player {
fn sink_mut(&mut self) -> Result<&mut rodio::Sink> {
self.sink
.as_mut()
.ok_or(Error::unavailable("audio sink not available"))
.ok_or_else(|| Error::unavailable("audio sink not available"))
}

/// Starts or resumes playback.
Expand Down Expand Up @@ -1419,7 +1419,7 @@ impl Player {
// This prevents stalling the player when seeking in a track that has not started.
match track
.handle()
.ok_or(Error::unavailable("download not yet started"))
.ok_or_else(|| Error::unavailable("download not yet started"))
.and_then(|_| {
self.sink_mut()
.and_then(|sink| sink.try_seek(position).map_err(Into::into))
Expand Down
7 changes: 5 additions & 2 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub struct Http {
url: String,
}

/// Default HTTPS port.
const HTTPS_PORT: u16 = 443;

impl Http {
/// Creates proxy configuration from environment.
///
Expand Down Expand Up @@ -108,8 +111,8 @@ impl Http {
let target_url = Url::parse(target)?;
let host = target_url
.host_str()
.ok_or(Error::invalid_argument("target host not available"))?;
let port = target_url.port().unwrap_or(443);
.ok_or_else(|| Error::invalid_argument("target host not available"))?;
let port = target_url.port().unwrap_or(HTTPS_PORT);
let tcp_stream = TcpStream::connect(&self.url).await?;
Self::tunnel(tcp_stream, host, port, self.auth.as_ref()).await
}
Expand Down
2 changes: 1 addition & 1 deletion src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ impl Client {
let queue = self
.queue
.as_ref()
.ok_or(Error::internal("no active queue"))?;
.ok_or_else(|| Error::internal("no active queue"))?;

let player_position = self.player.position();
let mut position = player_position;
Expand Down

0 comments on commit b8a12eb

Please sign in to comment.