mod op;
use crate::{
Span, Spanned,
expression::Expression,
visitor::{VisitWith, Visitor, VisitorMut},
};
use boa_interner::{Interner, ToInternedString};
use core::ops::ControlFlow;
pub use op::*;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub struct Unary {
op: UnaryOp,
target: Box<Expression>,
span: Span,
}
impl Unary {
#[inline]
#[must_use]
pub fn new(op: UnaryOp, target: Expression, span: Span) -> Self {
Self {
op,
target: Box::new(target),
span,
}
}
#[inline]
#[must_use]
pub const fn op(&self) -> UnaryOp {
self.op
}
#[inline]
#[must_use]
pub fn target(&self) -> &Expression {
self.target.as_ref()
}
#[inline]
#[must_use]
pub fn target_mut(&mut self) -> &mut Expression {
self.target.as_mut()
}
}
impl Spanned for Unary {
#[inline]
fn span(&self) -> Span {
self.span
}
}
impl ToInternedString for Unary {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
format!("{} {}", self.op, self.target.to_interned_string(interner))
}
}
impl From<Unary> for Expression {
#[inline]
fn from(op: Unary) -> Self {
Self::Unary(op)
}
}
impl VisitWith for Unary {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: Visitor<'a>,
{
visitor.visit_expression(&self.target)
}
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: VisitorMut<'a>,
{
visitor.visit_expression_mut(&mut self.target)
}
}