From c2f0582d1907dbef69e9ad42ba9d4301337fe1e8 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 23 Oct 2021 12:41:52 +0530 Subject: initial implementation of multipass code fixer --- bin/src/config.rs | 78 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 18 deletions(-) (limited to 'bin/src/config.rs') diff --git a/bin/src/config.rs b/bin/src/config.rs index f2cf29d..077f73e 100644 --- a/bin/src/config.rs +++ b/bin/src/config.rs @@ -1,7 +1,12 @@ -use std::{default::Default, fs, path::PathBuf, str::FromStr}; +use std::{ + default::Default, + fs, io, + path::{Path, PathBuf}, + str::FromStr, +}; use clap::Clap; -use globset::{GlobBuilder, GlobSetBuilder}; +use globset::{Error as GlobError, GlobBuilder, GlobSet, GlobSetBuilder}; use vfs::ReadOnlyVfs; use crate::err::ConfigErr; @@ -77,25 +82,14 @@ pub struct LintConfig { impl LintConfig { pub fn from_opts(opts: Opts) -> Result { - let ignores = { - let mut set = GlobSetBuilder::new(); - for pattern in opts.ignore { - let glob = GlobBuilder::new(&pattern).build().map_err(|err| { - ConfigErr::InvalidGlob(err.glob().map(|i| i.to_owned()), err.kind().clone()) - })?; - set.add(glob); - } - set.build().map_err(|err| { - ConfigErr::InvalidGlob(err.glob().map(|i| i.to_owned()), err.kind().clone()) - }) - }?; - - let walker = dirs::Walker::new(opts.target).map_err(ConfigErr::InvalidPath)?; + let ignores = build_ignore_set(&opts.ignore).map_err(|err| { + ConfigErr::InvalidGlob(err.glob().map(|i| i.to_owned()), err.kind().clone()) + })?; - let files = walker - .filter(|path| matches!(path.extension(), Some(e) if e == "nix")) + let files = walk_nix_files(&opts.target)? .filter(|path| !ignores.is_match(path)) .collect(); + Ok(Self { files, format: opts.format.unwrap_or_default(), @@ -113,6 +107,40 @@ impl LintConfig { } } +pub struct FixConfig { + pub files: Vec, + pub diff_only: bool, +} + +impl FixConfig { + pub fn from_opts(opts: Opts) -> Result { + let ignores = build_ignore_set(&opts.ignore).map_err(|err| { + ConfigErr::InvalidGlob(err.glob().map(|i| i.to_owned()), err.kind().clone()) + })?; + + let files = walk_nix_files(&opts.target)? + .filter(|path| !ignores.is_match(path)) + .collect(); + + let diff_only = match opts.subcmd { + Some(SubCommand::Fix(f)) => f.diff_only, + _ => false, + }; + + Ok(Self { files, diff_only }) + } + + pub fn vfs(&self) -> Result { + let mut vfs = ReadOnlyVfs::default(); + for file in self.files.iter() { + let _id = vfs.alloc_file_id(&file); + let data = fs::read_to_string(&file).map_err(ConfigErr::InvalidPath)?; + vfs.set_file_contents(&file, data.as_bytes()); + } + Ok(vfs) + } +} + mod dirs { use std::{ fs, @@ -168,3 +196,17 @@ mod dirs { } } } + +fn build_ignore_set(ignores: &Vec) -> Result { + let mut set = GlobSetBuilder::new(); + for pattern in ignores { + let glob = GlobBuilder::new(&pattern).build()?; + set.add(glob); + } + set.build() +} + +fn walk_nix_files>(target: P) -> Result, io::Error> { + let walker = dirs::Walker::new(target)?; + Ok(walker.filter(|path: &PathBuf| matches!(path.extension(), Some(e) if e == "nix"))) +} -- cgit v1.2.3