use serde_json::{json, Value};
pub mod data;
pub mod forms;
pub mod layout;
pub mod media;
pub mod special;
pub use data::*;
pub use forms::*;
pub use layout::*;
pub use media::*;
pub use special::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Role {
Input,
Output,
None,
}
pub trait Component: Send + Sync {
fn id(&self) -> &str;
fn kind(&self) -> &'static str;
fn props(&self) -> Value;
fn role(&self) -> Role {
Role::None
}
fn layout(&self) -> Layout {
Layout::default()
}
fn children(&self) -> Vec<&dyn Component> {
Vec::new()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Layout {
pub width: Option<u32>,
pub height: Option<u32>,
pub max_width: Option<u32>,
pub max_height: Option<u32>,
pub scale: Option<u32>,
pub min_width: Option<u32>,
}
impl Layout {
pub fn json(self) -> Value {
let mut o = serde_json::Map::new();
if let Some(w) = self.width {
o.insert("width".into(), json!(w));
}
if let Some(h) = self.height {
o.insert("height".into(), json!(h));
}
if let Some(mw) = self.max_width {
o.insert("max_width".into(), json!(mw));
}
if let Some(mh) = self.max_height {
o.insert("max_height".into(), json!(mh));
}
if let Some(s) = self.scale {
o.insert("scale".into(), json!(s));
}
if let Some(m) = self.min_width {
o.insert("min_width".into(), json!(m));
}
Value::Object(o)
}
}
pub struct WithLayout<C> {
inner: C,
layout: Layout,
}
impl<C: Component> WithLayout<C> {
pub fn new(inner: C) -> Self {
Self {
inner,
layout: Layout::default(),
}
}
pub fn width(mut self, w: u32) -> Self {
self.layout.width = Some(w);
self
}
pub fn height(mut self, h: u32) -> Self {
self.layout.height = Some(h);
self
}
pub fn max_width(mut self, mw: u32) -> Self {
self.layout.max_width = Some(mw);
self
}
pub fn max_height(mut self, mh: u32) -> Self {
self.layout.max_height = Some(mh);
self
}
pub fn scale(mut self, s: u32) -> Self {
self.layout.scale = Some(s);
self
}
pub fn min_width(mut self, w: u32) -> Self {
self.layout.min_width = Some(w);
self
}
pub fn set_layout(mut self, l: Layout) -> Self {
self.layout = l;
self
}
pub fn into_inner(self) -> C {
self.inner
}
pub fn layout(&self) -> Layout {
self.layout
}
}
impl<C: Component> Component for WithLayout<C> {
fn id(&self) -> &str {
self.inner.id()
}
fn kind(&self) -> &'static str {
self.inner.kind()
}
fn props(&self) -> Value {
self.inner.props()
}
fn role(&self) -> Role {
self.inner.role()
}
fn children(&self) -> Vec<&dyn Component> {
self.inner.children()
}
fn layout(&self) -> Layout {
self.layout
}
}
pub trait IntoBox {
fn into_box(self) -> Box<dyn Component>;
}
impl<T> IntoBox for T
where
T: Component + 'static,
{
fn into_box(self) -> Box<dyn Component> {
Box::new(self)
}
}
pub(crate) fn children_refs(items: &[Box<dyn Component>]) -> Vec<&dyn Component> {
items.iter().map(|b| b.as_ref() as &dyn Component).collect()
}