More detailed error message in case of command parse error (#684)

* Refactored `command::parse`

* Removed unnecessary duplication in error msg

* Renamed `NotEnoughArgs` -> `InsufficientArgs`

* Inaccurate var name

* Ditch wordy error prefix

* Use `split_whitespace` instead of regex

* Cleanup unused regex import

* `insert` cmd fails fast

* Refactor: use `and_then` instead of `unwrap`

* Updated `Command::to_string`

* Added `Command::basename`

* Better err msg when running cmd in unsupported view, fully closes #597

* Sort `match` branches by their order in the enum
This commit is contained in:
cyqsimon
2022-01-02 03:48:34 +08:00
committed by GitHub
parent 075ecd0e24
commit 9771c36c7b
6 changed files with 562 additions and 304 deletions

View File

@@ -1,12 +1,29 @@
use std::fmt;
use crate::spotify::UriType;
use url::{Host, Url};
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct SpotifyUrl {
pub id: String,
pub uri_type: UriType,
}
impl fmt::Display for SpotifyUrl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let type_seg = match self.uri_type {
UriType::Album => "album",
UriType::Artist => "artist",
UriType::Episode => "episode",
UriType::Playlist => "playlist",
UriType::Show => "show",
UriType::Track => "track",
};
write!(f, "https://open.spotify.com/{}/{}", type_seg, self.id)
}
}
impl SpotifyUrl {
fn new(id: &str, uri_type: UriType) -> SpotifyUrl {
SpotifyUrl {
@@ -22,8 +39,8 @@ impl SpotifyUrl {
/// assert_eq!(result.id, "4uLU6hMCjMI75M1A2tKUQC");
/// assert_eq!(result.uri_type, URIType::Track);
/// ```
pub fn from_url(s: &str) -> Option<SpotifyUrl> {
let url = Url::parse(s).ok()?;
pub fn from_url<S: AsRef<str>>(s: S) -> Option<SpotifyUrl> {
let url = Url::parse(s.as_ref()).ok()?;
if url.host() != Some(Host::Domain("open.spotify.com")) {
return None;
}