From 85bed151d467a7b75fd02cca2d6071f21198b6c1 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Sun, 14 May 2023 15:17:30 -0500 Subject: [PATCH] cleanup --- src/builder.rs | 15 ++++++++++++--- src/lib.rs | 15 +++++---------- src/metafile.rs | 6 ++++-- src/parser.rs | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 08deae6..ff8e702 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -120,7 +120,7 @@ fn get_pattern(key: &str, file: &MetaFile) -> Result { // BLANK returns nothing, so no more processing needs to be done if filename == "BLANK" { - return Ok(String::new()); + return Ok(String::from("")); }; // DEFAULT override for patterns defined higher in chain @@ -140,8 +140,17 @@ fn get_pattern(key: &str, file: &MetaFile) -> Result { metafile_to_string(&pattern) } -fn get_variable(_key: &str, _file: &MetaFile) -> Result { - todo!() +fn get_variable(key: &str, file: &MetaFile) -> Result { + let long_key = file.name()? + "." + key; + if let Some(val) = file.get_var(&long_key) { + Ok(val.clone()) + } else if let Some(val) = file.get_var(key) { + Ok(val.clone()) + } else if file.opts.undefined { + bail!("undefined variable: {}, {}", key, long_key) + } else { + Ok(String::new()) + } } fn find_dest(path: &Path, opts: &Options) -> Result { diff --git a/src/lib.rs b/src/lib.rs index 9a9a080..7f342f3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,16 +18,11 @@ use color_eyre::Result; pub fn get_opts() -> Result { let opts = Options::try_from(Opts::parse())?; - log!( - opts, - format!("cleaning build directory: {}", opts.build.display()), - 1 - ); - if opts.clean && opts.build.exists() { + let exists = opts.build.exists(); + if exists && opts.clean { std::fs::remove_dir_all(&opts.build)?; - } - - if !opts.build.exists() { + std::fs::create_dir(&opts.build)?; + } else if !exists { std::fs::create_dir(&opts.build)?; } @@ -37,7 +32,7 @@ pub fn get_opts() -> Result { pub fn build_dir(opts: &Options) -> Result<()> { let mut source = DirNode::build(opts.source.clone(), opts)?; - let global_init = MetaFile::new(&opts); + let global_init = MetaFile::new(opts); source.map(&global_init)?; diff --git a/src/metafile.rs b/src/metafile.rs index 01ef06f..a090dc3 100644 --- a/src/metafile.rs +++ b/src/metafile.rs @@ -19,7 +19,6 @@ impl<'a> MetaFile<'a> { let str = fs::read_to_string(&path)?; let mut metafile = parse_file(str, opts)?; metafile.path = path; - metafile.opts = opts; Ok(metafile) } @@ -121,7 +120,10 @@ impl<'a> DirNode<'a> { pub fn build(path: PathBuf, opts: &'a Options) -> Result { assert!(path.is_dir() && path.exists()); - fs::create_dir(opts.build.join(path.strip_prefix(&opts.source)?))?; + let build_dir = opts.build.join(path.strip_prefix(&opts.source)?); + if !build_dir.exists() { + fs::create_dir(build_dir)?; + } let files: Vec = Vec::new(); let dirs: Vec = Vec::new(); diff --git a/src/parser.rs b/src/parser.rs index 65e11d3..817302b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -72,7 +72,7 @@ fn parse_source(pairs: Pairs) -> Vec { Rule::var_sub => vec.push(source!(var(parse_sub(pair)))), Rule::arr_sub => vec.push(source!(arr(parse_sub(pair)))), Rule::pat_sub => vec.push(source!(pat(parse_sub(pair)))), - Rule::char_seq => vec.push(source!(pair)), + Rule::char_seq => vec.push(source!(pair.as_str())), // anything that isn't a substitution is a char_seq inside source _ => unreachable!(), } -- 2.45.2