From 209dba1eb6b31d2fb8b8748f95e400909f119301 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 29 May 2023 01:20:13 +0900 Subject: [PATCH] Cmpop::as_str --- ast/src/generic.rs | 17 +++++++++++++++++ ast/src/unparse.rs | 18 ++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/ast/src/generic.rs b/ast/src/generic.rs index 318a7bf..bb30e68 100644 --- a/ast/src/generic.rs +++ b/ast/src/generic.rs @@ -45,4 +45,21 @@ impl Default for EmptyRange { } } +impl Cmpop { + pub fn as_str(&self) -> &'static str { + match self { + Cmpop::Eq => "==", + Cmpop::NotEq => "!=", + Cmpop::Lt => "<", + Cmpop::LtE => "<=", + Cmpop::Gt => ">", + Cmpop::GtE => ">=", + Cmpop::Is => "is", + Cmpop::IsNot => "is not", + Cmpop::In => "in", + Cmpop::NotIn => "not in", + } + } +} + include!("gen/generic.rs"); diff --git a/ast/src/unparse.rs b/ast/src/unparse.rs index ebfe99a..66342ee 100644 --- a/ast/src/unparse.rs +++ b/ast/src/unparse.rs @@ -1,5 +1,5 @@ use crate::ConversionFlag; -use crate::{Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Expr, Identifier, Operator}; +use crate::{Arg, Arguments, Boolop, Comprehension, Constant, Expr, Identifier, Operator}; use std::fmt; mod precedence { @@ -285,19 +285,9 @@ impl<'a> Unparser<'a> { let new_lvl = precedence::CMP + 1; self.unparse_expr(left, new_lvl)?; for (op, cmp) in ops.iter().zip(comparators) { - let op = match op { - Cmpop::Eq => " == ", - Cmpop::NotEq => " != ", - Cmpop::Lt => " < ", - Cmpop::LtE => " <= ", - Cmpop::Gt => " > ", - Cmpop::GtE => " >= ", - Cmpop::Is => " is ", - Cmpop::IsNot => " is not ", - Cmpop::In => " in ", - Cmpop::NotIn => " not in ", - }; - self.p(op)?; + self.p(" ")?; + self.p(op.as_str())?; + self.p(" ")?; self.unparse_expr(cmp, new_lvl)?; } })