use rbx_types::{Ref, Variant};
use ustr::{Ustr, UstrMap};
#[derive(Debug)]
pub struct InstanceBuilder {
pub(crate) referent: Ref,
pub(crate) name: String,
pub(crate) class: Ustr,
pub(crate) properties: Vec<(Ustr, Variant)>,
pub(crate) children: Vec<InstanceBuilder>,
}
impl InstanceBuilder {
pub fn new<S: Into<Ustr>>(class: S) -> Self {
let class = class.into();
let name = class.to_string();
InstanceBuilder {
referent: Ref::new(),
name,
class,
properties: Vec::new(),
children: Vec::new(),
}
}
pub fn with_property_capacity<S: Into<Ustr>>(class: S, capacity: usize) -> Self {
let class = class.into();
let name = class.to_string();
InstanceBuilder {
referent: Ref::new(),
name,
class,
properties: Vec::with_capacity(capacity),
children: Vec::new(),
}
}
pub fn empty() -> Self {
InstanceBuilder {
referent: Ref::new(),
name: String::new(),
class: Ustr::default(),
properties: Vec::new(),
children: Vec::new(),
}
}
pub fn referent(&self) -> Ref {
self.referent
}
pub fn with_referent<R: Into<Ref>>(self, referent: R) -> Self {
Self {
referent: referent.into(),
..self
}
}
pub fn with_name<S: Into<String>>(self, name: S) -> Self {
Self {
name: name.into(),
..self
}
}
pub fn set_name<S: Into<String>>(&mut self, name: S) {
self.name = name.into();
}
pub fn with_class<S: Into<Ustr>>(self, class: S) -> Self {
Self {
class: class.into(),
..self
}
}
pub fn set_class<S: Into<Ustr>>(&mut self, class: S) {
self.class = class.into();
}
pub fn with_property<K: Into<Ustr>, V: Into<Variant>>(mut self, key: K, value: V) -> Self {
self.properties.push((key.into(), value.into()));
self
}
pub fn add_property<K: Into<Ustr>, V: Into<Variant>>(&mut self, key: K, value: V) {
self.properties.push((key.into(), value.into()));
}
pub fn has_property<K: Into<Ustr>>(&self, key: K) -> bool {
let key = key.into();
self.properties.iter().any(|(k, _)| *k == key)
}
pub fn with_properties<K, V, I>(mut self, props: I) -> Self
where
K: Into<Ustr>,
V: Into<Variant>,
I: IntoIterator<Item = (K, V)>,
{
let props = props.into_iter().map(|(k, v)| (k.into(), v.into()));
self.properties.extend(props);
self
}
pub fn add_properties<K, V, I>(&mut self, props: I)
where
K: Into<Ustr>,
V: Into<Variant>,
I: IntoIterator<Item = (K, V)>,
{
let props = props.into_iter().map(|(k, v)| (k.into(), v.into()));
self.properties.extend(props);
}
pub fn with_child(mut self, child: InstanceBuilder) -> Self {
self.children.push(child);
self
}
pub fn add_child(&mut self, child: InstanceBuilder) {
self.children.push(child);
}
pub fn with_children<I>(mut self, children: I) -> Self
where
I: IntoIterator<Item = InstanceBuilder>,
{
self.children.extend(children);
self
}
pub fn add_children<I>(&mut self, children: I)
where
I: IntoIterator<Item = InstanceBuilder>,
{
self.children.extend(children);
}
}
#[derive(Debug)]
pub struct Instance {
pub(crate) referent: Ref,
pub(crate) children: Vec<Ref>,
pub(crate) parent: Ref,
pub name: String,
pub class: Ustr,
pub properties: UstrMap<Variant>,
}
impl Instance {
#[inline]
pub fn referent(&self) -> Ref {
self.referent
}
#[inline]
pub fn children(&self) -> &[Ref] {
&self.children
}
#[inline]
pub fn parent(&self) -> Ref {
self.parent
}
}