use crate::expr::Expression;
use crate::repr::{Decor, Decorate, SetSpan, Span};
use crate::RawString;
use std::ops::Range;
pub type IntoIter = Box<dyn Iterator<Item = Expression>>;
pub type Iter<'a> = Box<dyn Iterator<Item = &'a Expression> + 'a>;
pub type IterMut<'a> = Box<dyn Iterator<Item = &'a mut Expression> + 'a>;
#[derive(Debug, Clone, Eq, Default)]
pub struct Array {
values: Vec<Expression>,
trailing: RawString,
trailing_comma: bool,
decor: Decor,
span: Option<Range<usize>>,
}
impl Array {
#[inline]
pub fn new() -> Self {
Array::default()
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Array {
values: Vec::with_capacity(capacity),
..Default::default()
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
self.values.len()
}
#[inline]
pub fn clear(&mut self) {
self.values.clear();
}
#[inline]
pub fn get(&self, index: usize) -> Option<&Expression> {
self.values.get(index)
}
#[inline]
pub fn get_mut(&mut self, index: usize) -> Option<&mut Expression> {
self.values.get_mut(index)
}
#[inline]
pub fn insert(&mut self, index: usize, value: impl Into<Expression>) {
self.values.insert(index, value.into());
}
#[inline]
pub fn push(&mut self, value: impl Into<Expression>) {
self.values.push(value.into());
}
#[inline]
pub fn pop(&mut self) -> Option<Expression> {
self.values.pop()
}
#[inline]
pub fn remove(&mut self, index: usize) -> Expression {
self.values.remove(index)
}
#[inline]
pub fn iter(&self) -> Iter<'_> {
Box::new(self.values.iter())
}
#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_> {
Box::new(self.values.iter_mut())
}
#[inline]
pub fn trailing(&self) -> &RawString {
&self.trailing
}
#[inline]
pub fn set_trailing(&mut self, trailing: impl Into<RawString>) {
self.trailing = trailing.into();
}
#[inline]
pub fn trailing_comma(&self) -> bool {
self.trailing_comma
}
#[inline]
pub fn set_trailing_comma(&mut self, yes: bool) {
self.trailing_comma = yes;
}
pub(crate) fn despan(&mut self, input: &str) {
self.decor.despan(input);
self.trailing.despan(input);
for value in &mut self.values {
value.despan(input);
}
}
}
impl PartialEq for Array {
fn eq(&self, other: &Self) -> bool {
self.values == other.values
&& self.trailing_comma == other.trailing_comma
&& self.trailing == other.trailing
}
}
impl From<Vec<Expression>> for Array {
fn from(values: Vec<Expression>) -> Self {
Array {
values,
..Default::default()
}
}
}
impl<T> Extend<T> for Array
where
T: Into<Expression>,
{
fn extend<I>(&mut self, iterable: I)
where
I: IntoIterator<Item = T>,
{
let iter = iterable.into_iter();
let reserve = if self.is_empty() {
iter.size_hint().0
} else {
(iter.size_hint().0 + 1) / 2
};
self.values.reserve(reserve);
iter.for_each(|v| self.push(v));
}
}
impl<T> FromIterator<T> for Array
where
T: Into<Expression>,
{
fn from_iter<I>(iterable: I) -> Self
where
I: IntoIterator<Item = T>,
{
let iter = iterable.into_iter();
let lower = iter.size_hint().0;
let mut array = Array::with_capacity(lower);
array.extend(iter);
array
}
}
impl IntoIterator for Array {
type Item = Expression;
type IntoIter = IntoIter;
fn into_iter(self) -> Self::IntoIter {
Box::new(self.values.into_iter())
}
}
impl<'a> IntoIterator for &'a Array {
type Item = &'a Expression;
type IntoIter = Iter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a> IntoIterator for &'a mut Array {
type Item = &'a mut Expression;
type IntoIter = IterMut<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
decorate_impl!(Array);
span_impl!(Array);