mirror of
https://github.com/librespot-org/librespot.git
synced 2024-11-08 16:45:43 +00:00
strongly type ActivityPeriod
(#1055)
switch fallibly to unsigned integers
This commit is contained in:
parent
5451d14972
commit
762f6d1a6f
2 changed files with 44 additions and 14 deletions
|
@ -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 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 {
|
||||
fn from(err: ProtobufError) -> Self {
|
||||
Self::new(ErrorKind::FailedPrecondition, err)
|
||||
|
|
|
@ -111,10 +111,12 @@ pub struct Biographies(pub Vec<Biography>);
|
|||
impl_deref_wrapped!(Biographies, Vec<Biography>);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ActivityPeriod {
|
||||
pub start_year: i32,
|
||||
pub end_year: i32,
|
||||
pub decade: i32,
|
||||
pub enum ActivityPeriod {
|
||||
Timespan {
|
||||
start_year: u16,
|
||||
end_year: Option<u16>,
|
||||
},
|
||||
Decade(u16),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
|
@ -196,7 +198,7 @@ impl TryFrom<&<Self as Metadata>::Message> for Artist {
|
|||
external_ids: artist.get_external_id().into(),
|
||||
portraits: artist.get_portrait().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(),
|
||||
related: artist.get_related().try_into()?,
|
||||
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<&ActivityPeriodMessage> for ActivityPeriod {
|
||||
fn from(activity_periode: &ActivityPeriodMessage) -> Self {
|
||||
Self {
|
||||
start_year: activity_periode.get_start_year(),
|
||||
end_year: activity_periode.get_end_year(),
|
||||
decade: activity_periode.get_decade(),
|
||||
}
|
||||
impl TryFrom<&ActivityPeriodMessage> for ActivityPeriod {
|
||||
type Error = librespot_core::Error;
|
||||
|
||||
fn try_from(period: &ActivityPeriodMessage) -> Result<Self, Self::Error> {
|
||||
let activity_period = match (
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue