use crate::nodes::types::{Node, Numeric};
#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(not(feature = "std"))]
use alloc::{
collections::BTreeMap as HashMap,
string::{String, ToString},
vec::Vec,
};
pub struct ObjectBuilder {
map: HashMap<String, Node>,
}
impl ObjectBuilder {
pub fn new() -> Self {
Self {
map: HashMap::new(),
}
}
#[cfg(feature = "std")]
pub fn with_capacity(capacity: usize) -> Self {
Self {
map: HashMap::with_capacity(capacity),
}
}
#[cfg(not(feature = "std"))]
pub fn with_capacity(_capacity: usize) -> Self {
Self::new()
}
pub fn add_str(mut self, key: &str, value: &str) -> Self {
self.map
.insert(key.to_string(), Node::Str(value.to_string()));
self
}
pub fn add_i32(mut self, key: &str, value: i32) -> Self {
self.map
.insert(key.to_string(), Node::Number(Numeric::Int32(value)));
self
}
pub fn add_i64(mut self, key: &str, value: i64) -> Self {
self.map
.insert(key.to_string(), Node::Number(Numeric::Integer(value)));
self
}
pub fn add_u32(mut self, key: &str, value: u32) -> Self {
self.map
.insert(key.to_string(), Node::Number(Numeric::UInt32(value)));
self
}
pub fn add_u64(mut self, key: &str, value: u64) -> Self {
self.map
.insert(key.to_string(), Node::Number(Numeric::UInteger(value)));
self
}
pub fn add_f64(mut self, key: &str, value: f64) -> Self {
self.map
.insert(key.to_string(), Node::Number(Numeric::Float(value)));
self
}
pub fn add_bool(mut self, key: &str, value: bool) -> Self {
self.map.insert(key.to_string(), Node::Boolean(value));
self
}
pub fn add_null(mut self, key: &str) -> Self {
self.map.insert(key.to_string(), Node::None);
self
}
pub fn add_node(mut self, key: &str, node: Node) -> Self {
self.map.insert(key.to_string(), node);
self
}
pub fn add_object(mut self, key: &str, map: HashMap<String, Node>) -> Self {
self.map.insert(key.to_string(), Node::Object(map));
self
}
pub fn add_array(mut self, key: &str, vec: Vec<Node>) -> Self {
self.map.insert(key.to_string(), Node::Array(vec));
self
}
pub fn build(self) -> Node {
Node::Object(self.map)
}
}
impl Default for ObjectBuilder {
fn default() -> Self {
Self::new()
}
}
pub struct ArrayBuilder {
vec: Vec<Node>,
}
impl ArrayBuilder {
pub fn new() -> Self {
Self { vec: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
vec: Vec::with_capacity(capacity),
}
}
pub fn push_str(mut self, value: &str) -> Self {
self.vec.push(Node::Str(value.to_string()));
self
}
pub fn add_str(self, value: &str) -> Self {
self.push_str(value)
}
pub fn push_i32(mut self, value: i32) -> Self {
self.vec.push(Node::Number(Numeric::Int32(value)));
self
}
pub fn add_i32(self, value: i32) -> Self {
self.push_i32(value)
}
pub fn push_f64(mut self, value: f64) -> Self {
self.vec.push(Node::Number(Numeric::Float(value)));
self
}
pub fn add_f64(self, value: f64) -> Self {
self.push_f64(value)
}
pub fn push_bool(mut self, value: bool) -> Self {
self.vec.push(Node::Boolean(value));
self
}
pub fn add_bool(self, value: bool) -> Self {
self.push_bool(value)
}
pub fn push_null(mut self) -> Self {
self.vec.push(Node::None);
self
}
pub fn add_null(self) -> Self {
self.push_null()
}
pub fn push_node(mut self, node: Node) -> Self {
self.vec.push(node);
self
}
pub fn add_node(self, node: Node) -> Self {
self.push_node(node)
}
pub fn build(self) -> Node {
Node::Array(self.vec)
}
}
impl Default for ArrayBuilder {
fn default() -> Self {
Self::new()
}
}