From 242b0b9591fa4fd5f8b4a45ec4c179f451c62c14 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 15 May 2022 23:44:53 -0700 Subject: feat: initial development release --- src/commands/format.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/commands/format.rs (limited to 'src/commands/format.rs') diff --git a/src/commands/format.rs b/src/commands/format.rs new file mode 100644 index 0000000..a0aabc4 --- /dev/null +++ b/src/commands/format.rs @@ -0,0 +1,79 @@ +// This file is part of Chorus . +// Copyright (C) 2022-2022 Fuwn +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use std::path::PathBuf; + +use walkdir::{DirEntry, WalkDir}; + +/// Format an existing Chorus package +#[allow(clippy::unnecessary_wraps)] +pub fn format(path: &Option) -> anyhow::Result<()> { + let our_path = path.clone().unwrap_or_else(|| PathBuf::from(".")); + + // Only try to format if the target Chorus package directory exists + if our_path.exists() { + let mut entries: Vec = vec![]; + + std::env::set_current_dir(&our_path)?; + + // Iterate over all paths in the target Chorus package + for entry in WalkDir::new(our_path).into_iter().filter_map(Result::ok) { + // Only track COBOL files to format if the file has a valid COBOL extension + if entry + .file_name() + .to_str() + .unwrap_or("") + .rsplit('.') + .next() + .map(|extension: &str| { + #[allow(clippy::blocks_in_if_conditions)] + { + let mut valid_extension = false; + + // If any user defined COBOL extensions exist, check against them; + // otherwise, check against the "cbl" file extension. + if let Some(valid_extensions) = + &crate::config::CONFIG.build.extensions + { + for an_extension in valid_extensions { + if valid_extension { + break; + } + + valid_extension = extension.eq_ignore_ascii_case(an_extension); + } + } else { + valid_extension = extension.eq_ignore_ascii_case("cbl"); + } + + valid_extension + } + }) + == Some(true) + { + entries.push(entry); + } + } + + for entry in entries { + println!("entry: {:?}", entry); + } + } + + Ok(()) +} -- cgit v1.2.3