-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathproto_path.rs
222 lines (190 loc) · 5.73 KB
/
proto_path.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use std::borrow::Cow;
use std::fmt::{Debug, Formatter};
use std::path::{Path, PathBuf, MAIN_SEPARATOR_STR};
use bevy::asset::{AssetPath, HandleId};
use path_clean::PathClean;
use crate::path::{PathError, ProtoPathContext};
/// A wrapper around an [`AssetPath`] that represents a path to a [prototype].
///
/// [prototype]: crate::proto::Prototypical
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct ProtoPath(AssetPath<'static>);
impl ProtoPath {
/// Creates a new [`ProtoPath`] from the given path and a [path context].
///
/// This supports the following path types:
/// * "Absolute" Asset Paths
/// * `/prototypes/Template.prototype.ron`
/// * Relative Paths
/// * `./Template.prototype.ron`
/// * `Template.prototype.ron`
/// * Extensionless Relative Paths
/// * `Template`
///
/// Note that "Extensionless Relative Paths" require that the extension is configured
/// in the respective [`Config`].
/// Its extension will be chosen based on the first match with the path context's
/// [base path].
///
/// [path context]: ProtoPathContext
/// [`Config`]: crate::proto::Config
/// [base path]: ProtoPathContext::base_path
pub fn new<P: AsRef<Path>>(path: P, ctx: &dyn ProtoPathContext) -> Result<Self, PathError> {
// === Paths Types ===
// 1. Absolute Asset Paths
// a. "/prototypes/Template.prototype.ron"
// - Note: The leading "/" will need to be stripped to be valid
// 2. Relative Paths
// a. "./Template.prototype.ron"
// 3. Name-only Relative Paths
// a. "Template.prototype.ron"
// 4. Extensionless Name-only Relative Paths
// a. "Template"
let path = path.as_ref();
let base_path = ctx.base_path();
// 1
if path.has_root() {
return Ok(ProtoPath::from(
path.strip_prefix(MAIN_SEPARATOR_STR)
.map_err(|_| PathError::MalformedPath(path.to_path_buf()))?
.to_path_buf(),
));
}
let rel_path = base_path
.parent()
.ok_or_else(|| PathError::InvalidBase(base_path.to_path_buf()))?
.join(path)
.clean();
let io = ctx.asset_io();
// 2 & 3
if io.is_file(rel_path.as_path()) {
return Ok(ProtoPath::from(rel_path));
}
// 4
for extension in ctx.extensions() {
let ext = extension.strip_prefix('.').unwrap_or(extension);
if base_path.to_string_lossy().ends_with(&format!(".{ext}")) {
let rel_path = rel_path.with_extension(ext);
return if io.is_file(rel_path.as_path()) {
Ok(ProtoPath::from(rel_path))
} else {
Err(PathError::DoesNotExist(rel_path))
};
}
}
Err(PathError::InvalidExtension(base_path.to_path_buf()))
}
/// Get the underlying [`AssetPath`].
pub fn asset_path(&self) -> &AssetPath<'static> {
&self.0
}
/// Get the [`Path`] of the underlying [`AssetPath`].
pub fn path(&self) -> &Path {
self.0.path()
}
/// Get the [label] of the underlying [`AssetPath`].
///
/// [label]: AssetPath::label
pub fn label(&self) -> Option<&str> {
self.0.label()
}
}
impl Debug for ProtoPath {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(label) = self.0.label() {
write!(f, "{:?}#{}", self.0.path(), label)
} else {
write!(f, "{:?}", self.0.path())
}
}
}
impl PartialEq<AssetPath<'_>> for ProtoPath {
fn eq(&self, other: &AssetPath) -> bool {
&self.0 == other
}
}
impl PartialEq<&AssetPath<'_>> for ProtoPath {
fn eq(&self, other: &&AssetPath) -> bool {
&&self.0 == other
}
}
impl From<AssetPath<'_>> for ProtoPath {
fn from(value: AssetPath<'_>) -> Self {
Self(value.to_owned())
}
}
impl From<&AssetPath<'_>> for ProtoPath {
fn from(value: &AssetPath<'_>) -> Self {
Self(value.to_owned())
}
}
impl From<PathBuf> for ProtoPath {
fn from(value: PathBuf) -> Self {
Self(AssetPath::from(value))
}
}
impl From<&PathBuf> for ProtoPath {
fn from(value: &PathBuf) -> Self {
Self(AssetPath::from(value.clone()))
}
}
impl From<&Path> for ProtoPath {
fn from(value: &Path) -> Self {
Self(AssetPath::from(value.to_path_buf()))
}
}
impl From<String> for ProtoPath {
fn from(value: String) -> Self {
Self(AssetPath::from(value))
}
}
impl From<&String> for ProtoPath {
fn from(value: &String) -> Self {
Self(AssetPath::from(value.clone()))
}
}
impl From<&str> for ProtoPath {
fn from(value: &str) -> Self {
Self(AssetPath::from(value.to_owned()))
}
}
impl From<ProtoPath> for AssetPath<'static> {
fn from(value: ProtoPath) -> Self {
value.0
}
}
impl From<&ProtoPath> for AssetPath<'static> {
fn from(value: &ProtoPath) -> Self {
value.0.clone()
}
}
impl<'a> From<ProtoPath> for Cow<'a, ProtoPath> {
fn from(value: ProtoPath) -> Self {
Cow::Owned(value)
}
}
impl<'a> From<&'a ProtoPath> for Cow<'a, ProtoPath> {
fn from(value: &'a ProtoPath) -> Self {
Cow::Borrowed(value)
}
}
impl From<ProtoPath> for HandleId {
fn from(value: ProtoPath) -> Self {
value.0.get_id().into()
}
}
impl From<&ProtoPath> for HandleId {
fn from(value: &ProtoPath) -> Self {
value.0.get_id().into()
}
}
impl<'a> AsRef<AssetPath<'a>> for ProtoPath {
fn as_ref(&self) -> &AssetPath<'a> {
&self.0
}
}
impl AsRef<Path> for ProtoPath {
fn as_ref(&self) -> &Path {
self.0.path()
}
}