1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2022 Oxide Computer Company

//! Use `rustfmt` to format generated code:
//! ```
//! let codegen = quote::quote!{ struct Foo { bar: String } };
//! let formatted: String = rustfmt_wrapper::rustfmt(codegen).unwrap();
//! ```

use std::{
    env,
    io::Write,
    path::PathBuf,
    process::{Command, Stdio},
};

use thiserror::Error;

pub mod config;

#[derive(Error, Debug)]
pub enum Error {
    /// Command `rustfmt` could not be found
    #[error("rustfmt is not installed")]
    NoRustfmt,
    /// Command `rustfmt` produced an error at runtime.
    #[error("rustfmt runtime error")]
    Rustfmt(String),
    /// Nightly channel required, but not found.
    #[error("nightly channel required for unstable options")]
    Unstable(String),
    /// Error with file IO
    #[error(transparent)]
    IO(#[from] std::io::Error),
    /// Error from reading stdin of rustfmt
    #[error(transparent)]
    Conversion(#[from] std::string::FromUtf8Error),
}

/// Use the `rustfmt` command to format the input.
pub fn rustfmt<T: ToString>(input: T) -> Result<String, Error> {
    // The only rustfmt default we override is edition = 2018 (vs 2015)
    let config = config::Config {
        edition: Some(config::Edition::Edition2018),
        ..Default::default()
    };
    rustfmt_config(config, input)
}

/// Use the `rustfmt` command to format the input with the given [`Config`].
///
/// [`Config`]: config::Config
pub fn rustfmt_config<T: ToString>(mut config: config::Config, input: T) -> Result<String, Error> {
    let input = input.to_string();

    // rustfmt's default edition is 2015; our default is 2021.
    if config.edition.is_none() {
        config.edition = Some(config::Edition::Edition2018);
    }

    let mut builder = tempfile::Builder::new();
    builder.prefix("rustfmt-wrapper");
    let outdir = builder.tempdir().expect("failed to create tmp file");

    let rustfmt_config_path = outdir.as_ref().join("rustfmt.toml");
    std::fs::write(
        rustfmt_config_path,
        toml::to_string_pretty(&config).unwrap(),
    )?;

    let rustfmt = which_rustfmt().ok_or(Error::NoRustfmt)?;

    let mut args = vec![format!("--config-path={}", outdir.path().to_str().unwrap())];
    if config.unstable() {
        args.push("--unstable-features".to_string())
    }

    let mut command = Command::new(&rustfmt)
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();

    let mut stdin = command.stdin.take().unwrap();
    std::thread::spawn(move || {
        stdin
            .write_all(input.as_bytes())
            .expect("Failed to write to stdin");
    });

    let output = command.wait_with_output()?;
    if output.status.success() {
        Ok(String::from_utf8(output.stdout)?)
    } else {
        let err_str = String::from_utf8(output.stderr)?;
        if err_str.contains("Unrecognized option: 'unstable-features'") {
            Err(Error::Unstable(config.list_unstable()))
        } else {
            Err(Error::Rustfmt(err_str))
        }
    }
}

fn which_rustfmt() -> Option<PathBuf> {
    match env::var_os("RUSTFMT") {
        Some(which) => {
            if which.is_empty() {
                None
            } else {
                Some(PathBuf::from(which))
            }
        }
        None => toolchain_find::find_installed_component("rustfmt"),
    }
}

#[cfg(test)]
mod tests {
    use crate::{config::Config, rustfmt, rustfmt_config};
    use newline_converter::dos2unix;
    use quote::quote;

    #[test]
    fn test_basics() {
        let code = quote! { struct Foo { bar: String } };
        assert_eq!(
            dos2unix(rustfmt(code).unwrap().as_str()),
            "struct Foo {\n    bar: String,\n}\n"
        );
    }

    #[test]
    fn test_doc_comments() {
        let comment = "This is a very long doc comment that could span \
        multiple lines of text. For the purposes of this test, we're hoping \
        that it gets formatted into a single, nice doc comment.";
        let code = quote! {
           #[doc = #comment]
           struct Foo { bar: String }
        };

        let config = Config {
            normalize_doc_attributes: Some(true),
            wrap_comments: Some(true),
            ..Default::default()
        };

        assert_eq!(
            dos2unix(rustfmt_config(config, code).unwrap().as_str()),
            r#"///This is a very long doc comment that could span multiple lines of text. For
/// the purposes of this test, we're hoping that it gets formatted into a
/// single, nice doc comment.
struct Foo {
    bar: String,
}
"#,
        );
    }

    #[test]
    fn test_narrow_call() {
        let code = quote! {
            async fn go() {
                let _ = Client::new().operation_id().send().await?;
            }
        };

        let config = Config {
            max_width: Some(45),
            ..Default::default()
        };

        assert_eq!(
            dos2unix(rustfmt_config(config, code).unwrap().as_str()),
            "async fn go() {
    let _ = Client::new()
        .operation_id()
        .send()
        .await?;
}\n"
        );
    }
}