aboutsummaryrefslogtreecommitdiff
path: root/bin/src/fix.rs
diff options
context:
space:
mode:
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}