clippy
[pspp] / rust / tests / sack.rs
index 407f2ec9a1a149d996a65dc9d0c6bf19438a9d33..49b10e77acc52b8c423f4a09ed60f72d44dd4266 100644 (file)
@@ -1,5 +1,4 @@
 use std::fs::read_to_string;
-use std::io::{stdout, IsTerminal, Write};
 use std::path::PathBuf;
 
 use anyhow::{anyhow, Result};
@@ -59,24 +58,36 @@ struct Args {
     le: bool,
 
     /// Input file.
-    #[arg(required = true)]
-    input: PathBuf,
+    #[arg(required = true, name = "input")]
+    input_file_name: PathBuf,
+
+    /// Output file.
+    #[arg(required = true, name = "output")]
+    output_file_name: PathBuf,
 }
 
 fn main() -> Result<()> {
-    let Args { be, le, input } = Args::parse();
-    if stdout().is_terminal() {
-        return Err(anyhow!(
-            "not writing binary data to a terminal; redirect to a file"
-        ));
-    }
+    let Args {
+        be,
+        le,
+        input_file_name,
+        output_file_name,
+    } = Args::parse();
     let endian = match (be, le) {
         (false, false) | (true, false) => Endian::Big,
         (false, true) => Endian::Little,
         (true, true) => return Err(anyhow!("can't use both `--be` and `--le`")),
     };
-    let input = read_to_string(&input)?;
-    let output = sack(&input, endian)?;
-    stdout().write(&output)?;
+
+    let input_file_str = input_file_name.to_string_lossy();
+    let input = read_to_string(&input_file_name)
+        .map_err(|err| anyhow!("{input_file_str}: read failed ({err})"))?;
+
+    let output = sack(&input, Some(&input_file_str), endian)?;
+
+    let output_file_str = output_file_name.to_string_lossy();
+    std::fs::write(&output_file_name, output)
+        .map_err(|err| anyhow!("{output_file_str}: write failed ({err})"))?;
+
     Ok(())
 }