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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::{borrow::Cow, fmt, ops::Deref, path::PathBuf};

pub const MINECRAFT_NAMESPACE: &str = "minecraft";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ResourceKind {
    /// Resources located in the `assets/` directory.
    Assets,

    /// Resource located in the `data/` directory.
    Data,
}

/// Represents a Minecraft [resource location].
///
/// Resource locations are namespaced identifiers referencing blocks, items,
/// entity types, recipes, functions, advancements, tags, and various other
/// objects in vanilla Minecraft.
///
/// To understand why this type has a lifetime parameter, see the
/// [`ResourceIdentifier`] documentation.
///
/// [resource location]: <https://minecraft.fandom.com/wiki/Resource_location>
/// [`ResourceIdentifier`]: ResourceIdentifier#borrowing--ownership
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResourceLocation<'a> {
    /// Represents the location of a file in `assets/<namespace>/blockstates/`.
    BlockStates(ResourceIdentifier<'a>),

    /// Represents the location of a file in `assets/<namespace>/models/block/`.
    BlockModel(ModelIdentifier<'a>),

    /// Represents the location of a file in `assets/<namespace>/models/item/`.
    ItemModel(ModelIdentifier<'a>),
}

impl<'a> ResourceLocation<'a> {
    /// Returns a reference to the underlying [`ResourceIdentifier`].
    pub(crate) fn id(&self) -> &ResourceIdentifier<'a> {
        match self {
            Self::BlockModel(id) | Self::ItemModel(id) => id.deref(),
            Self::BlockStates(id) => id,
        }
    }

    /// Returns the type of resource that this location references.
    pub fn kind(&self) -> ResourceKind {
        match self {
            Self::BlockStates(_) | Self::BlockModel(_) | Self::ItemModel(_) => ResourceKind::Assets,
        }
    }

    /// Returns the namespace of the resource referenced by this location.
    pub fn namespace(&self) -> &str {
        self.id().namespace()
    }

    /// Returns the name / terminating "path" of the resource referenced by this
    /// location.
    ///
    /// For [`BlockModel`] or [`ItemModel`] variants, the name will **not**
    /// include any leading prefix like `block/` or `item/`. See the
    /// [`ModelIdentifier`] documentation for more information.
    ///
    /// [`BlockModel`]: Self::BlockModel
    /// [`ItemModel`]: Self::ItemModel
    pub fn name(&self) -> &str {
        match self {
            Self::BlockModel(id) | Self::ItemModel(id) => id.model_name(),
            _ => self.id().path(),
        }
    }

    /// Returns the name of the model specified by a [`BlockModel`] or
    /// [`ItemModel`], or `None` if `self` is not one of those variants.
    ///
    /// See the [`ModelIdentifier`] documentation for more information.
    ///
    /// [`BlockModel`]: Self::BlockModel
    /// [`ItemModel`]: Self::ItemModel
    pub fn model_name(&self) -> Option<&str> {
        match self {
            Self::BlockModel(id) | Self::ItemModel(id) => Some(id.model_name()),

            _ => None,
        }
    }

    /// Returns the path relative to `{assets,data}/<namespace>/` at which the
    /// resource's file can be found.
    pub fn directory(&self) -> &'static str {
        match self {
            Self::BlockStates(_) => "blockstates",
            Self::BlockModel(_) => "models/block",
            Self::ItemModel(_) => "models/item",
        }
    }

    /// Returns the file extension (e.g., `json`) used for this resource's file.
    pub fn extension(&self) -> &'static str {
        match self {
            Self::BlockStates(_) | Self::BlockModel(_) | Self::ItemModel(_) => "json",
        }
    }

    /// Returns a file path relative to the asset root at which the resource can
    /// be found.
    pub fn path(&self) -> PathBuf {
        let mut path = match self.kind() {
            ResourceKind::Assets => PathBuf::from("assets"),
            ResourceKind::Data => PathBuf::from("data"),
        };
        path.push(self.namespace());
        path.push(self.directory());
        path.push(self.name());

        path.with_extension(self.extension())
    }
}

/// A namespaced identifier for an undetermined type of resource.
///
/// A valid resource location has a format of `"namespace:path"`. If the
/// `namespace` portion is left out, then `"minecraft"` is the implied
/// namespace.
///
/// # Borrowing / Ownership
///
/// To avoid cloning / [`String`] construction when not necessary, this type can
/// either borrow or take ownership of the underlying string.
///
/// By default, no copying or allocating is done. You must call
/// [`into_owned()`][Self::into_owned] to get an owned identifier.
#[derive(Clone)]
pub struct ResourceIdentifier<'a>(Cow<'a, str>);

