aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rw-r--r--bin/Cargo.toml2
-rw-r--r--bin/src/config.rs3
-rw-r--r--bin/src/err.rs2
-rw-r--r--bin/src/fix.rs5
-rw-r--r--bin/src/fix/all.rs4
-rw-r--r--bin/src/fix/single.rs14
-rw-r--r--bin/src/main.rs31
7 files changed, 36 insertions, 25 deletions
diff --git a/bin/Cargo.toml b/bin/Cargo.toml
index 528f804..c5f222b 100644
--- a/bin/Cargo.toml
+++ b/bin/Cargo.toml
@@ -1,6 +1,6 @@
1[package] 1[package]
2name = "statix" 2name = "statix"
3version = "0.1.0" 3version = "0.2.0"
4edition = "2018" 4edition = "2018"
5 5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
diff --git a/bin/src/config.rs b/bin/src/config.rs
index 8ddfb4a..c856a59 100644
--- a/bin/src/config.rs
+++ b/bin/src/config.rs
@@ -234,9 +234,10 @@ impl FromStr for OutFormat {
234 match value.to_ascii_lowercase().as_str() { 234 match value.to_ascii_lowercase().as_str() {
235 #[cfg(feature = "json")] 235 #[cfg(feature = "json")]
236 "json" => Ok(Self::Json), 236 "json" => Ok(Self::Json),
237 #[cfg(not(feature = "json"))]
238 "json" => Err("statix was not compiled with the `json` feature flag"),
237 "errfmt" => Ok(Self::Errfmt), 239 "errfmt" => Ok(Self::Errfmt),
238 "stderr" => Ok(Self::StdErr), 240 "stderr" => Ok(Self::StdErr),
239 "json" => Err("statix was not compiled with the `json` feature flag"),
240 _ => Err("unknown output format, try: json, errfmt"), 241 _ => Err("unknown output format, try: json, errfmt"),
241 } 242 }
242 } 243 }
diff --git a/bin/src/err.rs b/bin/src/err.rs
index 5f71b57..53c3222 100644
--- a/bin/src/err.rs
+++ b/bin/src/err.rs
@@ -11,7 +11,7 @@ pub enum ConfigErr {
11 #[error("path error: {0}")] 11 #[error("path error: {0}")]
12 InvalidPath(#[from] io::Error), 12 InvalidPath(#[from] io::Error),
13 #[error("unable to parse `{0}` as line and column")] 13 #[error("unable to parse `{0}` as line and column")]
14 InvalidPosition(String) 14 InvalidPosition(String),
15} 15}
16 16
17#[derive(Error, Debug)] 17#[derive(Error, Debug)]
diff --git a/bin/src/fix.rs b/bin/src/fix.rs
index a7ddc4f..c378c13 100644
--- a/bin/src/fix.rs
+++ b/bin/src/fix.rs
@@ -24,6 +24,9 @@ pub struct Fixed {
24 24
25impl<'a> FixResult<'a> { 25impl<'a> FixResult<'a> {
26 fn empty(src: Source<'a>) -> Self { 26 fn empty(src: Source<'a>) -> Self {
27 Self { src, fixed: Vec::new() } 27 Self {
28 src,
29 fixed: Vec::new(),
30 }
28 } 31 }
29} 32}
diff --git a/bin/src/fix/all.rs b/bin/src/fix/all.rs
index 8c0770d..7f04f2c 100644
--- a/bin/src/fix/all.rs
+++ b/bin/src/fix/all.rs
@@ -3,7 +3,7 @@ use std::borrow::Cow;
3use lib::{Report, LINTS}; 3use lib::{Report, LINTS};
4use rnix::{parser::ParseError as RnixParseErr, WalkEvent}; 4use rnix::{parser::ParseError as RnixParseErr, WalkEvent};
5 5
6use crate::fix::{Fixed, FixResult}; 6use crate::fix::{FixResult, Fixed};
7 7
8fn collect_fixes(source: &str) -> Result<Vec<Report>, RnixParseErr> { 8fn collect_fixes(source: &str) -> Result<Vec<Report>, RnixParseErr> {
9 let parsed = rnix::parse(source).as_result()?; 9 let parsed = rnix::parse(source).as_result()?;
@@ -73,7 +73,7 @@ impl<'a> Iterator for FixResult<'a> {
73 73
74 Some(FixResult { 74 Some(FixResult {
75 src: self.src.clone(), 75 src: self.src.clone(),
76 fixed 76 fixed,
77 }) 77 })
78 } 78 }
79} 79}
diff --git a/bin/src/fix/single.rs b/bin/src/fix/single.rs
index 24b5c51..c09c710 100644
--- a/bin/src/fix/single.rs
+++ b/bin/src/fix/single.rs
@@ -12,7 +12,11 @@ pub struct SingleFixResult<'δ> {
12 12
13fn pos_to_byte(line: usize, col: usize, src: &str) -> Result<TextSize, SingleFixErr> { 13fn pos_to_byte(line: usize, col: usize, src: &str) -> Result<TextSize, SingleFixErr> {
14 let mut byte: TextSize = TextSize::of(""); 14 let mut byte: TextSize = TextSize::of("");
15 for (l, _) in src.split_inclusive('\n').zip(1..).take_while(|(_, i)| i < &line) { 15 for (l, _) in src
16 .split_inclusive('\n')
17 .zip(1..)
18 .take_while(|(_, i)| i < &line)
19 {
16 byte += TextSize::of(l); 20 byte += TextSize::of(l);
17 } 21 }
18 byte += TextSize::try_from(col).map_err(|_| SingleFixErr::Conversion(col))?; 22 byte += TextSize::try_from(col).map_err(|_| SingleFixErr::Conversion(col))?;
@@ -45,8 +49,8 @@ fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> {
45 } else { 49 } else {
46 None 50 None
47 } 51 }
48 }, 52 }
49 _ => None 53 _ => None,
50 }) 54 })
51 .flatten() 55 .flatten()
52 .next() 56 .next()
@@ -60,7 +64,5 @@ pub fn single(line: usize, col: usize, src: &str) -> Result<SingleFixResult, Sin
60 64
61 report.apply(src.to_mut()); 65 report.apply(src.to_mut());
62 66
63 Ok(SingleFixResult { 67 Ok(SingleFixResult { src })
64 src
65 })
66} 68}
diff --git a/bin/src/main.rs b/bin/src/main.rs
index cbf2601..4063621 100644
--- a/bin/src/main.rs
+++ b/bin/src/main.rs
@@ -6,7 +6,10 @@ mod traits;
6 6
7use std::io; 7use std::io;
8 8
9use crate::{err::{StatixErr, FixErr, SingleFixErr}, traits::WriteDiagnostic}; 9use crate::{
10 err::{FixErr, SingleFixErr, StatixErr},
11 traits::WriteDiagnostic,
12};
10 13
11use clap::Clap; 14use clap::Clap;
12use config::{Opts, SubCommand}; 15use config::{Opts, SubCommand};
@@ -17,7 +20,8 @@ fn _main() -> Result<(), StatixErr> {
17 match opts.cmd { 20 match opts.cmd {
18 SubCommand::Check(check_config) => { 21 SubCommand::Check(check_config) => {
19 let vfs = check_config.vfs()?; 22 let vfs = check_config.vfs()?;
20 let (lints, errors): (Vec<_>, Vec<_>) = vfs.iter().map(lint::lint).partition(Result::is_ok); 23 let (lints, errors): (Vec<_>, Vec<_>) =
24 vfs.iter().map(lint::lint).partition(Result::is_ok);
21 let lint_results = lints.into_iter().map(Result::unwrap); 25 let lint_results = lints.into_iter().map(Result::unwrap);
22 let errors = errors.into_iter().map(Result::unwrap_err); 26 let errors = errors.into_iter().map(Result::unwrap_err);
23 27
@@ -28,7 +32,7 @@ fn _main() -> Result<(), StatixErr> {
28 errors.for_each(|e| { 32 errors.for_each(|e| {
29 eprintln!("{}", e); 33 eprintln!("{}", e);
30 }); 34 });
31 }, 35 }
32 SubCommand::Fix(fix_config) => { 36 SubCommand::Fix(fix_config) => {
33 let vfs = fix_config.vfs()?; 37 let vfs = fix_config.vfs()?;
34 for entry in vfs.iter() { 38 for entry in vfs.iter() {
@@ -40,17 +44,17 @@ fn _main() -> Result<(), StatixErr> {
40 println!( 44 println!(
41 "{}", 45 "{}",
42 text_diff 46 text_diff
43 .unified_diff() 47 .unified_diff()
44 .context_radius(4) 48 .context_radius(4)
45 .header(&old_file, &new_file) 49 .header(&old_file, &new_file)
46 ); 50 );
47 } else { 51 } else {
48 let path = entry.file_path; 52 let path = entry.file_path;
49 std::fs::write(path, &*fix_result.src).map_err(FixErr::InvalidPath)?; 53 std::fs::write(path, &*fix_result.src).map_err(FixErr::InvalidPath)?;
50 } 54 }
51 } 55 }
52 } 56 }
53 }, 57 }
54 SubCommand::Single(single_config) => { 58 SubCommand::Single(single_config) => {
55 let path = single_config.target; 59 let path = single_config.target;
56 let src = std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)?; 60 let src = std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)?;
@@ -63,12 +67,13 @@ fn _main() -> Result<(), StatixErr> {
63 println!( 67 println!(
64 "{}", 68 "{}",
65 text_diff 69 text_diff
66 .unified_diff() 70 .unified_diff()
67 .context_radius(4) 71 .context_radius(4)
68 .header(&old_file, &new_file) 72 .header(&old_file, &new_file)
69 ); 73 );
70 } else { 74 } else {
71 std::fs::write(&path, &*single_fix_result.src).map_err(SingleFixErr::InvalidPath)?; 75 std::fs::write(&path, &*single_fix_result.src)
76 .map_err(SingleFixErr::InvalidPath)?;
72 } 77 }
73 } 78 }
74 } 79 }