use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use alpha2_media::{DecodeError, SourceFailure};
use super::{RemoteError, RemoteFailure, RemoteObject};
#[derive(Clone)]
pub enum SourceLocation {
File(PathBuf),
Remote(Arc<RemoteObject>),
}
impl SourceLocation {
#[must_use]
pub fn path(&self) -> Option<&Path> {
match self {
Self::File(path) => Some(path),
Self::Remote(_) => None,
}
}
#[must_use]
pub const fn remote(&self) -> Option<&Arc<RemoteObject>> {
match self {
Self::File(_) => None,
Self::Remote(object) => Some(object),
}
}
#[must_use]
pub fn shown(&self) -> String {
match self {
Self::File(path) => path.display().to_string(),
Self::Remote(object) => object.shown_url().to_owned(),
}
}
pub fn byte_length(&self) -> std::io::Result<u64> {
match self {
Self::File(path) => std::fs::metadata(path).map(|metadata| metadata.len()),
Self::Remote(object) => Ok(object.len()),
}
}
}
impl std::fmt::Debug for SourceLocation {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(&self.shown())
}
}
impl PartialEq for SourceLocation {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::File(a), Self::File(b)) => a == b,
(Self::Remote(a), Self::Remote(b)) => Arc::ptr_eq(a, b),
_ => false,
}
}
}
impl Eq for SourceLocation {}
impl Hash for SourceLocation {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Self::File(path) => path.hash(state),
Self::Remote(object) => std::ptr::hash(Arc::as_ptr(object), state),
}
}
}
impl From<PathBuf> for SourceLocation {
fn from(path: PathBuf) -> Self {
Self::File(path)
}
}
impl From<&Path> for SourceLocation {
fn from(path: &Path) -> Self {
Self::File(path.to_path_buf())
}
}
impl From<&PathBuf> for SourceLocation {
fn from(path: &PathBuf) -> Self {
Self::File(path.clone())
}
}
impl From<&SourceLocation> for SourceLocation {
fn from(location: &SourceLocation) -> Self {
location.clone()
}
}
impl From<&RemoteError> for SourceFailure {
fn from(error: &RemoteError) -> Self {
Self {
code: error.failure.code(),
message: error.message.clone(),
url: error.url.clone(),
status: error.status,
retry_after_ms: match error.failure {
RemoteFailure::Busy { retry_after_ms } => Some(retry_after_ms),
_ => None,
},
policy: match error.failure {
RemoteFailure::Refused(policy) => Some(policy.name()),
_ => None,
},
}
}
}
#[must_use]
pub fn decode_error(error: &std::io::Error) -> Option<DecodeError> {
let remote = RemoteError::of(error)?;
Some(if remote.failure == RemoteFailure::Cancelled {
DecodeError::Cancelled
} else {
DecodeError::Source(SourceFailure::from(remote))
})
}