2PCWN3KWPCSPLKGKZ553CBIYDEEBI6WDCKVRE2ZH2Z74LRTFD25AC
R524JUUE57KNHXLPN52X6DWK6PPQPBG5DXYNSVLGSEI5UMYXWDMQC
2JBFREZGJ2PST2DE3ZVDQADXAOFXBYPMSFTG7C65GDKLOZGETTGAC
2NYDNXH7PDF7ZYAGJXWYOUJKGDT4HVWZRU73SA4CSFLVIIK2CIDAC
D6UTHZA4XNAR2PTG4YEZFNNH3OTSNOWGDSVBYDRE5R2YSV7MPN6AC
MG46NYACGKHTJ5V6NLPDGKQSETQQNCOGP7TDS4KPJDJFNPKWEFJAC
SJ6AFVZL5HEXG5ZUV5STIGGIGLU56WGAQCSZBFFHQOVHAILBOS2QC
self.open_file(file)
.and_then(|file| {
PrettyPrinter::buffered(pretty_printer_config, file).print_directives(&directives)
})
.context(PrettyPrintingSnafu { file })
}
fn open_file(&self, file_path: &Utf8Path) -> io::Result<File> {
self.ensure_directory(file_path.parent().unwrap())?;
File::create(file_path)
}
let mut buffer = Vec::new();
PrettyPrinter::unbuffered(pretty_printer_config, &mut buffer)
.print_directives(&directives)
.and_then(|_| update_file(path, &buffer))
.context(PrettyPrintingSnafu { file: path })?;
fn ensure_directory(directory: &Utf8Path) -> io::Result<()> {
fs::create_dir_all(directory)
}
fn open_file(file_path: &Utf8Path) -> io::Result<File> {
ensure_directory(file_path.parent().unwrap())?;
File::options()
.create(true)
.read(true)
.write(true)
.open(file_path)
}
fn update_file(path: &Utf8Path, buffer: &[u8]) -> io::Result<()> {
let mut file = open_file(path)?;
let size =
usize::try_from(file.metadata()?.len()).expect("file should be smaller than usize::MAX");
if buffer.len() == size {
let mut current_contents = Vec::with_capacity(size);
file.read_to_end(&mut current_contents)?;
if current_contents == buffer {
tracing::debug!(%path, "skipping write of unchanged file");
return Ok(());
}
}
// File changed in the import process
file.write_all(buffer)?;
file.set_len(u64::try_from(buffer.len()).expect("should always work"))
}