strongly type ActivityPeriod (#1055)

switch fallibly to unsigned integers
This commit is contained in:
eladyn 2022-09-03 09:59:53 +02:00 committed by GitHub
parent 5451d14972
commit 762f6d1a6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 14 deletions

View file

@ -1,4 +1,9 @@
use std::{error, fmt, num::ParseIntError, str::Utf8Error, string::FromUtf8Error}; use std::{
error, fmt,
num::{ParseIntError, TryFromIntError},
str::Utf8Error,
string::FromUtf8Error,
};
use base64::DecodeError; use base64::DecodeError;
use http::{ use http::{
@ -419,6 +424,12 @@ impl From<ParseIntError> for Error {
} }
} }
impl From<TryFromIntError> for Error {
fn from(err: TryFromIntError) -> Self {
Self::new(ErrorKind::FailedPrecondition, err)
}
}
impl From<ProtobufError> for Error { impl From<ProtobufError> for Error {
fn from(err: ProtobufError) -> Self { fn from(err: ProtobufError) -> Self {
Self::new(ErrorKind::FailedPrecondition, err) Self::new(ErrorKind::FailedPrecondition, err)

View file

@ -111,10 +111,12 @@ pub struct Biographies(pub Vec<Biography>);
impl_deref_wrapped!(Biographies, Vec<Biography>); impl_deref_wrapped!(Biographies, Vec<Biography>);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ActivityPeriod { pub enum ActivityPeriod {
pub start_year: i32, Timespan {
pub end_year: i32, start_year: u16,
pub decade: i32, end_year: Option<u16>,
},
Decade(u16),
} }
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@ -196,7 +198,7 @@ impl TryFrom<&<Self as Metadata>::Message> for Artist {
external_ids: artist.get_external_id().into(), external_ids: artist.get_external_id().into(),
portraits: artist.get_portrait().into(), portraits: artist.get_portrait().into(),
biographies: artist.get_biography().into(), biographies: artist.get_biography().into(),
activity_periods: artist.get_activity_period().into(), activity_periods: artist.get_activity_period().try_into()?,
restrictions: artist.get_restriction().into(), restrictions: artist.get_restriction().into(),
related: artist.get_related().try_into()?, related: artist.get_related().try_into()?,
is_portrait_album_cover: artist.get_is_portrait_album_cover(), is_portrait_album_cover: artist.get_is_portrait_album_cover(),
@ -270,14 +272,31 @@ impl From<&BiographyMessage> for Biography {
impl_from_repeated!(BiographyMessage, Biographies); impl_from_repeated!(BiographyMessage, Biographies);
impl From<&ActivityPeriodMessage> for ActivityPeriod { impl TryFrom<&ActivityPeriodMessage> for ActivityPeriod {
fn from(activity_periode: &ActivityPeriodMessage) -> Self { type Error = librespot_core::Error;
Self {
start_year: activity_periode.get_start_year(), fn try_from(period: &ActivityPeriodMessage) -> Result<Self, Self::Error> {
end_year: activity_periode.get_end_year(), let activity_period = match (
decade: activity_periode.get_decade(), period.has_decade(),
period.has_start_year(),
period.has_end_year(),
) {
// (decade, start_year, end_year)
(true, false, false) => Self::Decade(period.get_decade().try_into()?),
(false, true, closed_period) => Self::Timespan {
start_year: period.get_start_year().try_into()?,
end_year: closed_period
.then(|| period.get_end_year().try_into())
.transpose()?,
},
_ => {
return Err(librespot_core::Error::failed_precondition(
"ActivityPeriod is expected to be either a decade or timespan",
))
} }
};
Ok(activity_period)
} }
} }
impl_from_repeated!(ActivityPeriodMessage, ActivityPeriods); impl_try_from_repeated!(ActivityPeriodMessage, ActivityPeriods);