aboutsummaryrefslogtreecommitdiff
path: root/bin/src/fix.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-11-06 09:21:55 +0000
committerAkshay <[email protected]>2021-11-06 09:21:55 +0000
commit324a333e671ed521138ad50fe463ed187874176e (patch)
tree8f294a109955881a14dbbb2bc088f14255fad687 /bin/src/fix.rs
parent63eba9d5c697e466d952588568719d0de1244ae4 (diff)
introducing "streaming" option to check, fix and single
Diffstat (limited to 'bin/src/fix.rs')
-rw-r--r--bin/src/fix.rs88
1 files changed, 86 insertions, 2 deletions
diff --git a/bin/src/fix.rs b/bin/src/fix.rs
index c378c13..e4ea94d 100644
--- a/bin/src/fix.rs
+++ b/bin/src/fix.rs
@@ -3,10 +3,10 @@ use std::borrow::Cow;
3use rnix::TextRange; 3use rnix::TextRange;
4 4
5mod all; 5mod all;
6pub use all::all; 6use all::all;
7 7
8mod single; 8mod single;
9pub use single::single; 9use single::single;
10 10
11type Source<'a> = Cow<'a, str>; 11type Source<'a> = Cow<'a, str>;
12 12
@@ -30,3 +30,87 @@ impl<'a> FixResult<'a> {
30 } 30 }
31 } 31 }
32} 32}
33
34pub mod main {
35 use std::borrow::Cow;
36
37 use crate::{
38 config::{Fix as FixConfig, FixOut, Single as SingleConfig},
39 err::{FixErr, StatixErr},
40 };
41
42 use similar::TextDiff;
43
44 pub fn all(fix_config: FixConfig) -> Result<(), StatixErr> {
45 let vfs = fix_config.vfs()?;
46 for entry in vfs.iter() {
47 match (fix_config.out(), super::all(entry.contents)) {
48 (FixOut::Diff, fix_result) => {
49 let src = fix_result
50 .map(|r| r.src)
51 .unwrap_or(Cow::Borrowed(entry.contents));
52 let text_diff = TextDiff::from_lines(entry.contents, &src);
53 let old_file = format!("{}", entry.file_path.display());
54 let new_file = format!("{} [fixed]", entry.file_path.display());
55 println!(
56 "{}",
57 text_diff
58 .unified_diff()
59 .context_radius(4)
60 .header(&old_file, &new_file)
61 );
62 }
63 (FixOut::Stream, fix_result) => {
64 let src = fix_result
65 .map(|r| r.src)
66 .unwrap_or(Cow::Borrowed(entry.contents));
67 println!("{}", &src)
68 }
69 (FixOut::Write, Some(fix_result)) => {
70 let path = entry.file_path;
71 std::fs::write(path, &*fix_result.src).map_err(FixErr::InvalidPath)?;
72 }
73 _ => (),
74 };
75 }
76 Ok(())
77 }
78
79 pub fn single(single_config: SingleConfig) -> Result<(), StatixErr> {
80 let vfs = single_config.vfs()?;
81 let entry = vfs.iter().next().unwrap();
82 let path = entry.file_path.display().to_string();
83 let original_src = entry.contents;
84 let (line, col) = single_config.position;
85
86 match (single_config.out(), super::single(line, col, original_src)) {
87 (FixOut::Diff, single_result) => {
88 let fixed_src = single_result
89 .map(|r| r.src)
90 .unwrap_or(Cow::Borrowed(original_src));
91 let text_diff = TextDiff::from_lines(original_src, &fixed_src);
92 let old_file = &path;
93 let new_file = format!("{} [fixed]", &path);
94 println!(
95 "{}",
96 text_diff
97 .unified_diff()
98 .context_radius(4)
99 .header(&old_file, &new_file)
100 );
101 }
102 (FixOut::Stream, single_result) => {
103 let src = single_result
104 .map(|r| r.src)
105 .unwrap_or(Cow::Borrowed(original_src));
106 println!("{}", &src)
107 }
108 (FixOut::Write, Ok(single_result)) => {
109 let path = entry.file_path;
110 std::fs::write(path, &*single_result.src).map_err(FixErr::InvalidPath)?;
111 }
112 (_, Err(e)) => return Err(e.into()),
113 };
114 Ok(())
115 }
116}