diff --git a/src/lib.rs b/src/lib.rs index fd84005..035d134 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,7 @@ pub trait IMbid { fn mbid(&self) -> &str; } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug)] enum UrlType { MusicBrainz, MusicButler, @@ -36,12 +36,17 @@ enum UrlType { Qobuz, } -#[derive(Debug, PartialEq, Eq)] struct InvalidUrlError { url_type: UrlType, url: String, } +impl fmt::Display for InvalidUrlError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "invalid url of type {:?}: {}", self.url_type, self.url) + } +} + /// MusicBrainz reference. #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] pub struct MusicBrainz(Url); @@ -422,7 +427,7 @@ impl From for Error { impl From for Error { fn from(err: InvalidUrlError) -> Error { - Error::InvalidUrlError(format!("{:?} - {}", err.url_type, err.url)) + Error::InvalidUrlError(err.to_string()) } } @@ -620,11 +625,27 @@ mod tests { assert_eq!(url, mb.url()); assert_eq!(uuid, mb.mbid()); + let url = format!("not a url at all"); + let expected_error: Error = url::ParseError::RelativeUrlWithoutBase.into(); + let actual_error = MusicBrainz::new(&url).unwrap_err(); + assert_eq!(actual_error, expected_error); + assert_eq!(actual_error.to_string(), expected_error.to_string()); + let url = format!("https://musicbrainz.org/artist/i-am-not-a-uuid"); - assert!(MusicBrainz::new(&url).is_err()); + let expected_error: Error = Uuid::try_parse("i-am-not-a-uuid").unwrap_err().into(); + let actual_error = MusicBrainz::new(&url).unwrap_err(); + assert_eq!(actual_error, expected_error); + assert_eq!(actual_error.to_string(), expected_error.to_string()); let url = format!("https://musicbrainz.org/artist"); - assert!(MusicBrainz::new(&url).is_err()); + let expected_error: Error = InvalidUrlError { + url_type: UrlType::MusicBrainz, + url: url.clone(), + } + .into(); + let actual_error = MusicBrainz::new(&url).unwrap_err(); + assert_eq!(actual_error, expected_error); + assert_eq!(actual_error.to_string(), expected_error.to_string()); } #[test]