impl<'a> ResourceIdentifier<'a> {
    /// Returns this identifier's underlying string representation.
    ///
    /// # Example
    ///
    /// ```
    /// # use minecraft_assets::api::*;
    /// let ident = ResourceIdentifier::from("stone");
    /// assert_eq!(ident.as_str(), "stone");
    ///
    /// let ident = ResourceIdentifier::from("minecraft:dirt");
    /// assert_eq!(ident.as_str(), "minecraft:dirt");
    /// ```
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Returns whether or not this resource location includes an explicit
    /// namespace.
    ///
    /// # Example
    ///
    /// ```
    /// # use minecraft_assets::api::*;
    /// let id = ResourceIdentifier::from("foo:bar");
    /// assert!(id.has_namespace());
    ///
    /// let id = ResourceIdentifier::from("bar");
    /// assert!(!id.has_namespace());
    /// ```
    pub fn has_namespace(&self) -> bool {
        self.colon_position().is_some()
    }

    /// Returns the namespace portion of the resource location.
    ///
    /// # Example
    ///
    /// ```
    /// # use minecraft_assets::api::*;
    /// let id = ResourceIdentifier::from("foo:bar");
    /// assert_eq!(id.namespace(), "foo");
    ///
    /// let id = ResourceIdentifier::from("bar");
    /// assert_eq!(id.namespace(), "minecraft");
    ///
    /// let id = ResourceIdentifier::from(":bar");
    /// assert_eq!(id.namespace(), "");
    /// ```
    pub fn namespace(&self) -> &str {
        self.colon_position()
            .map(|index| &self.0[..index])
            .unwrap_or_else(|| MINECRAFT_NAMESPACE)
    }

    /// Returns the path portion of the resource location.
    ///
    /// # Example
    ///
    /// ```
    /// # use minecraft_assets::api::*;
    /// let id = ResourceIdentifier::from("foo:bar");
    /// assert_eq!(id.path(), "bar");
    ///
    /// let id = ResourceIdentifier::from("bar");
    /// assert_eq!(id.path(), "bar");
    ///
    /// let id = ResourceIdentifier::from("foo:");
    /// assert_eq!(id.path(), "");
    /// ```
    pub fn path(&self) -> &str {
        self.colon_position()
            .map(|index| &self.0[index + 1..])
            .unwrap_or_else(|| &self.0[..])
    }

    /// Returns a new identifier with a canonical representation (i.e.,
    /// containing an explicit namespace).
    ///
    /// This will involve allocating a new [`String`] if `self` does not already
    /// contain an explicit namespace.
    ///
    /// # Examples
    ///
    /// Prepends the default namespace when one is not present:
    ///
    /// ```
    /// # use minecraft_assets::api::*;
    /// let ident = ResourceIdentifier::from("stone");
    /// let canonical = ident.to_canonical();
    ///
    /// assert_eq!(canonical.as_str(), "minecraft:stone");
    /// ```
    ///
    /// Performs a shallow copy when a namespace is already present:
    ///
    /// ```
    /// # use minecraft_assets::api::*;
    /// let ident = ResourceIdentifier::from("foo:bar");
    /// let canonical = ident.to_canonical();
    ///
    /// assert_eq!(canonical.as_str(), "foo:bar");
    ///
    /// // Prove that it was a cheap copy.
    /// assert_eq!(
    ///     ident.as_str().as_ptr() as usize,
    ///     canonical.as_str().as_ptr() as usize,
    /// );
    /// ```
    pub fn to_canonical(&self) -> ResourceIdentifier<'a> {
        if self.has_namespace() {
            self.clone()
        } else {
            let canonical = format!("{}:{}", self.namespace(), self.as_str());
            ResourceIdentifier(Cow::Owned(canonical))
        }
    }

    /// Returns a new [`ResourceIdentifier`] that owns the underlying string.
    ///
    /// This is useful for, e.g., storing the identifier in a data structure or
    /// passing it to another thread.
    ///
    /// By default, all `ResourceIdentifier`s borrow the string they are
    /// constructed with, so no copying will occur unless you call this
    /// function.
    ///
    /// # Examples
    ///
    /// Constructing an identifier using [`From`] simply borrows the data:
    ///
    /// ```compile_fail
    /// # use minecraft_assets::api::*;
    /// let string = String::from("my:ident");
    ///
    /// let ident = ResourceIdentifier::from(&string);
    ///
    /// // Identifier borrows data from `string`, cannot be sent across threads.
    /// std::thread::spawn(move || println!("{}", ident));
    /// ```
    ///
    /// Calling [`into_owned()`][Self::into_owned] on the identifier allows it
    /// to be sent to the thread:
    ///
    /// ```
    /// # use minecraft_assets::api::*;
    /// let string = String::from("my:ident");
    ///
    /// let ident = ResourceIdentifier::from(&string);
    /// let ident = ident.into_owned();
    ///
    /// std::thread::spawn(move || println!("{}", ident));
    /// ```
    pub fn into_owned(&self) -> ResourceIdentifier<'static> {
        let string = self.0.deref().to_owned();
        ResourceIdentifier(Cow::Owned(string))
    }

    fn colon_position(&self) -> Option<usize> {
        self.0.chars().position(|c| c == ':')
    }
}

