weld_codegen/
format.rs

1//! implementations of source code formatters (rustfmt, gofmt)
2//!
3
4use crate::error::{Error, Result};
5
6/// Formats source code
7#[allow(unused_variables)]
8pub trait SourceFormatter {
9    /// run formatter on all files
10    /// Default implementation does nothing
11    fn run(&self, source_files: &[&str]) -> Result<()> {
12        Ok(())
13    }
14}
15
16/// A formatter that does not format any code
17#[derive(Default)]
18pub struct NullFormatter {}
19impl SourceFormatter for NullFormatter {}
20
21/// execute the program with args
22pub(crate) fn run_command(program: &str, args: &[&str]) -> Result<()> {
23    let mut child = std::process::Command::new(program)
24        .args(args.iter())
25        .spawn()
26        .map_err(|e| Error::Formatter(format!("failed to start: {e}")))?;
27
28    let code = child
29        .wait()
30        .map_err(|e| Error::Formatter(format!("failed waiting for formatter: {e}")))?;
31    if !code.success() {
32        return Err(Error::Formatter(code.to_string()));
33    }
34    Ok(())
35}