use crate::value::{EntityId, Value};
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq)]
pub struct Entity {
pub type_name: Arc<str>,
pub attributes: Vec<Value>,
}
impl Entity {
pub fn new(type_name: impl Into<Arc<str>>, attributes: Vec<Value>) -> Self {
Self {
type_name: type_name.into(),
attributes,
}
}
pub fn attribute(&self, index: usize) -> Option<&Value> {
self.attributes.get(index)
}
pub fn text(&self, index: usize) -> Option<&str> {
self.attribute(index)?.unwrap_typed().as_text()
}
pub fn number(&self, index: usize) -> Option<f64> {
self.attribute(index)?.unwrap_typed().as_f64()
}
pub fn reference(&self, index: usize) -> Option<EntityId> {
self.attribute(index)?.as_ref_id()
}
pub fn references(&self) -> Vec<EntityId> {
let mut out = Vec::new();
for attr in &self.attributes {
attr.for_each_ref(&mut |id| out.push(id));
}
out
}
pub fn is_type(&self, name: &str) -> bool {
self.type_name.eq_ignore_ascii_case(name)
}
}