add theme data to circle and fix slideshow
This commit is contained in:
@@ -4,28 +4,23 @@ use rhai::{Array, CustomType, Dynamic, Engine, EvalAltResult, INT, Module, Posit
|
||||
use std::mem;
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::circle::Circle;
|
||||
use super::circle::{Circle, ThemeData};
|
||||
type RhaiCircle = Circle;
|
||||
type RhaiThemeData = ThemeData;
|
||||
|
||||
use crate::db::Collection;
|
||||
use crate::db::hero::OurDB;
|
||||
use serde::Serialize;
|
||||
use serde_json;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Registers a `.json()` method for any type `T` that implements the required traits.
|
||||
fn register_json_method<T>(engine: &mut Engine)
|
||||
where
|
||||
// The type must be:
|
||||
T: CustomType + Clone + Serialize, // A clonable, serializable, custom type for Rhai
|
||||
T: CustomType + Clone + Serialize,
|
||||
{
|
||||
// This is the function that will be called when a script runs '.json()'
|
||||
let to_json_fn = |obj: &mut T| -> Result<String, Box<EvalAltResult>> {
|
||||
// Use serde_json to serialize the object to a pretty-formatted string.
|
||||
// The '?' will automatically convert any serialization error into a Rhai error.
|
||||
serde_json::to_string(obj).map_err(|e| e.to_string().into())
|
||||
};
|
||||
|
||||
// Register the function as a method named "json" for the type 'T'.
|
||||
engine.build_type::<T>().register_fn("json", to_json_fn);
|
||||
}
|
||||
|
||||
@@ -39,6 +34,128 @@ fn id_from_i64_to_u32(id_i64: i64) -> Result<u32, Box<EvalAltResult>> {
|
||||
})
|
||||
}
|
||||
|
||||
#[export_module]
|
||||
mod rhai_theme_data_module {
|
||||
#[rhai_fn(name = "new_theme_data")]
|
||||
pub fn new_theme_data() -> RhaiThemeData {
|
||||
ThemeData::default()
|
||||
}
|
||||
|
||||
// --- Setters for ThemeData ---
|
||||
#[rhai_fn(name = "primary_color", return_raw, global, pure)]
|
||||
pub fn set_primary_color(
|
||||
theme: &mut RhaiThemeData,
|
||||
color: String,
|
||||
) -> Result<RhaiThemeData, Box<EvalAltResult>> {
|
||||
let mut owned_theme = mem::take(theme);
|
||||
owned_theme.primary_color = color;
|
||||
*theme = owned_theme;
|
||||
Ok(theme.clone())
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "background_color", return_raw, global, pure)]
|
||||
pub fn set_background_color(
|
||||
theme: &mut RhaiThemeData,
|
||||
color: String,
|
||||
) -> Result<RhaiThemeData, Box<EvalAltResult>> {
|
||||
let mut owned_theme = mem::take(theme);
|
||||
owned_theme.background_color = color;
|
||||
*theme = owned_theme;
|
||||
Ok(theme.clone())
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "background_pattern", return_raw, global, pure)]
|
||||
pub fn set_background_pattern(
|
||||
theme: &mut RhaiThemeData,
|
||||
pattern: String,
|
||||
) -> Result<RhaiThemeData, Box<EvalAltResult>> {
|
||||
let mut owned_theme = mem::take(theme);
|
||||
owned_theme.background_pattern = pattern;
|
||||
*theme = owned_theme;
|
||||
Ok(theme.clone())
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "logo_symbol", return_raw, global, pure)]
|
||||
pub fn set_logo_symbol(
|
||||
theme: &mut RhaiThemeData,
|
||||
symbol: String,
|
||||
) -> Result<RhaiThemeData, Box<EvalAltResult>> {
|
||||
let mut owned_theme = mem::take(theme);
|
||||
owned_theme.logo_symbol = symbol;
|
||||
*theme = owned_theme;
|
||||
Ok(theme.clone())
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "logo_url", return_raw, global, pure)]
|
||||
pub fn set_logo_url(
|
||||
theme: &mut RhaiThemeData,
|
||||
url: String,
|
||||
) -> Result<RhaiThemeData, Box<EvalAltResult>> {
|
||||
let mut owned_theme = mem::take(theme);
|
||||
owned_theme.logo_url = url;
|
||||
*theme = owned_theme;
|
||||
Ok(theme.clone())
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "nav_dashboard_visible", return_raw, global, pure)]
|
||||
pub fn set_nav_dashboard_visible(
|
||||
theme: &mut RhaiThemeData,
|
||||
visible: bool,
|
||||
) -> Result<RhaiThemeData, Box<EvalAltResult>> {
|
||||
let mut owned_theme = mem::take(theme);
|
||||
owned_theme.nav_dashboard_visible = visible;
|
||||
*theme = owned_theme;
|
||||
Ok(theme.clone())
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "nav_timeline_visible", return_raw, global, pure)]
|
||||
pub fn set_nav_timeline_visible(
|
||||
theme: &mut RhaiThemeData,
|
||||
visible: bool,
|
||||
) -> Result<RhaiThemeData, Box<EvalAltResult>> {
|
||||
let mut owned_theme = mem::take(theme);
|
||||
owned_theme.nav_timeline_visible = visible;
|
||||
*theme = owned_theme;
|
||||
Ok(theme.clone())
|
||||
}
|
||||
|
||||
// --- Getters for ThemeData ---
|
||||
#[rhai_fn(name = "get_primary_color", pure)]
|
||||
pub fn get_primary_color(theme: &mut RhaiThemeData) -> String {
|
||||
theme.primary_color.clone()
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "get_background_color", pure)]
|
||||
pub fn get_background_color(theme: &mut RhaiThemeData) -> String {
|
||||
theme.background_color.clone()
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "get_background_pattern", pure)]
|
||||
pub fn get_background_pattern(theme: &mut RhaiThemeData) -> String {
|
||||
theme.background_pattern.clone()
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "get_logo_symbol", pure)]
|
||||
pub fn get_logo_symbol(theme: &mut RhaiThemeData) -> String {
|
||||
theme.logo_symbol.clone()
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "get_logo_url", pure)]
|
||||
pub fn get_logo_url(theme: &mut RhaiThemeData) -> String {
|
||||
theme.logo_url.clone()
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "get_nav_dashboard_visible", pure)]
|
||||
pub fn get_nav_dashboard_visible(theme: &mut RhaiThemeData) -> bool {
|
||||
theme.nav_dashboard_visible
|
||||
}
|
||||
|
||||
#[rhai_fn(name = "get_nav_timeline_visible", pure)]
|
||||
pub fn get_nav_timeline_visible(theme: &mut RhaiThemeData) -> bool {
|
||||
theme.nav_timeline_visible
|
||||
}
|
||||
}
|
||||
|
||||
#[export_module]
|
||||
mod rhai_circle_module {
|
||||
// --- Circle Functions ---
|
||||
@@ -95,7 +212,7 @@ mod rhai_circle_module {
|
||||
#[rhai_fn(name = "theme", return_raw, global, pure)]
|
||||
pub fn circle_theme(
|
||||
circle: &mut RhaiCircle,
|
||||
theme: HashMap<String, String>,
|
||||
theme: RhaiThemeData,
|
||||
) -> Result<RhaiCircle, Box<EvalAltResult>> {
|
||||
let owned_circle = mem::take(circle);
|
||||
*circle = owned_circle.theme(theme);
|
||||
@@ -108,7 +225,6 @@ mod rhai_circle_module {
|
||||
circle: &mut RhaiCircle,
|
||||
added_circle: String,
|
||||
) -> Result<RhaiCircle, Box<EvalAltResult>> {
|
||||
// Use take to get ownership of the circle
|
||||
let owned_circle = mem::take(circle);
|
||||
*circle = owned_circle.add_circle(added_circle);
|
||||
Ok(circle.clone())
|
||||
@@ -120,93 +236,89 @@ mod rhai_circle_module {
|
||||
circle: &mut RhaiCircle,
|
||||
added_member: String,
|
||||
) -> Result<RhaiCircle, Box<EvalAltResult>> {
|
||||
// Use take to get ownership of the circle
|
||||
let owned_circle = mem::take(circle);
|
||||
*circle = owned_circle.add_member(added_member);
|
||||
Ok(circle.clone())
|
||||
}
|
||||
|
||||
// Circle Getters
|
||||
#[rhai_fn(get = "id", pure)]
|
||||
#[rhai_fn(name = "get_id", pure)]
|
||||
pub fn get_circle_id(circle: &mut RhaiCircle) -> i64 {
|
||||
circle.base_data.id as i64
|
||||
}
|
||||
#[rhai_fn(get = "created_at", pure)]
|
||||
#[rhai_fn(name = "get_created_at", pure)]
|
||||
pub fn get_circle_created_at(circle: &mut RhaiCircle) -> i64 {
|
||||
circle.base_data.created_at
|
||||
}
|
||||
#[rhai_fn(get = "modified_at", pure)]
|
||||
#[rhai_fn(name = "get_modified_at", pure)]
|
||||
pub fn get_circle_modified_at(circle: &mut RhaiCircle) -> i64 {
|
||||
circle.base_data.modified_at
|
||||
}
|
||||
|
||||
#[rhai_fn(get = "title", pure)]
|
||||
#[rhai_fn(name = "get_title", pure)]
|
||||
pub fn get_circle_title(circle: &mut RhaiCircle) -> String {
|
||||
circle.title.clone()
|
||||
}
|
||||
#[rhai_fn(get = "description", pure)]
|
||||
#[rhai_fn(name = "get_description", pure)]
|
||||
pub fn get_circle_description(circle: &mut RhaiCircle) -> Option<String> {
|
||||
circle.description.clone()
|
||||
}
|
||||
#[rhai_fn(get = "circles", pure)]
|
||||
#[rhai_fn(name = "get_circles", pure)]
|
||||
pub fn get_circle_circles(circle: &mut RhaiCircle) -> Vec<String> {
|
||||
circle.circles.clone()
|
||||
}
|
||||
#[rhai_fn(get = "ws_url", pure)]
|
||||
#[rhai_fn(name = "get_ws_url", pure)]
|
||||
pub fn get_circle_ws_url(circle: &mut RhaiCircle) -> String {
|
||||
circle.ws_url.clone()
|
||||
}
|
||||
#[rhai_fn(get = "logo", pure)]
|
||||
#[rhai_fn(name = "get_logo", pure)]
|
||||
pub fn get_circle_logo(circle: &mut RhaiCircle) -> Option<String> {
|
||||
circle.logo.clone()
|
||||
}
|
||||
#[rhai_fn(get = "theme", pure)]
|
||||
pub fn get_circle_theme(circle: &mut RhaiCircle) -> HashMap<String, String> {
|
||||
#[rhai_fn(name = "get_theme", pure)]
|
||||
pub fn get_circle_theme(circle: &mut RhaiCircle) -> RhaiThemeData {
|
||||
circle.theme.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_circle_rhai_module(engine: &mut Engine, db: Arc<OurDB>) {
|
||||
// Register the exported module globally
|
||||
let module = exported_module!(rhai_circle_module);
|
||||
engine.register_global_module(module.into());
|
||||
engine.build_type::<RhaiCircle>();
|
||||
engine.build_type::<RhaiThemeData>();
|
||||
|
||||
// Create a module for database functions
|
||||
let mut db_module = Module::new();
|
||||
let circle_module = exported_module!(rhai_circle_module);
|
||||
let theme_data_module = exported_module!(rhai_theme_data_module);
|
||||
|
||||
engine.register_global_module(circle_module.into());
|
||||
engine.register_global_module(theme_data_module.into());
|
||||
|
||||
register_json_method::<Circle>(engine);
|
||||
register_json_method::<ThemeData>(engine);
|
||||
|
||||
// Manually register database functions as they need to capture 'db'
|
||||
let db_clone_set_circle = db.clone();
|
||||
db_module.set_native_fn(
|
||||
"save_circle",
|
||||
move |circle: Circle| -> Result<Circle, Box<EvalAltResult>> {
|
||||
// Use the Collection trait method directly
|
||||
let result = db_clone_set_circle.set(&circle).map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("DB Error set_circle: {}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
|
||||
// Return the updated circle with the correct ID
|
||||
Ok(result.1)
|
||||
},
|
||||
);
|
||||
|
||||
register_json_method::<Circle>(engine);
|
||||
|
||||
// Manually register database functions as they need to capture 'db'
|
||||
let db_clone_delete_circle = db.clone();
|
||||
db_module.set_native_fn(
|
||||
"delete_circle",
|
||||
move |circle: Circle| -> Result<(), Box<EvalAltResult>> {
|
||||
// Use the Collection trait method directly
|
||||
let result = db_clone_delete_circle
|
||||
.collection::<Circle>()
|
||||
.expect("can open circle collection")
|
||||
.delete_by_id(circle.base_data.id)
|
||||
.expect("can delete circle");
|
||||
|
||||
// Return the updated circle with the correct ID
|
||||
Ok(result)
|
||||
},
|
||||
);
|
||||
@@ -215,7 +327,6 @@ pub fn register_circle_rhai_module(engine: &mut Engine, db: Arc<OurDB>) {
|
||||
db_module.set_native_fn(
|
||||
"get_circle",
|
||||
move || -> Result<Circle, Box<EvalAltResult>> {
|
||||
// Use the Collection trait method directly
|
||||
let all_circles: Vec<Circle> = db_clone_get_circle.get_all().map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("DB Error get_circle: {}", e).into(),
|
||||
@@ -234,12 +345,26 @@ pub fn register_circle_rhai_module(engine: &mut Engine, db: Arc<OurDB>) {
|
||||
},
|
||||
);
|
||||
|
||||
// --- Collection DB Functions ---
|
||||
let db_clone = db.clone();
|
||||
db_module.set_native_fn(
|
||||
"save_circle",
|
||||
move |circle: RhaiCircle| -> Result<RhaiCircle, Box<EvalAltResult>> {
|
||||
let result = db_clone.set(&circle).map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("DB Error: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
Ok(result.1)
|
||||
},
|
||||
);
|
||||
|
||||
let db_clone_get_circle_by_id = db.clone();
|
||||
db_module.set_native_fn(
|
||||
"get_circle_by_id",
|
||||
move |id_i64: INT| -> Result<Circle, Box<EvalAltResult>> {
|
||||
let id_u32 = id_from_i64_to_u32(id_i64)?;
|
||||
// Use the Collection trait method directly
|
||||
db_clone_get_circle_by_id
|
||||
.get_by_id(id_u32)
|
||||
.map_err(|e| {
|
||||
@@ -257,7 +382,6 @@ pub fn register_circle_rhai_module(engine: &mut Engine, db: Arc<OurDB>) {
|
||||
},
|
||||
);
|
||||
|
||||
// Add list_circles function to get all circles
|
||||
let db_clone_list_circles = db.clone();
|
||||
db_module.set_native_fn(
|
||||
"list_circles",
|
||||
@@ -282,7 +406,6 @@ pub fn register_circle_rhai_module(engine: &mut Engine, db: Arc<OurDB>) {
|
||||
},
|
||||
);
|
||||
|
||||
// Register the database module globally
|
||||
engine.register_global_module(db_module.into());
|
||||
|
||||
println!("Successfully registered circle Rhai module using export_module approach.");
|
||||
|
Reference in New Issue
Block a user