]> git.huck.website - metaforge.git/commitdiff
fixed: directly calling arrays by name rather than class
authorHuck Boles <huck@huck.website>
Wed, 24 May 2023 16:33:56 +0000 (11:33 -0500)
committerHuck Boles <huck@huck.website>
Wed, 24 May 2023 16:33:56 +0000 (11:33 -0500)
files/test_site/pattern/test/list.meta [new file with mode: 0644]
files/test_site/source/unit_tests/expand/direct_call.meta [new file with mode: 0644]
src/metafile/file/arrays.rs
src/metafile/file/attributes.rs
src/metafile/tests.rs
src/tests.rs

diff --git a/files/test_site/pattern/test/list.meta b/files/test_site/pattern/test/list.meta
new file mode 100644 (file)
index 0000000..dc7d08f
--- /dev/null
@@ -0,0 +1 @@
+@{item}
diff --git a/files/test_site/source/unit_tests/expand/direct_call.meta b/files/test_site/source/unit_tests/expand/direct_call.meta
new file mode 100644 (file)
index 0000000..eef563c
--- /dev/null
@@ -0,0 +1,3 @@
+@{ test.list.item = ['a','b','c','d'] }
+
+&{test.list}
index ab130efab924356c0388ec1f32378bd1b8e21f54..122e8b1741415139d343714bef98ada2a1d3ddd0 100644 (file)
@@ -22,12 +22,18 @@ impl<'a> MetaFile<'a> {
             // make a hash map of [keys in source] -> [defined arrays]
             .map(|key| {
                 // concat array to pattern name to get key in HashMap
+                let class = self.class().unwrap_or_default();
+                let class_key = Scope::Local(class + "." + key);
                 let name = self.name().unwrap_or_default();
-                let long_key = name + "." + key;
+                let name_key = Scope::Local(name + "." + key);
 
-                let value = if let Some(val) = self.arrays.get(&Scope::into_global(&long_key)) {
+                let value = if let Some(val) = self.arrays.get(&name_key) {
                     &val[..]
-                } else if let Some(val) = self.arrays.get(&Scope::into_local(&long_key)) {
+                } else if let Some(val) = self.arrays.get(&name_key.to_global()) {
+                    &val[..]
+                } else if let Some(val) = self.arrays.get(&class_key) {
+                    &val[..]
+                } else if let Some(val) = self.arrays.get(&class_key.to_global()) {
                     &val[..]
                 } else if let Some(val) = self.arrays.get(&Scope::into_global(key)) {
                     &val[..]
index 2fecd5eb5569d87b534de711db434a861c5d8385..0f2c6506a8079aaaeede1b867b41f82a16d44d3d 100644 (file)
@@ -11,35 +11,41 @@ impl<'a> MetaFile<'a> {
         Ok(path)
     }
 
+    pub fn class(&self) -> Result<String> {
+        // only care about classes in the pattern dir
+        self.path
+            .strip_prefix(&self.opts.pattern)?
+            .parent()
+            .map(|s| s.to_string_lossy().to_string().replace('/', "."))
+            .ok_or(
+                MetaError::Name {
+                    file: self.path.to_string_lossy().to_string(),
+                }
+                .into(),
+            )
+    }
+
     pub fn name(&self) -> Result<String> {
-        if self.path.starts_with(&self.opts.source) {
-            // in source dir, we want the file name without the '.meta' extension
-            let name: String = self
-                .path
-                .strip_prefix(&self.opts.source)?
-                .components()
-                .map(|x| {
-                    x.as_os_str()
-                        .to_string_lossy()
-                        .to_string()
-                        .replace(".meta", "")
-                })
-                .collect::<Vec<String>>()
-                .join(".");
-            Ok(name)
-        } else if self.path.starts_with(&self.opts.pattern) {
-            // in pattern dir, we want the parent dir
-            let name = self.path.strip_prefix(&self.opts.pattern)?;
-            let name = name
-                .parent()
-                .map(|s| s.to_string_lossy().to_string().replace('/', "."))
-                .unwrap_or_default();
-            Ok(name)
+        let path = if self.path.starts_with(&self.opts.pattern) {
+            self.path.strip_prefix(&self.opts.pattern)?
+        } else if self.path.starts_with(&self.opts.source) {
+            self.path.strip_prefix(&self.opts.source)?
         } else {
-            Err(MetaError::Name {
+            return Err(MetaError::Name {
                 file: self.path.to_string_lossy().to_string(),
             }
-            .into())
-        }
+            .into());
+        };
+
+        Ok(path
+            .components()
+            .map(|x| {
+                x.as_os_str()
+                    .to_string_lossy()
+                    .to_string()
+                    .replace(".meta", "")
+            })
+            .collect::<Vec<String>>()
+            .join("."))
     }
 }
index 3ad3af8e87ee7cd2fe2b1962977042f9f0ca227b..014f0c5b9ed0c1bcf9dd02805a65f0bd45d81c90 100644 (file)
@@ -24,8 +24,10 @@ fn test_name() -> Result<()> {
     pat2.path = pat2_path;
 
     assert_eq!(src.name()?, "test.file");
-    assert_eq!(pat1.name()?, "base");
-    assert_eq!(pat2.name()?, "test.class");
+    assert_eq!(pat1.name()?, "base.test");
+    assert_eq!(pat2.name()?, "test.class.file");
+    assert_eq!(pat1.class()?, "base");
+    assert_eq!(pat2.class()?, "test.class");
 
     Ok(())
 }
index 66da952e02d85f60a1fe2dd50c4686e396a8d6e3..26fb1779ff4480f8ec2972b1d87f5cc180339b2f 100644 (file)
@@ -109,6 +109,12 @@ unit_test!(
     "<html>\n<p>GOOD</p>\n</html>\n"
 );
 
+unit_test!(
+    direct_call,
+    "expand/direct_call",
+    "<html>\n<p>abcd</p>\n</html>\n"
+);
+
 panic_test!(ignore, "ignore.meta", "");
 
 #[test]