aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-16 20:23:58 +0000
committermat <github@matdoes.dev>2021-12-16 20:23:58 +0000
commitfa471dd90484136230e2b7315f2d5ed5b3e5a0c8 (patch)
treec73d27ce668b365d7d445089c516319b243143d4
parentdd66441e726754886dc9a9e012fc7a9c338da1e7 (diff)
downloadazalea-drasl-fa471dd90484136230e2b7315f2d5ed5b3e5a0c8.tar.xz
add files
-rw-r--r--azalea-core/src/resource_location.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs
new file mode 100644
index 00000000..df706e7a
--- /dev/null
+++ b/azalea-core/src/resource_location.rs
@@ -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, "");
+ }
+}