diff options
author | Nora Widdecke <nora@sequoia-pgp.org> | 2022-08-26 00:23:57 +0200 |
---|---|---|
committer | Nora Widdecke <nora@sequoia-pgp.org> | 2022-09-07 16:07:54 +0200 |
commit | 9c9d3b9386d9df97a1efec71480b6a04dd991dd3 (patch) | |
tree | 002cedb89b0040ed6e25906095ef3cd189cf93bb | |
parent | 9667e62a21689e16468157ca22a75988359acd01 (diff) |
sq: Refactor helper function.
- create_or_stdout is deprecated and only used by Config, pull it into
impl Config.
-rw-r--r-- | sq/src/sq.rs | 61 |
1 files 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<Box<dyn io::Write + Sync + Send>> { - 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<Box<dyn io::Write + Sync + Send>> { - #[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<Box<dyn io::Write + Sync + Send>> { + 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 |