use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TokenScope {
#[default]
Play,
Download,
#[serde(other)]
Unknown,
}
impl TokenScope {
pub fn name(self) -> &'static str {
match self {
TokenScope::Play => "play",
TokenScope::Download => "download",
TokenScope::Unknown => "unknown",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AssertUser {
pub user: String,
pub display_name: String,
pub client_endpoint: String,
pub item: String,
#[serde(default)]
pub scope: TokenScope,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub preferences: Option<spoolframe_types::TrackPreferences>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub download: Option<String>,
}
impl AssertUser {
pub fn within_bounds(mut self) -> Self {
self.display_name = super::server_name(self.display_name);
self
}
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TokenGranted {
pub token: String,
pub scope: TokenScope,
pub expires_at: i64,
}
impl fmt::Debug for TokenGranted {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TokenGranted")
.field("token", &"<redacted>")
.field("scope", &self.scope)
.field("expires_at", &self.expires_at)
.finish()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AssertionRefused {
NotShared,
InvalidUser,
InvalidDevice,
InvalidScope,
Unavailable,
#[serde(other)]
Unknown,
}
impl fmt::Display for AssertionRefused {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
AssertionRefused::NotShared => "the item is not shared with this server",
AssertionRefused::InvalidUser => "that is not a user the server may vouch for",
AssertionRefused::InvalidDevice => "the device's id does not read",
AssertionRefused::InvalidScope => "a token for that is not made",
AssertionRefused::Unavailable => "the other server cannot answer now",
AssertionRefused::Unknown => "refused for a reason this build does not know",
})
}
}