From 9c9d3b9386d9df97a1efec71480b6a04dd991dd3 Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Fri, 26 Aug 2022 00:23:57 +0200 Subject: sq: Refactor helper function. - create_or_stdout is deprecated and only used by Config, pull it into impl Config. --- sq/src/sq.rs | 61 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/sq/src/sq.rs b/sq/src/sq.rs index 0b8d7cd7..79b17b81 100644 --- a/sq/src/sq.rs +++ b/sq/src/sq.rs @@ -46,30 +46,6 @@ fn open_or_stdin(f: Option<&str>) } } -#[deprecated(note = "Use the appropriate function on Config instead")] -fn create_or_stdout(f: Option<&str>, force: bool) - -> Result> { - match f { - None => Ok(Box::new(io::stdout())), - Some(p) if p == "-" => Ok(Box::new(io::stdout())), - Some(f) => { - let p = Path::new(f); - if !p.exists() || force { - Ok(Box::new(OpenOptions::new() - .write(true) - .truncate(true) - .create(true) - .open(f) - .context("Failed to create output file")?)) - } else { - Err(anyhow::anyhow!( - format!("File {:?} exists, use \"sq --force ...\" to \ - overwrite", p))) - } - } - } -} - const SECONDS_IN_DAY : u64 = 24 * 60 * 60; const SECONDS_IN_YEAR : u64 = // Average number of days in a year. @@ -355,8 +331,7 @@ impl Config<'_> { /// authenticated payloads. fn create_or_stdout_safe(&self, f: Option<&str>) -> Result> { - #[allow(deprecated)] - create_or_stdout(f, self.force) + Config::create_or_stdout(f, self.force) } /// Opens the file (or stdout) for writing data that is NOT safe @@ -370,8 +345,7 @@ impl Config<'_> { emit_unstable_cli_warning(); self.unstable_cli_warning_emitted = true; } - #[allow(deprecated)] - create_or_stdout(f, self.force) + Config::create_or_stdout(f, self.force) } /// Opens the file (or stdout) for writing data that is safe for @@ -386,6 +360,37 @@ impl Config<'_> { } Ok(message) } + + /// Helper function, do not use directly. Instead, use create_or_stdout_safe + /// or create_or_stdout_unsafe. + fn create_or_stdout( + f: Option<&str>, + force: bool, + ) -> Result> { + match f { + None => Ok(Box::new(io::stdout())), + Some(p) if p == "-" => Ok(Box::new(io::stdout())), + Some(f) => { + let p = Path::new(f); + if !p.exists() || force { + Ok(Box::new( + OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(f) + .context("Failed to create output file")?, + )) + } else { + Err(anyhow::anyhow!(format!( + "File {:?} exists, use \"sq --force ...\" to \ + overwrite", + p + ))) + } + } + } + } } // TODO: Use `derive`d command structs. No more values_of -- cgit v1.2.3