Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 3dc4592

Browse files
committed
Replace Formatter trait with inherent impl
1 parent f5710b0 commit 3dc4592

File tree

4 files changed

+51
-68
lines changed

4 files changed

+51
-68
lines changed

src/actions/format.rs

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ use log::{log, debug};
2222
use rustfmt_nightly::{Config, Session, Input};
2323
use serde_json;
2424

25-
struct External<'a>(&'a Path, &'a Path);
26-
struct Internal;
27-
2825
/// Specified which `rustfmt` to use.
2926
#[derive(Clone)]
3027
pub enum Rustfmt {
@@ -43,81 +40,68 @@ impl From<Option<(String, PathBuf)>> for Rustfmt {
4340
}
4441
}
4542

46-
47-
pub trait Formatter {
48-
fn format(&self, input: String, cfg: Config) -> Result<String, String>;
49-
}
50-
51-
impl Formatter for External<'_> {
52-
fn format(&self, input: String, cfg: Config) -> Result<String, String> {
53-
let External(path, cwd) = self;
54-
55-
let (_file_handle, config_path) = gen_config_file(&cfg)?;
56-
let args = rustfmt_args(&cfg, &config_path);
57-
58-
let mut rustfmt = Command::new(path)
59-
.args(args)
60-
.current_dir(cwd)
61-
.stdin(Stdio::piped())
62-
.stdout(Stdio::piped())
63-
.spawn()
64-
.map_err(|_| format!("Couldn't spawn `{}`", path.display()))?;
65-
66-
{
67-
let stdin = rustfmt.stdin.as_mut()
68-
.ok_or_else(|| "Failed to open rustfmt stdin".to_string())?;
69-
stdin.write_all(input.as_bytes())
70-
.map_err(|_| "Failed to pass input to rustfmt".to_string())?;
43+
impl Rustfmt {
44+
pub fn format(&self, input: String, cfg: Config) -> Result<String, String> {
45+
match self {
46+
Rustfmt::Internal => format_internal(input, cfg),
47+
Rustfmt::External(path, cwd) => format_external(path, cwd, input, cfg),
7148
}
72-
73-
rustfmt.wait_with_output()
74-
.map_err(|err| format!("Error running rustfmt: {}", err))
75-
.and_then(|out| String::from_utf8(out.stdout)
76-
.map_err(|_| "Formatted code is not valid UTF-8".to_string()))
7749
}
7850
}
7951

80-
impl Formatter for Internal {
81-
fn format(&self, input: String, config: Config) -> Result<String, String> {
82-
let mut buf = Vec::<u8>::new();
52+
fn format_external(path: &PathBuf, cwd: &PathBuf, input: String, cfg: Config) -> Result<String, String> {
53+
let (_file_handle, config_path) = gen_config_file(&cfg)?;
54+
let args = rustfmt_args(&cfg, &config_path);
55+
56+
let mut rustfmt = Command::new(path)
57+
.args(args)
58+
.current_dir(cwd)
59+
.stdin(Stdio::piped())
60+
.stdout(Stdio::piped())
61+
.spawn()
62+
.map_err(|_| format!("Couldn't spawn `{}`", path.display()))?;
63+
64+
{
65+
let stdin = rustfmt.stdin.as_mut()
66+
.ok_or_else(|| "Failed to open rustfmt stdin".to_string())?;
67+
stdin.write_all(input.as_bytes())
68+
.map_err(|_| "Failed to pass input to rustfmt".to_string())?;
69+
}
70+
71+
rustfmt.wait_with_output()
72+
.map_err(|err| format!("Error running rustfmt: {}", err))
73+
.and_then(|out| String::from_utf8(out.stdout)
74+
.map_err(|_| "Formatted code is not valid UTF-8".to_string()))
75+
}
8376

84-
{
85-
let mut session = Session::new(config, Some(&mut buf));
77+
fn format_internal(input: String, config:Config) -> Result<String, String> {
78+
let mut buf = Vec::<u8>::new();
8679

87-
match session.format(Input::Text(input)) {
88-
Ok(report) => {
89-
// Session::format returns Ok even if there are any errors, i.e., parsing errors.
90-
if session.has_operational_errors() || session.has_parsing_errors() {
91-
debug!(
92-
"reformat: format_input failed: has errors, report = {}",
93-
report
94-
);
80+
{
81+
let mut session = Session::new(config, Some(&mut buf));
9582

96-
return Err("Reformat failed to complete successfully".into());
97-
}
98-
}
99-
Err(e) => {
100-
debug!("Reformat failed: {:?}", e);
83+
match session.format(Input::Text(input)) {
84+
Ok(report) => {
85+
// Session::format returns Ok even if there are any errors, i.e., parsing errors.
86+
if session.has_operational_errors() || session.has_parsing_errors() {
87+
debug!(
88+
"reformat: format_input failed: has errors, report = {}",
89+
report
90+
);
10191

10292
return Err("Reformat failed to complete successfully".into());
10393
}
10494
}
105-
}
106-
107-
String::from_utf8(buf)
108-
.map_err(|_| "Reformat output is not a valid UTF-8".into())
109-
}
110-
}
95+
Err(e) => {
96+
debug!("Reformat failed: {:?}", e);
11197

112-
impl Formatter for Rustfmt {
113-
fn format(&self, input: String, cfg: Config) -> Result<String, String> {
114-
match self {
115-
Rustfmt::Internal => Internal.format(input, cfg),
116-
Rustfmt::External(path, cwd) => {
117-
External(path.as_path(), cwd.as_path()).format(input, cfg)
98+
return Err("Reformat failed to complete successfully".into());
11899
}
119100
}
120101
}
102+
103+
String::from_utf8(buf)
104+
.map_err(|_| "Reformat output is not a valid UTF-8".into())
121105
}
122106

123107
fn random_file() -> Result<(File, PathBuf), String> {

src/actions/hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use crate::actions::format::{Rustfmt, Formatter};
11+
use crate::actions::format::Rustfmt;
1212
use crate::actions::requests;
1313
use crate::actions::InitActionContext;
1414
use crate::config::FmtConfig;

src/actions/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ impl InitActionContext {
270270
racer::Session::with_project_model(cache, pm)
271271
}
272272

273-
/// Since external Rustfmt can be specified, this returns an enum specifying
274-
/// which one to use and which implements the String-formatting `Formatter`
275-
/// trait.
273+
/// Depending on user configuration, we might use either external Rustfmt or
274+
/// the one we're shipping with.
275+
/// Locks config to read `rustfmt_path` key.
276276
fn formatter(&self) -> Rustfmt {
277277
let rustfmt = self.config.lock().unwrap().rustfmt_path.clone()
278278
.map(|path| (path, self.current_project.clone()));

src/actions/requests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use itertools::Itertools;
2222
use serde_derive::{Serialize, Deserialize};
2323
use log::{debug, log, trace};
2424

25-
use crate::actions::format::Formatter;
2625
use crate::actions::hover;
2726
use crate::actions::work_pool;
2827
use crate::actions::work_pool::WorkDescription;

0 commit comments

Comments
 (0)