nml/src/parser/style.rs

79 lines
2.1 KiB
Rust
Raw Normal View History

2024-08-03 09:33:21 +02:00
use std::collections::HashMap;
use std::rc::Rc;
use downcast_rs::impl_downcast;
use downcast_rs::Downcast;
/// Styling for an element
pub trait ElementStyle: Downcast + core::fmt::Debug {
/// The style key
fn key(&self) -> &'static str;
/// Attempts to create a new style from a [`json`] string
///
/// # Errors
///
/// Will fail if deserialization fails
fn from_json(&self, json: &str) -> Result<Rc<dyn ElementStyle>, String>;
2024-08-03 11:53:59 +02:00
/// Attempts to deserialize lua table into a new style
fn from_lua(
&self,
lua: &mlua::Lua,
value: mlua::Value,
) -> Result<Rc<dyn ElementStyle>, mlua::Error>;
2024-08-03 09:33:21 +02:00
}
impl_downcast!(ElementStyle);
2024-08-05 18:40:17 +02:00
#[derive(Default)]
pub struct StyleHolder {
styles: HashMap<String, Rc<dyn ElementStyle>>,
}
2024-08-03 09:33:21 +02:00
2024-08-05 18:40:17 +02:00
impl StyleHolder {
2024-08-03 09:33:21 +02:00
/// Checks if a given style key is registered
2024-08-06 18:58:41 +02:00
pub fn is_registered(&self, style_key: &str) -> bool { self.styles.contains_key(style_key) }
2024-08-03 09:33:21 +02:00
/// Gets the current active style for an element
/// NOTE: Will panic if a style is not defined for a given element
/// If you need to process user input, use [`is_registered`]
2024-08-06 18:58:41 +02:00
pub fn current(&self, style_key: &str) -> Rc<dyn ElementStyle> {
2024-08-14 22:50:32 +02:00
self.styles.get(style_key).cloned().unwrap()
2024-08-03 09:33:21 +02:00
}
/// Sets the [`style`]
2024-08-06 18:58:41 +02:00
pub fn set_current(&mut self, style: Rc<dyn ElementStyle>) {
2024-08-05 18:40:17 +02:00
self.styles.insert(style.key().to_string(), style);
2024-08-03 09:33:21 +02:00
}
}
#[macro_export]
macro_rules! impl_elementstyle {
($t:ty, $key:expr) => {
2024-08-14 22:50:32 +02:00
impl $crate::parser::style::ElementStyle for $t {
2024-08-03 09:33:21 +02:00
fn key(&self) -> &'static str { $key }
2024-08-05 18:40:17 +02:00
fn from_json(
&self,
json: &str,
2024-08-14 22:50:32 +02:00
) -> Result<std::rc::Rc<dyn $crate::parser::style::ElementStyle>, String> {
2024-08-03 09:33:21 +02:00
serde_json::from_str::<$t>(json)
.map_err(|e| e.to_string())
2024-08-05 18:40:17 +02:00
.map(|obj| {
2024-08-14 22:50:32 +02:00
std::rc::Rc::new(obj) as std::rc::Rc<dyn $crate::parser::style::ElementStyle>
2024-08-05 18:40:17 +02:00
})
2024-08-03 09:33:21 +02:00
}
2024-08-03 11:53:59 +02:00
fn from_lua(
&self,
lua: &mlua::Lua,
value: mlua::Value,
2024-08-14 22:50:32 +02:00
) -> Result<std::rc::Rc<dyn $crate::parser::style::ElementStyle>, mlua::Error> {
2024-08-05 18:40:17 +02:00
mlua::LuaSerdeExt::from_value::<$t>(lua, value).map(|obj| {
2024-08-14 22:50:32 +02:00
std::rc::Rc::new(obj) as std::rc::Rc<dyn $crate::parser::style::ElementStyle>
2024-08-05 18:40:17 +02:00
})
2024-08-03 11:53:59 +02:00
}
2024-08-03 09:33:21 +02:00
}
};
}