mirror of
https://github.com/mat-1/azalea.git
synced 2024-09-19 22:52:32 +00:00
add files
This commit is contained in:
parent
dd66441e72
commit
fa471dd904
1 changed files with 58 additions and 0 deletions
58
azalea-core/src/resource_location.rs
Normal file
58
azalea-core/src/resource_location.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
//! A resource, like minecraft:stone
|
||||
|
||||
pub struct ResourceLocation<'a> {
|
||||
pub namespace: &'a str,
|
||||
pub path: &'a str,
|
||||
}
|
||||
|
||||
static DEFAULT_NAMESPACE: &str = "minecraft";
|
||||
// static REALMS_NAMESPACE: &str = "realms";
|
||||
|
||||
impl<'a> ResourceLocation<'a> {
|
||||
pub fn new(resource_string: &str) -> Result<ResourceLocation, String> {
|
||||
let sep_byte_position_option = resource_string.chars().position(|c| c == ':');
|
||||
let (namespace, path) = if let Some(sep_byte_position) = sep_byte_position_option {
|
||||
if sep_byte_position == 0 {
|
||||
(DEFAULT_NAMESPACE, &resource_string[1..])
|
||||
} else {
|
||||
(
|
||||
&resource_string[..sep_byte_position],
|
||||
&resource_string[sep_byte_position + 1..],
|
||||
)
|
||||
}
|
||||
} else {
|
||||
(DEFAULT_NAMESPACE, resource_string)
|
||||
};
|
||||
Ok(ResourceLocation { namespace, path })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn basic_resource_location() {
|
||||
let r = ResourceLocation::new("abcdef:ghijkl").unwrap();
|
||||
assert_eq!(r.namespace, "abcdef");
|
||||
assert_eq!(r.path, "ghijkl");
|
||||
}
|
||||
#[test]
|
||||
fn no_namespace() {
|
||||
let r = ResourceLocation::new("azalea").unwrap();
|
||||
assert_eq!(r.namespace, "minecraft");
|
||||
assert_eq!(r.path, "azalea");
|
||||
}
|
||||
#[test]
|
||||
fn colon_start() {
|
||||
let r = ResourceLocation::new(":azalea").unwrap();
|
||||
assert_eq!(r.namespace, "minecraft");
|
||||
assert_eq!(r.path, "azalea");
|
||||
}
|
||||
#[test]
|
||||
fn colon_end() {
|
||||
let r = ResourceLocation::new("azalea:").unwrap();
|
||||
assert_eq!(r.namespace, "azalea");
|
||||
assert_eq!(r.path, "");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue