From 8616bb938c97ce1e48d47a491a3748855c3479bb Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Wed, 31 May 2023 14:22:47 -0500 Subject: [PATCH] refactored: scope to use display + more intuitive api --- src/metafile.rs | 21 +++++++++++++-------- src/metafile/file.rs | 4 ++-- src/metafile/file/arrays.rs | 8 ++++---- src/metafile/file/patterns.rs | 6 +++--- src/metafile/scope.rs | 20 ++++++++++++-------- src/parser/array.rs | 4 ++-- src/parser/def_block.rs | 4 ++-- 7 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/metafile.rs b/src/metafile.rs index c23fa8b..54f6879 100644 --- a/src/metafile.rs +++ b/src/metafile.rs @@ -11,6 +11,8 @@ pub use scope::*; #[cfg(test)] mod tests; +use std::fmt::Display; + #[derive(Debug, Clone, PartialEq)] pub enum Src { Str(String), @@ -20,27 +22,30 @@ pub enum Src { } impl Src { - pub fn to_var(var: impl ToString) -> Self { + pub fn to_var(var: impl Display) -> Self { Src::Var(var.to_string()) } - pub fn to_arr(arr: impl ToString) -> Self { + pub fn to_arr(arr: impl Display) -> Self { Src::Arr(arr.to_string()) } - pub fn to_pat(pat: impl ToString) -> Self { + pub fn to_pat(pat: impl Display) -> Self { Src::Pat(pat.to_string()) } - pub fn to_str(str: impl ToString) -> Self { + pub fn to_str(str: impl Display) -> Self { + println!("{}", str.to_string()); Src::Str(str.to_string()) } } -impl ToString for Src { - fn to_string(&self) -> String { - match self { +impl Display for Src { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let str = match self { Src::Var(x) | Src::Arr(x) | Src::Pat(x) | Src::Str(x) => x.to_string(), - } + }; + + write!(f, "{str}") } } diff --git a/src/metafile/file.rs b/src/metafile/file.rs index c89c277..a91843c 100644 --- a/src/metafile/file.rs +++ b/src/metafile/file.rs @@ -72,10 +72,10 @@ impl<'a> MetaFile<'a> { })?; base.merge(self); - base.patterns.insert(Scope::into_global("SOURCE"), html); + base.patterns.insert(Scope::create_global("SOURCE"), html); let mut base_path = self.opts.pattern.join("base").join( self.patterns - .get(&Scope::into_global("base")) + .get(&Scope::create_global("base")) .unwrap_or(&"default".into()), ); diff --git a/src/metafile/file/arrays.rs b/src/metafile/file/arrays.rs index 122e8b1..1b38c0f 100644 --- a/src/metafile/file/arrays.rs +++ b/src/metafile/file/arrays.rs @@ -29,15 +29,15 @@ impl<'a> MetaFile<'a> { let value = if let Some(val) = self.arrays.get(&name_key) { &val[..] - } else if let Some(val) = self.arrays.get(&name_key.to_global()) { + } else if let Some(val) = self.arrays.get(&name_key.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()) { + } else if let Some(val) = self.arrays.get(&class_key.global()) { &val[..] - } else if let Some(val) = self.arrays.get(&Scope::into_global(key)) { + } else if let Some(val) = self.arrays.get(&Scope::create_global(key)) { &val[..] - } else if let Some(val) = self.arrays.get(&Scope::into_local(key)) { + } else if let Some(val) = self.arrays.get(&Scope::create_local(key)) { &val[..] } else if self.opts.undefined { panic!( diff --git a/src/metafile/file/patterns.rs b/src/metafile/file/patterns.rs index 1df5844..d019484 100644 --- a/src/metafile/file/patterns.rs +++ b/src/metafile/file/patterns.rs @@ -5,16 +5,16 @@ impl<'a> MetaFile<'a> { log!(self.opts, format!("expanding {key}"), 2); // SOURCE is already expanded in the initial construct() call if key == "SOURCE" { - if let Some(source) = self.patterns.get(&Scope::into_global("SOURCE")) { + if let Some(source) = self.patterns.get(&Scope::create_global("SOURCE")) { return Ok(source.to_string()); } else { return Ok(String::new()); } } - let mut filename = if let Some(name) = self.patterns.get(&Scope::into_local(key)) { + let mut filename = if let Some(name) = self.patterns.get(&Scope::create_local(key)) { Ok(name.to_string()) - } else if let Some(name) = self.patterns.get(&Scope::into_global(key)) { + } else if let Some(name) = self.patterns.get(&Scope::create_global(key)) { Ok(name.to_string()) } else if self .opts diff --git a/src/metafile/scope.rs b/src/metafile/scope.rs index 8e23b1d..c1187c7 100644 --- a/src/metafile/scope.rs +++ b/src/metafile/scope.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + #[derive(Debug, Clone, Eq, Hash, PartialEq)] pub enum Scope { Local(String), @@ -5,11 +7,11 @@ pub enum Scope { } impl Scope { - pub fn into_local(str: impl ToString) -> Scope { + pub fn create_local(str: impl Display) -> Scope { Scope::Local(str.to_string()) } - pub fn into_global(str: impl ToString) -> Scope { + pub fn create_global(str: impl ToString) -> Scope { Scope::Global(str.to_string()) } @@ -27,19 +29,21 @@ impl Scope { } } - pub fn to_local(&self) -> Scope { + pub fn local(&self) -> Scope { Scope::Local(self.to_string()) } - pub fn to_global(&self) -> Scope { + pub fn global(&self) -> Scope { Scope::Global(self.to_string()) } } -impl ToString for Scope { - fn to_string(&self) -> String { - match self { +impl Display for Scope { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let str = match self { Scope::Local(x) | Scope::Global(x) => x.to_string(), - } + }; + + write!(f, "{str}") } } diff --git a/src/parser/array.rs b/src/parser/array.rs index 8358f58..30b2979 100644 --- a/src/parser/array.rs +++ b/src/parser/array.rs @@ -40,9 +40,9 @@ fn parse_assign_array(pair: Pair) -> Result<(Scope, Vec)> { } if global { - Ok((Scope::into_global(key), val)) + Ok((Scope::create_global(key), val)) } else { - Ok((Scope::into_local(key), val)) + Ok((Scope::create_local(key), val)) } } diff --git a/src/parser/def_block.rs b/src/parser/def_block.rs index 26333ef..080d1d4 100644 --- a/src/parser/def_block.rs +++ b/src/parser/def_block.rs @@ -55,8 +55,8 @@ fn parse_assign(pair: Pair) -> Result<(Scope, &str)> { } if global { - Ok((Scope::into_global(key), val)) + Ok((Scope::create_global(key), val)) } else { - Ok((Scope::into_local(key), val)) + Ok((Scope::create_local(key), val)) } } -- 2.45.2