From 041e499386490ca83f45fe124574aba5152c081c Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:09:42 +0200 Subject: [PATCH 01/11] [rustfmt: libterm] lib.rs: fix single line attribute --- src/libterm/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libterm/lib.rs b/src/libterm/lib.rs index 8bf8044f81431..7e332ac9aaa3a 100644 --- a/src/libterm/lib.rs +++ b/src/libterm/lib.rs @@ -64,7 +64,8 @@ #![feature(vec_push_all)] #![cfg_attr(windows, feature(libc))] -#[macro_use] extern crate log; +#[macro_use] +extern crate log; pub use terminfo::TerminfoTerminal; #[cfg(windows)] From d84f4f75eda6c5b7f66456c748330441c016cb21 Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:22:16 +0200 Subject: [PATCH 02/11] [rustfmt: libterm] lib.rs: missing coma in enum --- src/libterm/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libterm/lib.rs b/src/libterm/lib.rs index 7e332ac9aaa3a..efa1b4ab659ef 100644 --- a/src/libterm/lib.rs +++ b/src/libterm/lib.rs @@ -207,7 +207,7 @@ pub mod attr { /// Convenience attribute to set the foreground color ForegroundColor(super::color::Color), /// Convenience attribute to set the background color - BackgroundColor(super::color::Color) + BackgroundColor(super::color::Color), } } From 3992a26078ee356415976850e4a990f26ee1cc7a Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:23:10 +0200 Subject: [PATCH 03/11] [rustfmt: libterm] lib.rs: remove extra spaces I'm not sure about this one. Imo it makes it less readable. --- src/libterm/lib.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/libterm/lib.rs b/src/libterm/lib.rs index efa1b4ab659ef..a8ac458d31aeb 100644 --- a/src/libterm/lib.rs +++ b/src/libterm/lib.rs @@ -158,23 +158,23 @@ pub mod color { /// Number for a terminal color pub type Color = u16; - pub const BLACK: Color = 0; - pub const RED: Color = 1; - pub const GREEN: Color = 2; - pub const YELLOW: Color = 3; - pub const BLUE: Color = 4; + pub const BLACK: Color = 0; + pub const RED: Color = 1; + pub const GREEN: Color = 2; + pub const YELLOW: Color = 3; + pub const BLUE: Color = 4; pub const MAGENTA: Color = 5; - pub const CYAN: Color = 6; - pub const WHITE: Color = 7; + pub const CYAN: Color = 6; + pub const WHITE: Color = 7; - pub const BRIGHT_BLACK: Color = 8; - pub const BRIGHT_RED: Color = 9; - pub const BRIGHT_GREEN: Color = 10; - pub const BRIGHT_YELLOW: Color = 11; - pub const BRIGHT_BLUE: Color = 12; + pub const BRIGHT_BLACK: Color = 8; + pub const BRIGHT_RED: Color = 9; + pub const BRIGHT_GREEN: Color = 10; + pub const BRIGHT_YELLOW: Color = 11; + pub const BRIGHT_BLUE: Color = 12; pub const BRIGHT_MAGENTA: Color = 13; - pub const BRIGHT_CYAN: Color = 14; - pub const BRIGHT_WHITE: Color = 15; + pub const BRIGHT_CYAN: Color = 14; + pub const BRIGHT_WHITE: Color = 15; } /// Terminal attributes From 4f7c00b4d733f16768112093b2c20ae0a050022e Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:24:30 +0200 Subject: [PATCH 04/11] [rustfmt: libterm] lib.rs: fix multiline "new()" --- src/libterm/lib.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/libterm/lib.rs b/src/libterm/lib.rs index a8ac458d31aeb..e80c5c11d457b 100644 --- a/src/libterm/lib.rs +++ b/src/libterm/lib.rs @@ -101,25 +101,19 @@ impl Write for WriterWrapper { /// Return a Terminal wrapping stdout, or None if a terminal couldn't be /// opened. pub fn stdout() -> Option + Send>> { - TerminfoTerminal::new(WriterWrapper { - wrapped: box std::io::stdout(), - }) + TerminfoTerminal::new(WriterWrapper { wrapped: box std::io::stdout() }) } #[cfg(windows)] /// Return a Terminal wrapping stdout, or None if a terminal couldn't be /// opened. pub fn stdout() -> Option + Send>> { - let ti = TerminfoTerminal::new(WriterWrapper { - wrapped: box std::io::stdout(), - }); + let ti = TerminfoTerminal::new(WriterWrapper { wrapped: box std::io::stdout() }); match ti { Some(t) => Some(t), None => { - WinConsole::new(WriterWrapper { - wrapped: box std::io::stdout(), - }) + WinConsole::new(WriterWrapper { wrapped: box std::io::stdout() }) } } } @@ -128,25 +122,19 @@ pub fn stdout() -> Option + Send>> { /// Return a Terminal wrapping stderr, or None if a terminal couldn't be /// opened. pub fn stderr() -> Option + Send>> { - TerminfoTerminal::new(WriterWrapper { - wrapped: box std::io::stderr(), - }) + TerminfoTerminal::new(WriterWrapper { wrapped: box std::io::stderr() }) } #[cfg(windows)] /// Return a Terminal wrapping stderr, or None if a terminal couldn't be /// opened. pub fn stderr() -> Option + Send>> { - let ti = TerminfoTerminal::new(WriterWrapper { - wrapped: box std::io::stderr(), - }); + let ti = TerminfoTerminal::new(WriterWrapper { wrapped: box std::io::stderr() }); match ti { Some(t) => Some(t), None => { - WinConsole::new(WriterWrapper { - wrapped: box std::io::stderr(), - }) + WinConsole::new(WriterWrapper { wrapped: box std::io::stderr() }) } } } From 2bb4b7a37d99a42aed86289bd6017b824591d0a3 Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:27:59 +0200 Subject: [PATCH 05/11] [rustfmt libterm] terminfo/mod.rs: fix single line functions --- src/libterm/terminfo/mod.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/libterm/terminfo/mod.rs b/src/libterm/terminfo/mod.rs index ffe896b93a718..d759b2a5fe9d5 100644 --- a/src/libterm/terminfo/mod.rs +++ b/src/libterm/terminfo/mod.rs @@ -160,13 +160,19 @@ impl Terminal for TerminfoTerminal { Ok(()) } - fn get_ref<'a>(&'a self) -> &'a T { &self.out } + fn get_ref<'a>(&'a self) -> &'a T { + &self.out + } - fn get_mut<'a>(&'a mut self) -> &'a mut T { &mut self.out } + fn get_mut<'a>(&'a mut self) -> &'a mut T { + &mut self.out + } } impl UnwrappableTerminal for TerminfoTerminal { - fn unwrap(self) -> T { self.out } + fn unwrap(self) -> T { + self.out + } } impl TerminfoTerminal { From f40ccb23d34a437b7ef6b739f6b7253a920249e7 Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:32:36 +0200 Subject: [PATCH 06/11] [rustfmt libterm] terminfo/mod.rs: basic fixes - remove extra spaces, or add some when necessary - split single lines into multiple lines - fix indentation - add missing commas --- src/libterm/terminfo/mod.rs | 53 ++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/libterm/terminfo/mod.rs b/src/libterm/terminfo/mod.rs index d759b2a5fe9d5..e63e639351197 100644 --- a/src/libterm/terminfo/mod.rs +++ b/src/libterm/terminfo/mod.rs @@ -28,13 +28,13 @@ use self::parm::{expand, Number, Variables}; #[derive(Debug)] pub struct TermInfo { /// Names for the terminal - pub names: Vec , + pub names: Vec, /// Map of capability name to boolean value pub bools: HashMap, /// Map of capability name to numeric value pub numbers: HashMap, /// Map of capability name to raw (unexpanded) string - pub strings: HashMap > + pub strings: HashMap>, } pub mod searcher; @@ -61,7 +61,7 @@ fn cap_for_attr(attr: attr::Attr) -> &'static str { attr::Reverse => "rev", attr::Secure => "invis", attr::ForegroundColor(_) => "setaf", - attr::BackgroundColor(_) => "setab" + attr::BackgroundColor(_) => "setab", } } @@ -70,7 +70,7 @@ fn cap_for_attr(attr: attr::Attr) -> &'static str { pub struct TerminfoTerminal { num_colors: u16, out: T, - ti: Box + ti: Box, } impl Terminal for TerminfoTerminal { @@ -80,9 +80,9 @@ impl Terminal for TerminfoTerminal { let s = expand(self.ti .strings .get("setaf") - .unwrap() - , - &[Number(color as isize)], &mut Variables::new()); + .unwrap(), + &[Number(color as isize)], + &mut Variables::new()); if s.is_ok() { try!(self.out.write_all(&s.unwrap())); return Ok(true) @@ -97,9 +97,9 @@ impl Terminal for TerminfoTerminal { let s = expand(self.ti .strings .get("setab") - .unwrap() - , - &[Number(color as isize)], &mut Variables::new()); + .unwrap(), + &[Number(color as isize)], + &mut Variables::new()); if s.is_ok() { try!(self.out.write_all(&s.unwrap())); return Ok(true) @@ -116,9 +116,7 @@ impl Terminal for TerminfoTerminal { let cap = cap_for_attr(attr); let parm = self.ti.strings.get(cap); if parm.is_some() { - let s = expand(parm.unwrap(), - &[], - &mut Variables::new()); + let s = expand(parm.unwrap(), &[], &mut Variables::new()); if s.is_ok() { try!(self.out.write_all(&s.unwrap())); return Ok(true) @@ -192,16 +190,16 @@ impl TerminfoTerminal { Err(err) => return match env::var("MSYSCON") { Ok(ref val) if &val[..] == "mintty.exe" => { // msys terminal - Some(box TerminfoTerminal{ + Some(box TerminfoTerminal { out: out, ti: msys_terminfo(), num_colors: 8, }) - }, + } _ => { debug!("error finding terminfo entry: {:?}", err); None - }, + } }, }; @@ -212,20 +210,25 @@ impl TerminfoTerminal { } let inf = ti.unwrap(); - let nc = if inf.strings.get("setaf").is_some() - && inf.strings.get("setab").is_some() { - inf.numbers.get("colors").map_or(0, |&n| n) - } else { 0 }; - - Some(box TerminfoTerminal {out: out, - ti: inf, - num_colors: nc}) + let nc = if inf.strings.get("setaf").is_some() && inf.strings.get("setab").is_some() { + inf.numbers.get("colors").map_or(0, |&n| n) + } else { + 0 + }; + + Some(box TerminfoTerminal { + out: out, + ti: inf, + num_colors: nc, + }) } fn dim_if_necessary(&self, color: color::Color) -> color::Color { if color >= self.num_colors && color >= 8 && color < 16 { color-8 - } else { color } + } else { + color + } } } From 72392fcfb2ccfa53da098fbd829169befacfb314 Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:35:41 +0200 Subject: [PATCH 07/11] [rustfmt libterm] terminfo/mod.rs: add ";" to return statements --- src/libterm/terminfo/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libterm/terminfo/mod.rs b/src/libterm/terminfo/mod.rs index e63e639351197..c1d6e29112c95 100644 --- a/src/libterm/terminfo/mod.rs +++ b/src/libterm/terminfo/mod.rs @@ -85,7 +85,7 @@ impl Terminal for TerminfoTerminal { &mut Variables::new()); if s.is_ok() { try!(self.out.write_all(&s.unwrap())); - return Ok(true) + return Ok(true); } } Ok(false) @@ -102,7 +102,7 @@ impl Terminal for TerminfoTerminal { &mut Variables::new()); if s.is_ok() { try!(self.out.write_all(&s.unwrap())); - return Ok(true) + return Ok(true); } } Ok(false) @@ -119,7 +119,7 @@ impl Terminal for TerminfoTerminal { let s = expand(parm.unwrap(), &[], &mut Variables::new()); if s.is_ok() { try!(self.out.write_all(&s.unwrap())); - return Ok(true) + return Ok(true); } } Ok(false) @@ -153,7 +153,7 @@ impl Terminal for TerminfoTerminal { expand(op, &[], &mut Variables::new()) }); if s.is_ok() { - return self.out.write_all(&s.unwrap()) + return self.out.write_all(&s.unwrap()); } Ok(()) } From 4f8bba145ed037c060af3de49b1f8b2279a44f81 Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:36:55 +0200 Subject: [PATCH 08/11] [rustfmt libterm] terminfo/mod.rs: [UNSURE] line split Not sure if this is very readable? --- src/libterm/terminfo/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libterm/terminfo/mod.rs b/src/libterm/terminfo/mod.rs index c1d6e29112c95..dc36cf2e06c0d 100644 --- a/src/libterm/terminfo/mod.rs +++ b/src/libterm/terminfo/mod.rs @@ -149,9 +149,8 @@ impl Terminal for TerminfoTerminal { cap = self.ti.strings.get("op"); } } - let s = cap.map_or(Err("can't find terminfo capability `sgr0`".to_owned()), |op| { - expand(op, &[], &mut Variables::new()) - }); + let s = cap.map_or(Err("can't find terminfo capability `sgr0`".to_owned()), + |op| expand(op, &[], &mut Variables::new())); if s.is_ok() { return self.out.write_all(&s.unwrap()); } From 27f9bdca388001e589f8d03b4f07f46ae2895754 Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:38:17 +0200 Subject: [PATCH 09/11] [rustfmt libterm] terminfo/mod.rs: [UNSURE] alignment --- src/libterm/terminfo/mod.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/libterm/terminfo/mod.rs b/src/libterm/terminfo/mod.rs index dc36cf2e06c0d..b8fda21b95881 100644 --- a/src/libterm/terminfo/mod.rs +++ b/src/libterm/terminfo/mod.rs @@ -49,17 +49,17 @@ pub mod parm; fn cap_for_attr(attr: attr::Attr) -> &'static str { match attr { - attr::Bold => "bold", - attr::Dim => "dim", - attr::Italic(true) => "sitm", - attr::Italic(false) => "ritm", - attr::Underline(true) => "smul", - attr::Underline(false) => "rmul", - attr::Blink => "blink", - attr::Standout(true) => "smso", - attr::Standout(false) => "rmso", - attr::Reverse => "rev", - attr::Secure => "invis", + attr::Bold => "bold", + attr::Dim => "dim", + attr::Italic(true) => "sitm", + attr::Italic(false) => "ritm", + attr::Underline(true) => "smul", + attr::Underline(false) => "rmul", + attr::Blink => "blink", + attr::Standout(true) => "smso", + attr::Standout(false) => "rmso", + attr::Reverse => "rev", + attr::Secure => "invis", attr::ForegroundColor(_) => "setaf", attr::BackgroundColor(_) => "setab", } From 440ac93a2bd16a7aa6f9f79d710ae6d4f930fa31 Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 20:46:43 +0200 Subject: [PATCH 10/11] [rustfmt libterm] terminfo/parm.rs: comas --- src/libterm/terminfo/parm.rs | 41 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/libterm/terminfo/parm.rs b/src/libterm/terminfo/parm.rs index 9110be33907b0..cb6e448ea29c0 100644 --- a/src/libterm/terminfo/parm.rs +++ b/src/libterm/terminfo/parm.rs @@ -32,14 +32,14 @@ enum States { SeekIfElse(isize), SeekIfElsePercent(isize), SeekIfEnd(isize), - SeekIfEndPercent(isize) + SeekIfEndPercent(isize), } #[derive(Copy, Clone, PartialEq)] enum FormatState { FormatStateFlags, FormatStateWidth, - FormatStatePrecision + FormatStatePrecision, } /// Types of parameters a capability can use @@ -47,7 +47,7 @@ enum FormatState { #[derive(Clone)] pub enum Param { Words(String), - Number(isize) + Number(isize), } /// Container for static and dynamic variable arrays @@ -55,7 +55,7 @@ pub struct Variables { /// Static variables A-Z sta: [Param; 26], /// Dynamic variables a-z - dyn: [Param; 26] + dyn: [Param; 26], } impl Variables { @@ -119,10 +119,13 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) } else { output.push(c); } - }, + } Percent => { match cur { - '%' => { output.push(c); state = Nothing }, + '%' => { + output.push(c); + state = Nothing + } 'c' => if !stack.is_empty() { match stack.pop().unwrap() { // if c is 0, use 0200 (128) for ncurses compatibility @@ -133,7 +136,7 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) c as u8 }) } - _ => return Err("a non-char was used with %c".to_owned()) + _ => return Err("a non-char was used with %c".to_owned()), } } else { return Err("stack is empty".to_owned()) }, 'p' => state = PushParam, @@ -271,7 +274,7 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) flags.width = cur as usize - '0' as usize; fstate = FormatStateWidth; } - _ => unreachable!() + _ => unreachable!(), } state = FormatPattern(flags, fstate); } @@ -293,14 +296,14 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) return Err(format!("unrecognized format option {:?}", cur)) } } - }, + } PushParam => { // params are 1-indexed stack.push(mparams[match cur.to_digit(10) { Some(d) => d as usize - 1, None => return Err("bad param number".to_owned()) }].clone()); - }, + } SetVar => { if cur >= 'A' && cur <= 'Z' { if !stack.is_empty() { @@ -315,7 +318,7 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) } else { return Err("bad variable name in %P".to_owned()); } - }, + } GetVar => { if cur >= 'A' && cur <= 'Z' { let idx = (cur as u8) - b'A'; @@ -326,16 +329,16 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) } else { return Err("bad variable name in %g".to_owned()); } - }, + } CharConstant => { stack.push(Number(c as isize)); state = CharClose; - }, + } CharClose => { if cur != '\'' { return Err("malformed character constant".to_owned()); } - }, + } IntConstant(i) => { match cur { '}' => { @@ -451,7 +454,7 @@ struct Flags { alternate: bool, left: bool, sign: bool, - space: bool + space: bool, } impl Flags { @@ -467,7 +470,7 @@ enum FormatOp { FormatOctal, FormatHex, FormatHEX, - FormatString + FormatString, } impl FormatOp { @@ -478,7 +481,7 @@ impl FormatOp { 'x' => FormatHex, 'X' => FormatHEX, 's' => FormatString, - _ => panic!("bad FormatOp char") + _ => panic!("bad FormatOp char"), } } fn to_char(self) -> char { @@ -487,7 +490,7 @@ impl FormatOp { FormatOctal => 'o', FormatHex => 'x', FormatHEX => 'X', - FormatString => 's' + FormatString => 's', } } } @@ -538,7 +541,7 @@ fn format(val: Param, op: FormatOp, flags: Flags) -> Result ,String> { s.extend(s_); } } - FormatString => unreachable!() + FormatString => unreachable!(), } s } From 8969bb20048db2a2f9c5c55fb7141018a64d897e Mon Sep 17 00:00:00 2001 From: corentih Date: Sat, 24 Oct 2015 21:28:37 +0200 Subject: [PATCH 11/11] [rustfmt libterm] terminfo/parm.rs lines --- src/libterm/terminfo/parm.rs | 152 ++++++++++++++++++++++++----------- 1 file changed, 106 insertions(+), 46 deletions(-) diff --git a/src/libterm/terminfo/parm.rs b/src/libterm/terminfo/parm.rs index cb6e448ea29c0..daedba86f89d0 100644 --- a/src/libterm/terminfo/parm.rs +++ b/src/libterm/terminfo/parm.rs @@ -91,8 +91,7 @@ impl Variables { /// /// To be compatible with ncurses, `vars` should be the same between calls to `expand` for /// multiple capabilities for the same terminal. -pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) - -> Result , String> { +pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) -> Result, String> { let mut state = Nothing; // expanded cap will only rarely be larger than the cap itself @@ -149,76 +148,100 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) Words(s) => stack.push(Number(s.len() as isize)), _ => return Err("a non-str was used with %l".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '+' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(x + y)), _ => return Err("non-numbers on stack with +".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '-' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(x - y)), _ => return Err("non-numbers on stack with -".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '*' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(x * y)), _ => return Err("non-numbers on stack with *".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '/' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(x / y)), _ => return Err("non-numbers on stack with /".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, 'm' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(x % y)), _ => return Err("non-numbers on stack with %".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '&' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(x & y)), _ => return Err("non-numbers on stack with &".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '|' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(x | y)), _ => return Err("non-numbers on stack with |".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '^' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(x ^ y)), _ => return Err("non-numbers on stack with ^".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '=' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(if x == y { 1 } else { 0 })), _ => return Err("non-numbers on stack with =".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '>' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(if x > y { 1 } else { 0 })), _ => return Err("non-numbers on stack with >".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '<' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(y), Number(x)) => stack.push(Number(if x < y { 1 } else { 0 })), _ => return Err("non-numbers on stack with <".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, 'A' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(0), Number(_)) => stack.push(Number(0)), @@ -226,27 +249,35 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) (Number(_), Number(_)) => stack.push(Number(1)), _ => return Err("non-numbers on stack with logical and".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, 'O' => if stack.len() > 1 { match (stack.pop().unwrap(), stack.pop().unwrap()) { (Number(0), Number(0)) => stack.push(Number(0)), (Number(_), Number(_)) => stack.push(Number(1)), _ => return Err("non-numbers on stack with logical or".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '!' => if !stack.is_empty() { match stack.pop().unwrap() { Number(0) => stack.push(Number(1)), Number(_) => stack.push(Number(0)), _ => return Err("non-number on stack with logical not".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, '~' => if !stack.is_empty() { match stack.pop().unwrap() { Number(x) => stack.push(Number(!x)), _ => return Err("non-number on stack with %~".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, 'i' => match (mparams[0].clone(), mparams[1].clone()) { (Number(x), Number(y)) => { mparams[0] = Number(x+1); @@ -259,10 +290,14 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) 'd'|'o'|'x'|'X'|'s' => if !stack.is_empty() { let flags = Flags::new(); let res = format(stack.pop().unwrap(), FormatOp::from_char(cur), flags); - if res.is_err() { return res } + if res.is_err() { + return res; + } output.push_all(&res.unwrap()) - } else { return Err("stack is empty".to_owned()) }, - ':'|'#'|' '|'.'|'0'...'9' => { + } else { + return Err("stack is empty".to_owned()); + }, + ':' | '#' | ' ' | '.' | '0'...'9' => { let mut flags = Flags::new(); let mut fstate = FormatStateFlags; match cur { @@ -288,7 +323,9 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) _ => return Err("non-number on stack \ with conditional".to_owned()) } - } else { return Err("stack is empty".to_owned()) }, + } else { + return Err("stack is empty".to_owned()); + }, 'e' => state = SeekIfEnd(0), ';' => (), @@ -309,12 +346,16 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) if !stack.is_empty() { let idx = (cur as u8) - b'A'; vars.sta[idx as usize] = stack.pop().unwrap(); - } else { return Err("stack is empty".to_owned()) } + } else { + return Err("stack is empty".to_owned()); + } } else if cur >= 'a' && cur <= 'z' { if !stack.is_empty() { let idx = (cur as u8) - b'a'; vars.dyn[idx as usize] = stack.pop().unwrap(); - } else { return Err("stack is empty".to_owned()) } + } else { + return Err("stack is empty".to_owned()); + } } else { return Err("bad variable name in %P".to_owned()); } @@ -357,12 +398,16 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) match (*fstate, cur) { (_,'d')|(_,'o')|(_,'x')|(_,'X')|(_,'s') => if !stack.is_empty() { let res = format(stack.pop().unwrap(), FormatOp::from_char(cur), *flags); - if res.is_err() { return res } + if res.is_err() { + return res; + } output.push_all(&res.unwrap()); // will cause state to go to Nothing old_state = FormatPattern(*flags, *fstate); - } else { return Err("stack is empty".to_owned()) }, - (FormatStateFlags,'#') => { + } else { + return Err("stack is empty".to_owned()); + }, + (FormatStateFlags, '#') => { flags.alternate = true; } (FormatStateFlags,'-') => { @@ -384,7 +429,9 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) (FormatStateWidth,'0'...'9') => { let old = flags.width; flags.width = flags.width * 10 + (cur as usize - '0' as usize); - if flags.width < old { return Err("format width overflow".to_owned()) } + if flags.width < old { + return Err("format width overflow".to_owned()); + } } (FormatStateWidth,'.') => { *fstate = FormatStatePrecision; @@ -459,8 +506,14 @@ struct Flags { impl Flags { fn new() -> Flags { - Flags{ width: 0, precision: 0, alternate: false, - left: false, sign: false, space: false } + Flags { + width: 0, + precision: 0, + alternate: false, + left: false, + sign: false, + space: false, + } } } @@ -555,8 +608,7 @@ fn format(val: Param, op: FormatOp, flags: Flags) -> Result ,String> { s } _ => { - return Err(format!("non-string on stack with %{:?}", - op.to_char())) + return Err(format!("non-string on stack with %{:?}", op.to_char())); } } } @@ -597,7 +649,8 @@ mod tests { fn test_op_i() { let mut vars = Variables::new(); assert_eq!(expand(b"%p1%d%p2%d%p3%d%i%p1%d%p2%d%p3%d", - &[Number(1),Number(2),Number(3)], &mut vars), + &[Number(1), Number(2), Number(3)], + &mut vars), Ok("123233".bytes().collect::>())); assert_eq!(expand(b"%p1%d%p2%d%i%p1%d%p2%d", &[], &mut vars), Ok("0011".bytes().collect::>())); @@ -619,7 +672,8 @@ mod tests { for &cap in &caps { let res = get_res("", cap, &[], vars); assert!(res.is_err(), - "Op {} succeeded incorrectly with 0 stack entries", cap); + "Op {} succeeded incorrectly with 0 stack entries", + cap); let p = if cap == "%s" || cap == "%l" { Words("foo".to_string()) } else { @@ -627,19 +681,25 @@ mod tests { }; let res = get_res("%p1", cap, &[p], vars); assert!(res.is_ok(), - "Op {} failed with 1 stack entry: {}", cap, res.err().unwrap()); + "Op {} failed with 1 stack entry: {}", + cap, + res.err().unwrap()); } let caps = ["%+", "%-", "%*", "%/", "%m", "%&", "%|", "%A", "%O"]; for &cap in &caps { let res = expand(cap.as_bytes(), &[], vars); assert!(res.is_err(), - "Binop {} succeeded incorrectly with 0 stack entries", cap); + "Binop {} succeeded incorrectly with 0 stack entries", + cap); let res = get_res("%{1}", cap, &[], vars); assert!(res.is_err(), - "Binop {} succeeded incorrectly with 1 stack entry", cap); + "Binop {} succeeded incorrectly with 1 stack entry", + cap); let res = get_res("%{1}%{2}", cap, &[], vars); assert!(res.is_ok(), - "Binop {} failed with 2 stack entries: {:?}", cap, res.err().unwrap()); + "Binop {} failed with 2 stack entries: {:?}", + cap, + res.err().unwrap()); } } @@ -673,16 +733,13 @@ mod tests { let s = b"\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m"; let res = expand(s, &[Number(1)], &mut vars); assert!(res.is_ok(), res.err().unwrap()); - assert_eq!(res.unwrap(), - "\\E[31m".bytes().collect::>()); + assert_eq!(res.unwrap(), "\\E[31m".bytes().collect::>()); let res = expand(s, &[Number(8)], &mut vars); assert!(res.is_ok(), res.err().unwrap()); - assert_eq!(res.unwrap(), - "\\E[90m".bytes().collect::>()); + assert_eq!(res.unwrap(), "\\E[90m".bytes().collect::>()); let res = expand(s, &[Number(42)], &mut vars); assert!(res.is_ok(), res.err().unwrap()); - assert_eq!(res.unwrap(), - "\\E[38;5;42m".bytes().collect::>()); + assert_eq!(res.unwrap(), "\\E[38;5;42m".bytes().collect::>()); } #[test] @@ -693,14 +750,17 @@ mod tests { &[Words("foo".to_string()), Words("foo".to_string()), Words("f".to_string()), - Words("foo".to_string())], vars), + Words("foo".to_string())], + vars), Ok("foofoo ffo".bytes().collect::>())); assert_eq!(expand(b"%p1%:-4.2s", &[Words("foo".to_owned())], vars), Ok("fo ".bytes().collect::>())); assert_eq!(expand(b"%p1%d%p1%.3d%p1%5d%p1%:+d", &[Number(1)], vars), Ok("1001 1+1".bytes().collect::>())); - assert_eq!(expand(b"%p1%o%p1%#o%p2%6.4x%p2%#6.4X", &[Number(15), Number(27)], vars), + assert_eq!(expand(b"%p1%o%p1%#o%p2%6.4x%p2%#6.4X", + &[Number(15), Number(27)], + vars), Ok("17017 001b0X001B".bytes().collect::>())); } }