impl<'a, S> From<&'a S> for ResourceIdentifier<'a>
where
    S: AsRef<str> + ?Sized,
{
    fn from(source: &'a S) -> Self {
        Self(Cow::Borrowed(source.as_ref()))
    }
}

impl<'a> PartialEq for ResourceIdentifier<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl<'a> Eq for ResourceIdentifier<'a> {}

impl<'a> fmt::Debug for ResourceIdentifier<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ResId").field(&self.as_str()).finish()
    }
}

impl<'a> fmt::Display for ResourceIdentifier<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_canonical().as_str())
    }
}

/// A namespaced identifier for a block or item model.
///
/// Prior to 1.13, model identifiers found in
/// `assets/<namespace>/blockstates/*.json` did not include a prefix like
/// `block/` or `ident/` to disambiguate between different types of models.
///
/// Because of this, the `minecraft-assets` API forces the user to always
/// specify which type of model they are trying to load (note the existence of
/// both [`BlockModel`] and [`ItemModel`] variants in [`ResourceLocation`]).
/// This way, the API will work with versions prior to 1.13.
///
/// This struct exists mostly for internal convenience so we can scrap the
/// leading `block/` or `item/` if it is included.
///
/// [`BlockModel`]: ResourceLocation::BlockModel
/// [`ItemModel`]: ResourceLocation::ItemModel
#[derive(Debug, Clone)]
pub struct ModelIdentifier<'a>(ResourceIdentifier<'a>);

impl<'a> ModelIdentifier<'a> {
    /// Returns the name of the model, stripping the leading path component if
    /// there is one.
    ///
    /// # Example
    ///
    /// ```
    /// # use minecraft_assets::api::*;
    /// let ident = ModelIdentifier::from("stone");
    /// assert_eq!(ident.model_name(), "stone");
    /// let ident = ModelIdentifier::from("foo:stone");
    /// assert_eq!(ident.model_name(), "stone");
    ///
    /// let ident = ModelIdentifier::from("block/oak_planks");
    /// assert_eq!(ident.model_name(), "oak_planks");
    /// let ident = ModelIdentifier::from("foo:block/oak_planks");
    /// assert_eq!(ident.model_name(), "oak_planks");
    ///
    /// let ident = ModelIdentifier::from("item/diamond_hoe");
    /// assert_eq!(ident.model_name(), "diamond_hoe");
    /// let ident = ModelIdentifier::from("foo:item/diamond_hoe");
    /// assert_eq!(ident.model_name(), "diamond_hoe");
    ///
    /// ```
    pub fn model_name(&self) -> &str {
        self.slash_position()
            .map(|index| &self.0.path()[index + 1..])
            .unwrap_or_else(|| self.0.path())
    }

    fn slash_position(&self) -> Option<usize> {
        self.0.path().chars().position(|c| c == '/')
    }
}

impl<'a, S> From<S> for ModelIdentifier<'a>
where
    S: Into<ResourceIdentifier<'a>>,
{
    fn from(source: S) -> Self {
        Self(source.into())
    }
}

impl<'a> Deref for ModelIdentifier<'a> {
    type Target = ResourceIdentifier<'a>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a> PartialEq for ModelIdentifier<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.model_name() == other.model_name()
    }
}

impl<'a> Eq for ModelIdentifier<'a> {}