use std::fmt::{Display, Formatter, Result};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinaryOp {
Arithmetic(ArithmeticOp),
Bitwise(BitwiseOp),
Relational(RelationalOp),
Logical(LogicalOp),
Comma,
}
impl From<ArithmeticOp> for BinaryOp {
#[inline]
fn from(op: ArithmeticOp) -> Self {
Self::Arithmetic(op)
}
}
impl From<BitwiseOp> for BinaryOp {
#[inline]
fn from(op: BitwiseOp) -> Self {
Self::Bitwise(op)
}
}
impl From<RelationalOp> for BinaryOp {
#[inline]
fn from(op: RelationalOp) -> Self {
Self::Relational(op)
}
}
impl From<LogicalOp> for BinaryOp {
#[inline]
fn from(op: LogicalOp) -> Self {
Self::Logical(op)
}
}
impl BinaryOp {
const fn as_str(self) -> &'static str {
match self {
Self::Arithmetic(ref op) => op.as_str(),
Self::Bitwise(ref op) => op.as_str(),
Self::Relational(ref op) => op.as_str(),
Self::Logical(ref op) => op.as_str(),
Self::Comma => ",",
}
}
}
impl Display for BinaryOp {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.as_str())
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ArithmeticOp {
Add,
Sub,
Div,
Mul,
Exp,
Mod,
}
impl ArithmeticOp {
const fn as_str(self) -> &'static str {
match self {
Self::Add => "+",
Self::Sub => "-",
Self::Div => "/",
Self::Mul => "*",
Self::Exp => "**",
Self::Mod => "%",
}
}
}
impl Display for ArithmeticOp {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.as_str())
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BitwiseOp {
And,
Or,
Xor,
Shl,
Shr,
UShr,
}
impl BitwiseOp {
const fn as_str(self) -> &'static str {
match self {
Self::And => "&",
Self::Or => "|",
Self::Xor => "^",
Self::Shl => "<<",
Self::Shr => ">>",
Self::UShr => ">>>",
}
}
}
impl Display for BitwiseOp {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.as_str())
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RelationalOp {
Equal,
NotEqual,
StrictEqual,
StrictNotEqual,
GreaterThan,
GreaterThanOrEqual,
LessThan,
LessThanOrEqual,
In,
InstanceOf,
}
impl RelationalOp {
const fn as_str(self) -> &'static str {
match self {
Self::Equal => "==",
Self::NotEqual => "!=",
Self::StrictEqual => "===",
Self::StrictNotEqual => "!==",
Self::GreaterThan => ">",
Self::GreaterThanOrEqual => ">=",
Self::LessThan => "<",
Self::LessThanOrEqual => "<=",
Self::In => "in",
Self::InstanceOf => "instanceof",
}
}
}
impl Display for RelationalOp {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.as_str())
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LogicalOp {
And,
Or,
Coalesce,
}
impl LogicalOp {
const fn as_str(self) -> &'static str {
match self {
Self::And => "&&",
Self::Or => "||",
Self::Coalesce => "??",
}
}
}
impl Display for LogicalOp {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.as_str())
}
}