Guess I should put this under version control LOL
This commit is contained in:
commit
2035318460
73 changed files with 8836 additions and 0 deletions
52
lifecycle/src/rsx/props.rs
Normal file
52
lifecycle/src/rsx/props.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
//! Implements a Props struct that mostly acts as expected. For arbitrary primitive values,
|
||||
//! it shadows a `serde_json::Value`.
|
||||
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::rsx::{RSX, StylesList};
|
||||
|
||||
/// A value stored inside the `attributes` field on a `Props` instance.
|
||||
/// It shadows `serde_json::Value`, but also allows for some other value
|
||||
/// types common to Alchemy.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum AttributeType {
|
||||
Value(Value),
|
||||
//RSX(RSX)
|
||||
//EventHandler(Box<ComponentEventHandler>)
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for AttributeType {
|
||||
/// Converts a &str to a storable AttributeType.
|
||||
fn from(f: &str) -> Self {
|
||||
AttributeType::Value(Value::String(f.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Emulates props from React, in a sense. Common keys such as `children`, `key` and `styles`
|
||||
/// are extracted out for fast access, and everything else found gets put into the `attributes`
|
||||
/// HashMap.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Props {
|
||||
pub attributes: HashMap<&'static str, AttributeType>,
|
||||
pub children: Vec<RSX>,
|
||||
pub key: String,
|
||||
pub styles: StylesList
|
||||
}
|
||||
|
||||
impl Props {
|
||||
/// Returns a Vec of RSX nodes, which are really just cloned pointers for the most part.
|
||||
pub fn children(&self) -> Vec<RSX> {
|
||||
self.children.clone()
|
||||
}
|
||||
|
||||
/// Returns a Option<&AttributeType> from the `attributes` inner HashMap.
|
||||
pub fn get(&self, key: &str) -> Option<&AttributeType> {
|
||||
match key {
|
||||
"children" => { None },
|
||||
"key" => { None },
|
||||
"styles" => { None },
|
||||
_ => { None } //self.attributes.get(key) }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in a new issue