2022-01-14 22:42:18 +00:00
|
|
|
use std::{
|
|
|
|
convert::{TryFrom, TryInto},
|
|
|
|
fmt::Debug,
|
|
|
|
ops::Deref,
|
|
|
|
};
|
2021-12-07 22:22:24 +00:00
|
|
|
|
|
|
|
use thiserror::Error;
|
|
|
|
|
2022-01-14 22:28:09 +00:00
|
|
|
use crate::util::try_from_repeated_message;
|
2021-12-07 22:22:24 +00:00
|
|
|
|
2021-12-16 21:42:37 +00:00
|
|
|
use librespot_core::date::Date;
|
2021-12-07 22:22:24 +00:00
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
use librespot_protocol as protocol;
|
2021-12-07 22:22:24 +00:00
|
|
|
use protocol::metadata::Availability as AvailabilityMessage;
|
|
|
|
|
|
|
|
pub type AudioItemAvailability = Result<(), UnavailabilityReason>;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Availability {
|
|
|
|
pub catalogue_strs: Vec<String>,
|
|
|
|
pub start: Date,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Availabilities(pub Vec<Availability>);
|
|
|
|
|
|
|
|
impl Deref for Availabilities {
|
|
|
|
type Target = Vec<Availability>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Error)]
|
|
|
|
pub enum UnavailabilityReason {
|
|
|
|
#[error("blacklist present and country on it")]
|
|
|
|
Blacklisted,
|
|
|
|
#[error("available date is in the future")]
|
|
|
|
Embargo,
|
2021-12-11 19:22:44 +00:00
|
|
|
#[error("required data was not present")]
|
|
|
|
NoData,
|
2021-12-07 22:22:24 +00:00
|
|
|
#[error("whitelist present and country not on it")]
|
|
|
|
NotWhitelisted,
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:28:09 +00:00
|
|
|
impl TryFrom<&AvailabilityMessage> for Availability {
|
|
|
|
type Error = librespot_core::Error;
|
|
|
|
fn try_from(availability: &AvailabilityMessage) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Self {
|
2021-12-07 22:22:24 +00:00
|
|
|
catalogue_strs: availability.get_catalogue_str().to_vec(),
|
2022-01-14 22:28:09 +00:00
|
|
|
start: availability.get_start().try_into()?,
|
|
|
|
})
|
2021-12-07 22:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:28:09 +00:00
|
|
|
try_from_repeated_message!(AvailabilityMessage, Availabilities);
|