diff options
author | Akshay <[email protected]> | 2021-10-23 08:11:52 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-23 08:11:52 +0100 |
commit | c2f0582d1907dbef69e9ad42ba9d4301337fe1e8 (patch) | |
tree | aacf975dfeb8bd416b70abfe8a21a2ce7c325d0c /bin/src/config.rs | |
parent | dfcdaf91674461a5150902cb3fdb8f198367ff20 (diff) |
initial implementation of multipass code fixer
Diffstat (limited to 'bin/src/config.rs')
-rw-r--r-- | bin/src/config.rs | 78 |
1 files changed, 60 insertions, 18 deletions
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 @@ | |||
1 | use std::{default::Default, fs, path::PathBuf, str::FromStr}; | 1 | use std::{ |
2 | default::Default, | ||
3 | fs, io, | ||
4 | path::{Path, PathBuf}, | ||
5 | str::FromStr, | ||
6 | }; | ||
2 | 7 | ||
3 | use clap::Clap; | 8 | use clap::Clap; |
4 | use globset::{GlobBuilder, GlobSetBuilder}; | 9 | use globset::{Error as GlobError, GlobBuilder, GlobSet, GlobSetBuilder}; |
5 | use vfs::ReadOnlyVfs; | 10 | use vfs::ReadOnlyVfs; |
6 | 11 | ||
7 | use crate::err::ConfigErr; | 12 | use crate::err::ConfigErr; |
@@ -77,25 +82,14 @@ pub struct LintConfig { | |||
77 | 82 | ||
78 | impl LintConfig { | 83 | impl LintConfig { |
79 | pub fn from_opts(opts: Opts) -> Result<Self, ConfigErr> { | 84 | pub fn from_opts(opts: Opts) -> Result<Self, ConfigErr> { |
80 | let ignores = { | 85 | let ignores = build_ignore_set(&opts.ignore).map_err(|err| { |
81 | let mut set = GlobSetBuilder::new(); | 86 | ConfigErr::InvalidGlob(err.glob().map(|i| i.to_owned()), err.kind().clone()) |
82 | for pattern in opts.ignore { | 87 | })?; |
83 | let glob = GlobBuilder::new(&pattern).build().map_err(|err| { | ||
84 | ConfigErr::InvalidGlob(err.glob().map(|i| i.to_owned()), err.kind().clone()) | ||
85 | })?; | ||
86 | set.add(glob); | ||
87 | } | ||
88 | set.build().map_err(|err| { | ||
89 | ConfigErr::InvalidGlob(err.glob().map(|i| i.to_owned()), err.kind().clone()) | ||
90 | }) | ||
91 | }?; | ||
92 | |||
93 | let walker = dirs::Walker::new(opts.target).map_err(ConfigErr::InvalidPath)?; | ||
94 | 88 | ||
95 | let files = walker | 89 | let files = walk_nix_files(&opts.target)? |
96 | .filter(|path| matches!(path.extension(), Some(e) if e == "nix")) | ||
97 | .filter(|path| !ignores.is_match(path)) | 90 | .filter(|path| !ignores.is_match(path)) |
98 | .collect(); | 91 | .collect(); |
92 | |||
99 | Ok(Self { | 93 | Ok(Self { |
100 | files, | 94 | files, |
101 | format: opts.format.unwrap_or_default(), | 95 | format: opts.format.unwrap_or_default(), |
@@ -113,6 +107,40 @@ impl LintConfig { | |||
113 | } | 107 | } |
114 | } | 108 | } |
115 | 109 | ||
110 | pub struct FixConfig { | ||
111 | pub files: Vec<PathBuf>, | ||
112 | pub diff_only: bool, | ||
113 | } | ||
114 | |||
115 | impl FixConfig { | ||
116 | pub fn from_opts(opts: Opts) -> Result<Self, ConfigErr> { | ||
117 | let ignores = build_ignore_set(&opts.ignore).map_err(|err| { | ||
118 | ConfigErr::InvalidGlob(err.glob().map(|i| i.to_owned()), err.kind().clone()) | ||
119 | })?; | ||
120 | |||
121 | let files = walk_nix_files(&opts.target)? | ||
122 | .filter(|path| !ignores.is_match(path)) | ||
123 | .collect(); | ||
124 | |||
125 | let diff_only = match opts.subcmd { | ||
126 | Some(SubCommand::Fix(f)) => f.diff_only, | ||
127 | _ => false, | ||
128 | }; | ||
129 | |||
130 | Ok(Self { files, diff_only }) | ||
131 | } | ||
132 | |||
133 | pub fn vfs(&self) -> Result<ReadOnlyVfs, ConfigErr> { | ||
134 | let mut vfs = ReadOnlyVfs::default(); | ||
135 | for file in self.files.iter() { | ||
136 | let _id = vfs.alloc_file_id(&file); | ||
137 | let data = fs::read_to_string(&file).map_err(ConfigErr::InvalidPath)?; | ||
138 | vfs.set_file_contents(&file, data.as_bytes()); | ||
139 | } | ||
140 | Ok(vfs) | ||
141 | } | ||
142 | } | ||
143 | |||
116 | mod dirs { | 144 | mod dirs { |
117 | use std::{ | 145 | use std::{ |
118 | fs, | 146 | fs, |
@@ -168,3 +196,17 @@ mod dirs { | |||
168 | } | 196 | } |
169 | } | 197 | } |
170 | } | 198 | } |
199 | |||
200 | fn build_ignore_set(ignores: &Vec<String>) -> Result<GlobSet, GlobError> { | ||
201 | let mut set = GlobSetBuilder::new(); | ||
202 | for pattern in ignores { | ||
203 | let glob = GlobBuilder::new(&pattern).build()?; | ||
204 | set.add(glob); | ||
205 | } | ||
206 | set.build() | ||
207 | } | ||
208 | |||
209 | fn walk_nix_files<P: AsRef<Path>>(target: P) -> Result<impl Iterator<Item = PathBuf>, io::Error> { | ||
210 | let walker = dirs::Walker::new(target)?; | ||
211 | Ok(walker.filter(|path: &PathBuf| matches!(path.extension(), Some(e) if e == "nix"))) | ||
212 | } | ||