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)); + } +}