alpha2_host_core/remote/
location.rs1use std::hash::{Hash, Hasher};
4use std::path::{Path, PathBuf};
5use std::sync::Arc;
6
7use alpha2_media::{DecodeError, SourceFailure};
8
9use super::{RemoteError, RemoteFailure, RemoteObject};
10
11#[derive(Clone)]
13pub enum SourceLocation {
14 File(PathBuf),
16 Remote(Arc<RemoteObject>),
18}
19
20impl SourceLocation {
21 #[must_use]
23 pub fn path(&self) -> Option<&Path> {
24 match self {
25 Self::File(path) => Some(path),
26 Self::Remote(_) => None,
27 }
28 }
29
30 #[must_use]
32 pub const fn remote(&self) -> Option<&Arc<RemoteObject>> {
33 match self {
34 Self::File(_) => None,
35 Self::Remote(object) => Some(object),
36 }
37 }
38
39 #[must_use]
41 pub fn shown(&self) -> String {
42 match self {
43 Self::File(path) => path.display().to_string(),
44 Self::Remote(object) => object.shown_url().to_owned(),
45 }
46 }
47
48 pub fn byte_length(&self) -> std::io::Result<u64> {
53 match self {
54 Self::File(path) => std::fs::metadata(path).map(|metadata| metadata.len()),
55 Self::Remote(object) => Ok(object.len()),
56 }
57 }
58}
59
60impl std::fmt::Debug for SourceLocation {
61 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 formatter.write_str(&self.shown())
63 }
64}
65
66impl PartialEq for SourceLocation {
67 fn eq(&self, other: &Self) -> bool {
68 match (self, other) {
69 (Self::File(a), Self::File(b)) => a == b,
70 (Self::Remote(a), Self::Remote(b)) => Arc::ptr_eq(a, b),
71 _ => false,
72 }
73 }
74}
75
76impl Eq for SourceLocation {}
77
78impl Hash for SourceLocation {
79 fn hash<H: Hasher>(&self, state: &mut H) {
80 match self {
81 Self::File(path) => path.hash(state),
82 Self::Remote(object) => std::ptr::hash(Arc::as_ptr(object), state),
83 }
84 }
85}
86
87impl From<PathBuf> for SourceLocation {
88 fn from(path: PathBuf) -> Self {
89 Self::File(path)
90 }
91}
92
93impl From<&Path> for SourceLocation {
94 fn from(path: &Path) -> Self {
95 Self::File(path.to_path_buf())
96 }
97}
98
99impl From<&PathBuf> for SourceLocation {
100 fn from(path: &PathBuf) -> Self {
101 Self::File(path.clone())
102 }
103}
104
105impl From<&SourceLocation> for SourceLocation {
106 fn from(location: &SourceLocation) -> Self {
107 location.clone()
108 }
109}
110
111impl From<&RemoteError> for SourceFailure {
112 fn from(error: &RemoteError) -> Self {
113 Self {
114 code: error.failure.code(),
115 message: error.message.clone(),
116 url: error.url.clone(),
117 status: error.status,
118 retry_after_ms: match error.failure {
119 RemoteFailure::Busy { retry_after_ms } => Some(retry_after_ms),
120 _ => None,
121 },
122 policy: match error.failure {
123 RemoteFailure::Refused(policy) => Some(policy.name()),
124 _ => None,
125 },
126 }
127 }
128}
129
130#[must_use]
133pub fn decode_error(error: &std::io::Error) -> Option<DecodeError> {
134 let remote = RemoteError::of(error)?;
135 Some(if remote.failure == RemoteFailure::Cancelled {
136 DecodeError::Cancelled
137 } else {
138 DecodeError::Source(SourceFailure::from(remote))
139 })
140}