From b55d626b330845dbeb105b2e6b5f9d28ec03ebf3 Mon Sep 17 00:00:00 2001 From: Bugen Zhao Date: Tue, 19 Sep 2023 17:53:16 +0800 Subject: [PATCH] use bool instead of OnOff Signed-off-by: Bugen Zhao --- sqllogictest/src/parser.rs | 52 ++++++++++++++++++++------------------ sqllogictest/src/runner.rs | 4 +-- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/sqllogictest/src/parser.rs b/sqllogictest/src/parser.rs index f894190..940afdc 100644 --- a/sqllogictest/src/parser.rs +++ b/sqllogictest/src/parser.rs @@ -294,39 +294,41 @@ impl std::fmt::Display for Record { } } -/// Whether to enable or disable a feature. Used in `control` statements. #[derive(Debug, PartialEq, Eq, Clone)] -pub enum OnOff { - On, - Off, +#[non_exhaustive] +pub enum Control { + /// Control sort mode. + SortMode(SortMode), + /// Control whether or not to substitute variables in the SQL. + Substitution(bool), } -impl OnOff { - pub fn try_from_str(s: &str) -> Result { +trait ControlItem: Sized { + /// Try to parse from string. + fn try_from_str(s: &str) -> Result; + + /// Convert to string. + fn as_str(&self) -> &'static str; +} + +impl ControlItem for bool { + fn try_from_str(s: &str) -> Result { match s { - "on" => Ok(Self::On), - "off" => Ok(Self::Off), + "on" => Ok(true), + "off" => Ok(false), _ => Err(ParseErrorKind::InvalidControl(s.to_string())), } } - pub fn as_str(&self) -> &'static str { - match self { - Self::On => "on", - Self::Off => "off", + fn as_str(&self) -> &'static str { + if *self { + "on" + } else { + "off" } } } -#[derive(Debug, PartialEq, Eq, Clone)] -#[non_exhaustive] -pub enum Control { - /// Control sort mode. - SortMode(SortMode), - /// Control whether or not to substitute variables in the SQL. - Substitution(OnOff), -} - #[derive(Debug, PartialEq, Eq, Clone)] pub enum Injected { /// Pseudo control command to indicate the begin of an include statement. Automatically @@ -388,8 +390,8 @@ pub enum SortMode { ValueSort, } -impl SortMode { - pub fn try_from_str(s: &str) -> Result { +impl ControlItem for SortMode { + fn try_from_str(s: &str) -> Result { match s { "nosort" => Ok(Self::NoSort), "rowsort" => Ok(Self::RowSort), @@ -398,7 +400,7 @@ impl SortMode { } } - pub fn as_str(&self) -> &'static str { + fn as_str(&self) -> &'static str { match self { Self::NoSort => "nosort", Self::RowSort => "rowsort", @@ -679,7 +681,7 @@ fn parse_inner(loc: &Location, script: &str) -> Result records.push(Record::Control(Control::SortMode(sort_mode))), Err(k) => return Err(k.at(loc)), }, - ["substitution", on_off] => match OnOff::try_from_str(on_off) { + ["substitution", on_off] => match bool::try_from_str(on_off) { Ok(on_off) => records.push(Record::Control(Control::Substitution(on_off))), Err(k) => return Err(k.at(loc)), }, diff --git a/sqllogictest/src/runner.rs b/sqllogictest/src/runner.rs index 2799151..67252ec 100644 --- a/sqllogictest/src/runner.rs +++ b/sqllogictest/src/runner.rs @@ -723,8 +723,8 @@ impl> Runner { self.sort_mode = Some(sort_mode); } Control::Substitution(on_off) => match (&mut self.substitution, on_off) { - (s @ None, OnOff::On) => *s = Some(Substitution::default()), - (s @ Some(_), OnOff::Off) => *s = None, + (s @ None, true) => *s = Some(Substitution::default()), + (s @ Some(_), false) => *s = None, _ => {} }, }