From: Huck Boles Date: Wed, 24 May 2023 16:33:56 +0000 (-0500) Subject: fixed: directly calling arrays by name rather than class X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=95f7404623cba541481d2584a3d38c609467c336;p=metaforge.git fixed: directly calling arrays by name rather than class --- diff --git a/files/test_site/pattern/test/list.meta b/files/test_site/pattern/test/list.meta new file mode 100644 index 0000000..dc7d08f --- /dev/null +++ b/files/test_site/pattern/test/list.meta @@ -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 index 0000000..eef563c --- /dev/null +++ b/files/test_site/source/unit_tests/expand/direct_call.meta @@ -0,0 +1,3 @@ +@{ test.list.item = ['a','b','c','d'] } + +&{test.list} diff --git a/src/metafile/file/arrays.rs b/src/metafile/file/arrays.rs index ab130ef..122e8b1 100644 --- a/src/metafile/file/arrays.rs +++ b/src/metafile/file/arrays.rs @@ -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[..] diff --git a/src/metafile/file/attributes.rs b/src/metafile/file/attributes.rs index 2fecd5e..0f2c650 100644 --- a/src/metafile/file/attributes.rs +++ b/src/metafile/file/attributes.rs @@ -11,35 +11,41 @@ impl<'a> MetaFile<'a> { Ok(path) } + pub fn class(&self) -> Result { + // 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 { - 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::>() - .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::>() + .join(".")) } } diff --git a/src/metafile/tests.rs b/src/metafile/tests.rs index 3ad3af8..014f0c5 100644 --- a/src/metafile/tests.rs +++ b/src/metafile/tests.rs @@ -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(()) } diff --git a/src/tests.rs b/src/tests.rs index 66da952..26fb177 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -109,6 +109,12 @@ unit_test!( "\n

GOOD

\n\n" ); +unit_test!( + direct_call, + "expand/direct_call", + "\n

abcd

\n\n" +); + panic_test!(ignore, "ignore.meta", ""); #[test]