Skip to main content

format_file

Function format_file 

Source
pub fn format_file(path: &Path) -> Result<bool, FormatFileError>
Expand description

Formats the adam-lang source file at path in place.

  • Postcondition: on Ok(true), path’s contents are replaced with [adam_lang::format_source]’s output. On Ok(false), path is left untouched because it already matched that output exactly (no write performed).
  • Complexity: for n input bytes and m output bytes, reads and compares O(n + m) bytes and writes O(m) bytes when changed. Uses O(n + m) space, in addition to [adam_lang::format_source]’s time and space.

§Errors

Returns FormatFileError::Io if path can’t be read, or (only after formatting succeeds and differs from the original) can’t be written back. Returns FormatFileError::Format if path’s contents can’t be safely formatted (see [adam_lang::format_source]’s # Errors). A read or format error leaves the file untouched; a write error may leave a partial write.

§Examples

use std::path::Path;

let path = Path::new("sheet.adm2");
let changed = adam_fmt::format_file(path)?;
assert!(!adam_fmt::format_file(path)?);