use crate::components::Component;
use serde::Serialize; use std::iter::IntoIterator;
#[derive(Serialize)]
pub struct View {
view: String,
data: Vec<Component>,
}
impl View {
pub fn new(name: Option<impl Into<String>>) -> Self {
let view = name
.map(Into::into)
.unwrap_or_else(|| hostname::get().unwrap_or_default().to_string_lossy().into());
Self {
view,
data: Vec::new(),
}
}
pub fn add<I>(&mut self, components: I) -> &mut Self
where
I: IntoIterator<Item = Component>,
{
self.data.extend(components);
self
}
pub fn to_json(&self) -> serde_json::Value {
serde_json::json!({
"view": self.view,
"data": self.data, })
}
pub fn name(&self) -> &str {
&self.view
}
}