From 99dd3a8ab05a94d3ffb2529c06ab6ed59a2dbf82 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Tue, 2 Apr 2024 16:04:36 +0530 Subject: [PATCH] Implement `as_str` & `Display` for all operator enums (#10691) ## Summary This PR adds the `as_str` implementation for all the operator methods. It already exists for `CmpOp` which is being [used in the linter](https://github.com/astral-sh/ruff/blob/ffcd77860c316bfe5709551389eb52e5feb702f6/crates/ruff_linter/src/rules/flake8_simplify/rules/key_in_dict.rs#L117) and it makes sense to implement it for the rest as well. This will also be utilized in error messages for the new parser. --- crates/ruff_python_ast/src/nodes.rs | 98 ++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 17 deletions(-) diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 1d47a1676f..09de037850 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -2735,6 +2735,21 @@ pub enum BoolOp { Or, } +impl BoolOp { + pub const fn as_str(&self) -> &'static str { + match self { + BoolOp::And => "and", + BoolOp::Or => "or", + } + } +} + +impl fmt::Display for BoolOp { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_str()) + } +} + /// See also [operator](https://docs.python.org/3/library/ast.html#ast.operator) #[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)] pub enum Operator { @@ -2753,6 +2768,32 @@ pub enum Operator { FloorDiv, } +impl Operator { + pub const fn as_str(&self) -> &'static str { + match self { + Operator::Add => "+", + Operator::Sub => "-", + Operator::Mult => "*", + Operator::MatMult => "@", + Operator::Div => "/", + Operator::Mod => "%", + Operator::Pow => "**", + Operator::LShift => "<<", + Operator::RShift => ">>", + Operator::BitOr => "|", + Operator::BitXor => "^", + Operator::BitAnd => "&", + Operator::FloorDiv => "//", + } + } +} + +impl fmt::Display for Operator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_str()) + } +} + /// See also [unaryop](https://docs.python.org/3/library/ast.html#ast.unaryop) #[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)] pub enum UnaryOp { @@ -2762,6 +2803,23 @@ pub enum UnaryOp { USub, } +impl UnaryOp { + pub const fn as_str(&self) -> &'static str { + match self { + UnaryOp::Invert => "~", + UnaryOp::Not => "not", + UnaryOp::UAdd => "+", + UnaryOp::USub => "-", + } + } +} + +impl fmt::Display for UnaryOp { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_str()) + } +} + /// See also [cmpop](https://docs.python.org/3/library/ast.html#ast.cmpop) #[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)] pub enum CmpOp { @@ -2777,6 +2835,29 @@ pub enum CmpOp { NotIn, } +impl CmpOp { + pub const 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", + } + } +} + +impl fmt::Display for CmpOp { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_str()) + } +} + /// See also [comprehension](https://docs.python.org/3/library/ast.html#ast.comprehension) #[derive(Clone, Debug, PartialEq)] pub struct Comprehension { @@ -3282,23 +3363,6 @@ impl Deref for TypeParams { pub type Suite = Vec; -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", - } - } -} - impl Parameters { pub fn empty(range: TextRange) -> Self { Self {