From 5af176c94f3e797d2d784b0c3df103a2921fd62d Mon Sep 17 00:00:00 2001 From: sharkdp Date: Thu, 11 Oct 2018 22:27:42 +0200 Subject: [PATCH] Move transpose to util module --- src/app.rs | 7 +------ src/main.rs | 1 + src/util.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/util.rs diff --git a/src/app.rs b/src/app.rs index 2c868df2..0d32bab7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -20,6 +20,7 @@ use errors::*; use inputfile::InputFile; use line_range::LineRange; use style::{OutputComponent, OutputComponents, OutputWrap}; +use util::transpose; #[derive(Debug, Clone, Copy, PartialEq)] pub enum PagingMode { @@ -74,12 +75,6 @@ fn is_truecolor_terminal() -> bool { .unwrap_or(false) } -/// Helper function that might appear in Rust stable at some point -/// (https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.transpose) -fn transpose(opt: Option>) -> Result> { - opt.map_or(Ok(None), |res| res.map(Some)) -} - pub struct App { pub matches: ArgMatches<'static>, interactive_output: bool, diff --git a/src/main.rs b/src/main.rs index f1fa5547..a5484193 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ mod preprocessor; mod printer; mod style; mod terminal; +mod util; use std::collections::HashSet; use std::io; diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 00000000..4675adbc --- /dev/null +++ b/src/util.rs @@ -0,0 +1,27 @@ +/// Helper function that might appear in Rust stable at some point +/// (https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.transpose) +pub fn transpose(opt: Option>) -> Result, E> { + opt.map_or(Ok(None), |res| res.map(Some)) +} + +#[cfg(test)] +mod tests { + use super::transpose; + + #[derive(Debug, PartialEq)] + struct TestError; + + type TestResult = Result; + + #[test] + fn basic() { + let a: Option> = Some(Ok(2)); + assert_eq!(Ok(Some(2)), transpose(a)); + + let b: Option> = Some(Err(TestError)); + assert_eq!(Err(TestError), transpose(b)); + + let c: Option> = None; + assert_eq!(Ok(None), transpose(c)); + } +}