use crate::nodes::node::Node;
#[cfg(feature = "alloc")]
pub struct ArrayBuilder {
items: alloc::vec::Vec<Node>,
}
#[cfg(feature = "alloc")]
impl ArrayBuilder {
pub fn new() -> Self {
Self {
items: alloc::vec::Vec::new(),
}
}
pub fn push<T: Into<Node>>(mut self, value: T) -> Self {
self.items.push(value.into());
self
}
pub fn extend<T: Into<Node>>(mut self, values: impl IntoIterator<Item = T>) -> Self {
self.items.extend(values.into_iter().map(|v| v.into()));
self
}
pub fn push_if<T: Into<Node>>(mut self, condition: bool, value: T) -> Self {
if condition {
self.items.push(value.into());
}
self
}
pub fn push_opt<T: Into<Node>>(mut self, value: Option<T>) -> Self {
if let Some(v) = value {
self.items.push(v.into());
}
self
}
pub fn build(self) -> Node {
Node::Array(self.items)
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
}
#[cfg(feature = "alloc")]
impl Default for ArrayBuilder {
fn default() -> Self {
Self::new()
}
}