Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: general small refactors to simplify code #1382

Merged
merged 1 commit into from
Feb 3, 2024

Conversation

ThomasFrans
Copy link
Contributor

Some small general refactoring to improve code readability and adhere to Rust conventions.

Describe your changes

  • Remove expect in favor of unwrap when the Result's error variant contains the info in the expect anyway (eg. when locking things). The line number/context are given by the backtrace.
  • Remove over-specification of types (&T instead of &RWReadLockGuard)
  • Put reused values into constants
  • try_from instead of manual function
  • Change if let Some(()) = ... to if T.is_some()

Checklist before requesting a review

  • Documentation was updated (i.e. due to changes in keybindings, commands, etc.)
  • Changelog was updated with relevant user-facing changes (eg. not dependency updates,
    not performance improvements, etc.)

@ThomasFrans
Copy link
Contributor Author

ThomasFrans commented Jan 28, 2024

These are some things I encountered while adding documentation in #1381. The only one that I'm not sure about is the removal of expect() in favor of unwrap(). The reasoning as explained in the PR message is that unwrap() provides the same information as expect() but looks a bit cleaner. The text in the expect() doesn't really add that much valuable information in the case of unlocking poisoned synchronization types and sending data through closed channels.

@ThomasFrans ThomasFrans marked this pull request as ready for review January 29, 2024 09:14
- Remove `expect` in favor of `unwrap` when the `Result`'s error variant
  contains the info in the `expect` anyway (eg. when locking things).
  The line number/context are given by the backtrace.
- Remove over-specification of types (`&T` instead of
  `&RWReadLockGuard`)
- Put reused values into constants
- `FromStr` instead of manual function
- Change `if let Some(()) = ...` to `if T.is_some()`
@ThomasFrans
Copy link
Contributor Author

ThomasFrans commented Feb 1, 2024

Before solving the merge conflicts, I quickly changed the UriType creation from From<&str> to FromStr as it's parsing the string.

@hrkfdn
Copy link
Owner

hrkfdn commented Feb 2, 2024

Thanks! Back then I thought the tiny extra context that expect() brings is beneficial, but I guess it's sort of pointless if you have a full stack trace.

@ThomasFrans
Copy link
Contributor Author

ThomasFrans commented Feb 2, 2024

There is the stack trace to point out the location and there is the general message of the error that says what went wrong. I think one of the next Rust releases is going to remove a bunch of debug information by default by stripping every binary, whether it's in the profile or not. You could always disable the stripping again if that happens. I quickly checked what another project like Alacritty does for locking and sending to channels and it seems like they also use unwrap(). Also checked Zellij and they use expect() for receiving from channels but unwrap() for locking. In the receive, they always use the same message so it doesn't give any context either.

If you'd like to keep the context of the expect(), I'd be happy to revert the change to unwrap() 🙂

Copy link
Owner

@hrkfdn hrkfdn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, let's get it in! :)

Comment on lines +77 to +84
let user = ASYNC_RUNTIME.get().unwrap().block_on(user_rx).ok();
let volume = cfg.state().volume;
spotify.set_volume(volume);

spotify.api.set_worker_channel(spotify.channel.clone());
spotify.api.update_token();

spotify.api.set_user(spotify.user.clone());
spotify.api.set_user(user);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not a problem, but this will set the user later than before. A quick check didn't give me anything, so I think this is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand. I thought nothing would change because Spotify::user was only used in the Spotify::new() method?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, yeah, I think I may have misread that. Sorry for the noise!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I understand. I grepped the project so I knew it wasn't used anywhere else. I can imagine you don't know every line off the top of your head 😄

(PS In the next days I'll probably try to solve/continue work on some of the bugs that have shown up in the last couple of months, if that's ok 🙂)

Comment on lines +446 to +468
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn parses_album_uri() {
let uri_type = "spotify:album:29F5MF6Q9VYlryDsYEQz6a".parse();
assert!(matches!(uri_type, Ok(UriType::Album)));
}

#[test]
fn parse_invalid_uri() {
let uri_type: Result<UriType, _> = "kayava".parse();
assert!(matches!(uri_type, Err(UriParseError)));
}

#[test]

fn parse_playlist_uri() {
let uri_type = "spotify:playlist:37i9dQZF1DX36Xw4IJIVKA".parse();
assert!(matches!(uri_type, Ok(UriType::Playlist)));
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🙏

@hrkfdn hrkfdn merged commit 3c8e546 into hrkfdn:main Feb 3, 2024
5 checks passed
@ThomasFrans ThomasFrans deleted the refactor-small-bits branch February 3, 2024 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants