asset_resolver/
resolver.rs

1//! This module contains the main code revolving around the resolver mechanism
2
3use std::path::{PathBuf, Path};
4
5/// The asset resolver trait. This should be implemented by any custom resolvers
6pub trait AssetResolver {
7    /// Resolve a path to an asset.
8    ///
9    /// The incoming path could be in any format. If it can be 
10    /// resolved by the implementer, it should return `Some(PathBuf)`. 
11    /// Otherwise, it should return `None`.
12    ///
13    /// Additional mechanisms may handle chaining resolvers to handle multiple 
14    /// path formats, so don't worry about catching every case in one resolver.
15    fn resolve(&self, path: &str) -> Option<PathBuf>;
16}
17
18
19/// The default resolver. This simply parses path strings using `Path::new()`
20pub struct DefaultResolver;
21
22impl AssetResolver for DefaultResolver {
23
24    /// Directly resolves paths using Rust's built-in 
25    /// [`Path`](https://doc.rust-lang.org/std/path/struct.Path.html) type.
26    /// 
27    /// # Example
28    /// ```
29    /// # use asset_resolver::{AssetResolver, DefaultResolver};
30    /// let resolver = DefaultResolver;
31    /// let path = resolver.resolve("/home/user/assets/test.png").unwrap();
32    /// assert_eq!(path.to_str().unwrap(), "/home/user/assets/test.png");
33    /// ```
34    fn resolve(&self, path: &str) -> Option<PathBuf> {
35        Path::new(path).to_owned().into()
36    }
37}
38
39/// A resolver that always returns `None`.
40pub struct NullResolver;
41
42impl AssetResolver for NullResolver {
43
44    /// Does not resolve paths. Just returns `None`.
45    fn resolve(&self, _path: &str) -> Option<PathBuf> {
46        None
47    }
48}