PathBufLikeTruncation
for T +where + PathBuf: From
, +{ + fn truncate_loudly(self, destination: P) -> Result<()> { + match std::fs::File::options() + .append(false) + .create(true) + .truncate(true) + .write(true) + .open(PathBuf::from(destination)) + { + Err(e) => { + eprintln!("{e}"); + Err(e.into()) + } + Ok(mut file) => { + let bytes = self.to_string().as_bytes().to_vec(); + + match file.write(&bytes) { + Err(e) => { + eprintln!("{e}"); + Err(e.into()) + } + Ok(n) => { + if n == bytes.len() { + Ok(()) + } else { + eprintln!( + "Creating an exact copy was not possible." + ); + Err(sysexits::ExitCode::IoErr) + } + } + } + } + } + } + + fn truncate_silently(self, destination: P) -> Result<()> { + match std::fs::File::options() + .append(false) + .create(true) + .truncate(true) + .write(true) + .open(PathBuf::from(destination)) + { + Err(e) => Err(e.into()), + Ok(mut file) => { + let bytes = self.to_string().as_bytes().to_vec(); + + match file.write(&bytes) { + Err(e) => Err(e.into()), + Ok(n) => { + if n == bytes.len() { + Ok(()) + } else { + Err(sysexits::ExitCode::IoErr) + } + } + } + } + } + } +} + +/******************************************************************************/