From 439c0eba59bf217d592f6723553bb0c4b2c427ab Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 20 Jul 2018 13:10:46 -0700 Subject: [PATCH] shrink some more enums --- src/escape/mod.rs | 53 +++++++++++++++++++++++++--------------- src/escape/osc.rs | 3 --- src/escape/parser/mod.rs | 26 ++++++++++++-------- 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/escape/mod.rs b/src/escape/mod.rs index 44227bd60..c7cc66010 100644 --- a/src/escape/mod.rs +++ b/src/escape/mod.rs @@ -2,7 +2,6 @@ //! semantic meaning to them. It can also encode the semantic values as //! escape sequences. It provides encoding and decoding functionality //! only; it does not provide terminal emulation facilities itself. -use num; use std::fmt::{Display, Error as FmtError, Formatter, Write as FmtWrite}; pub mod csi; @@ -20,13 +19,13 @@ pub enum Action { /// Send a single printable character to the display Print(char), /// A C0 or C1 control code - Control(Control), + Control(ControlCode), /// Device control. This is uncommon wrt. terminal emulation. - DeviceControl(DeviceControlMode), + DeviceControl(Box), /// A command that typically doesn't change the contents of the /// terminal, but rather influences how it displays or otherwise /// interacts with the rest of the system - OperatingSystemCommand(OperatingSystemCommand), + OperatingSystemCommand(Box), CSI(CSI), Esc(Esc), } @@ -37,8 +36,7 @@ impl Display for Action { fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { match self { Action::Print(c) => write!(f, "{}", c), - Action::Control(Control::Code(c)) => f.write_char(*c as u8 as char), - Action::Control(Control::Unspecified(c)) => f.write_char(*c as char), + Action::Control(c) => f.write_char(*c as u8 as char), Action::DeviceControl(_) => unimplemented!(), Action::OperatingSystemCommand(osc) => osc.fmt(f), Action::CSI(csi) => csi.fmt(f), @@ -104,19 +102,34 @@ pub enum ControlCode { GroupSeparator = 0x1d, RecordSeparator = 0x1e, UnitSeparator = 0x1f, -} -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum Control { - Code(ControlCode), - Unspecified(u8), -} - -impl From for Control { - fn from(b: u8) -> Self { - match num::FromPrimitive::from_u8(b) { - Some(result) => Control::Code(result), - None => Control::Unspecified(b), - } - } + // C1 8-bit values + BPH = 0x82, + NBH = 0x83, + NEL = 0x85, + SSA = 0x86, + ESA = 0x87, + HTS = 0x88, + HTJ = 0x89, + VTS = 0x8a, + PLD = 0x8b, + PLU = 0x8c, + RI = 0x8d, + SS2 = 0x8e, + SS3 = 0x8f, + DCS = 0x90, + PU1 = 0x91, + PU2 = 0x92, + STS = 0x93, + CCH = 0x94, + MW = 0x95, + SPA = 0x96, + EPA = 0x97, + SOS = 0x98, + SCI = 0x9a, + CSI = 0x9b, + ST = 0x9c, + OSC = 0x9d, + PM = 0x9e, + APC = 0x9f, } diff --git a/src/escape/osc.rs b/src/escape/osc.rs index 4a512fa51..cb5de547d 100644 --- a/src/escape/osc.rs +++ b/src/escape/osc.rs @@ -11,8 +11,6 @@ pub enum OperatingSystemCommand { SetHyperlink(Option), Unspecified(Vec>), - #[doc(hidden)] - __Nonexhaustive, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -191,7 +189,6 @@ impl Display for OperatingSystemCommand { //f.write_all(item.as_slice())?; } } - __Nonexhaustive => {} }; write!(f, "\x07")?; Ok(()) diff --git a/src/escape/parser/mod.rs b/src/escape/parser/mod.rs index 3ad722cef..d35d3b3cd 100644 --- a/src/escape/parser/mod.rs +++ b/src/escape/parser/mod.rs @@ -1,4 +1,5 @@ use escape::{Action, DeviceControlMode, Esc, OperatingSystemCommand, CSI}; +use num; use vte; /// The `Parser` struct holds the state machine that is used to decode @@ -44,28 +45,33 @@ impl<'a, F: FnMut(Action)> vte::Perform for Performer<'a, F> { } fn execute(&mut self, byte: u8) { - (self.callback)(Action::Control(byte.into())); + match num::FromPrimitive::from_u8(byte) { + Some(code) => (self.callback)(Action::Control(code)), + None => eprintln!("impossible C0/C1 control code {:?} was dropped", byte), + } } fn hook(&mut self, params: &[i64], intermediates: &[u8], ignored_extra_intermediates: bool) { - (self.callback)(Action::DeviceControl(DeviceControlMode::Enter { + (self.callback)(Action::DeviceControl(Box::new(DeviceControlMode::Enter { params: params.to_vec(), intermediates: intermediates.to_vec(), ignored_extra_intermediates, - })); + }))); } fn put(&mut self, data: u8) { - (self.callback)(Action::DeviceControl(DeviceControlMode::Data(data))); + (self.callback)(Action::DeviceControl(Box::new(DeviceControlMode::Data( + data, + )))); } fn unhook(&mut self) { - (self.callback)(Action::DeviceControl(DeviceControlMode::Exit)); + (self.callback)(Action::DeviceControl(Box::new(DeviceControlMode::Exit))); } fn osc_dispatch(&mut self, osc: &[&[u8]]) { let osc = OperatingSystemCommand::parse(osc); - (self.callback)(Action::OperatingSystemCommand(osc)); + (self.callback)(Action::OperatingSystemCommand(Box::new(osc))); } fn csi_dispatch( @@ -169,18 +175,18 @@ mod test { let mut p = Parser::new(); let actions = p.parse_as_vec(b"\x1b]0;hello\x07"); assert_eq!( - vec![Action::OperatingSystemCommand( + vec![Action::OperatingSystemCommand(Box::new( OperatingSystemCommand::SetIconNameAndWindowTitle("hello".to_owned()), - )], + ))], actions ); assert_eq!(encode(&actions), "\x1b]0;hello\x07"); let actions = p.parse_as_vec(b"\x1b]532534523;hello\x07"); assert_eq!( - vec![Action::OperatingSystemCommand( + vec![Action::OperatingSystemCommand(Box::new( OperatingSystemCommand::Unspecified(vec![b"532534523".to_vec(), b"hello".to_vec()]), - )], + ))], actions ); assert_eq!(encode(&actions), "\x1b]532534523;hello\x07");