use crate::expr::Expression;
use crate::{Decor, Decorate, Decorated, Ident, Span};
use std::ops::{self, Range};
#[derive(Debug, Clone, Eq)]
pub struct Attribute {
pub key: Decorated<Ident>,
pub value: Expression,
decor: Decor,
span: Option<Range<usize>>,
}
impl Attribute {
pub fn new(key: impl Into<Decorated<Ident>>, value: impl Into<Expression>) -> Attribute {
Attribute {
key: key.into(),
value: value.into(),
decor: Decor::default(),
span: None,
}
}
#[inline]
pub fn has_key(&self, key: &str) -> bool {
self.key.as_str() == key
}
pub(crate) fn despan(&mut self, input: &str) {
self.decor.despan(input);
self.key.decor_mut().despan(input);
self.value.despan(input);
}
}
impl PartialEq for Attribute {
fn eq(&self, other: &Self) -> bool {
self.key == other.key && self.value == other.value
}
}
decorate_impl!(Attribute);
span_impl!(Attribute);
pub struct AttributeMut<'a> {
attr: &'a mut Attribute,
}
impl<'a> AttributeMut<'a> {
pub(crate) fn new(attr: &'a mut Attribute) -> AttributeMut<'a> {
AttributeMut { attr }
}
pub fn get(&self) -> &Attribute {
self.attr
}
pub fn key_decor_mut(&mut self) -> &mut Decor {
self.attr.key.decor_mut()
}
pub fn value_mut(&mut self) -> &mut Expression {
&mut self.attr.value
}
}
impl ops::Deref for AttributeMut<'_> {
type Target = Attribute;
#[inline]
fn deref(&self) -> &Self::Target {
self.get()
}
}
impl Decorate for AttributeMut<'_> {
fn decor(&self) -> &Decor {
self.attr.decor()
}
fn decor_mut(&mut self) -> &mut Decor {
self.attr.decor_mut()
}
}
impl Span for AttributeMut<'_> {
fn span(&self) -> Option<Range<usize>> {
self.attr.span()
}
}