Repository: casey/just
File: src/subcommand.rs (Rust)

Code before:
    if !status.success() {
      return Err(Error::EditorStatus { editor, status });
    }

    Ok(())
  }

  fn format(config: &Config, ast: Ast, search: &Search) -> Result<(), Error<'static>> {
    config.require_unstable("The `--fmt` command is currently unstable.")?;

    if config.dry_run {
      use std::io::Read as _;
      let mut current_content = String::new();
      let mut file = File::open(&search.justfile).map_err(|e| Error::Load {
        path: search.justfile.clone(),
        io_error: e,
      })?;
      file
        .read_to_string(&mut current_content)
        .map_err(|e| Error::Load {
          path: search.justfile.clone(),
          io_error: e,
        })?;

      if current_content != format!("{}", ast) {
        Err(Error::NotWellFormatted {
          justfile: search.justfile.clone(),
        })
      } else {
        if config.verbosity.loud() {
          eprintln!("Justfile `{}` is well-formatted.", search.justfile.display());
        }
        Ok(())
      }
    } else if let Err(io_error) =
      File::create(&search.justfile).and_then(|mut file| write!(file, "{}", ast))
    {
      Err(Error::WriteJustfile {
        justfile: search.justfile.clone(),
        io_error,
      })
    } else {
      if config.verbosity.loud() {
        eprintln!("Wrote justfile to `{}`", search.justfile.display());
      }
      Ok(())
    }
  }

  fn init(config: &Config) -> Result<(), Error<'static>> {
    let search = Search::init(&config.search_config, &config.invocation_directory)?;

Review comment (suggestion):
I think we probably shouldn't call justfiles that produce diffs "not well formatted". The just have some different formatting which is not necessarily bad, just nonstandard.

```suggestion
        Err(Error::FormatCheckFoundDiff {
```

Code after:
    if !status.success() {
      return Err(Error::EditorStatus { editor, status });
    }

    Ok(())
  }

  fn format(config: &Config, ast: Ast, src: &str, search: &Search) -> Result<(), Error<'static>> {
    config.require_unstable("The `--fmt` command is currently unstable.")?;

    let src_formatted = ast.to_string();
    if config.check {
      if src == src_formatted {
        Ok(())
      } else {
        print!(
          "{}",
          similar::udiff::unified_diff(similar::Algorithm::Patience, &src, &src_formatted, 2, None)
        );
        Err(Error::FormatCheckFoundDiff)
      }
    } else if let Err(io_error) =
      File::create(&search.justfile).and_then(|mut file| file.write_all(&src_formatted.as_bytes()))
    {
      Err(Error::WriteJustfile {
        justfile: search.justfile.clone(),
        io_error,
      })
    } else {
      if config.verbosity.loud() {
        eprintln!("Wrote justfile to `{}`", search.justfile.display());
      }
      Ok(())
    }
  }

  fn init(config: &Config) -> Result<(), Error<'static>> {
    let search = Search::init(&config.search_config, &config.invocation_directory)?;

    if search.justfile.is_file() {
      Err(Error::InitExists {
        justfile: search.justfile,
      })
    } else if let Err(io_error) = fs::write(&search.justfile, INIT_JUSTFILE) {
      Err(Error::WriteJustfile {
        justfile: search.justfile,
        io_error,
      })
    } else {
      if config.verbosity.loud() {
        eprintln!("Wrote justfile to `{}`", search.justfile.display());