move dsl's to rhailib wip

This commit is contained in:
Timur Gordon 2025-06-24 19:23:27 +02:00
parent e2640b9421
commit cd2557c1c3
12 changed files with 161 additions and 65 deletions

View File

@ -655,7 +655,7 @@ mod rhai_library_module {
}
}
pub fn register_library_rhai_module(engine: &mut Engine, db: Arc<OurDB>) {
pub fn register_library_rhai_module(engine: &mut Engine) {
let module = exported_module!(rhai_library_module);
engine.register_global_module(module.into());

View File

@ -86,4 +86,4 @@ required-features = ["rhai"]
[[example]]
name = "rhai_auth_example"
path = "examples/rhai_auth_example.rs"
required-features = ["rhai", "authorization"]
required-features = ["rhai", "macros"]

View File

@ -87,16 +87,16 @@ impl Access {
pub fn can_access_resource(
db: Arc<OurDB>,
public_key: &str,
_resource_id_to_check: u32,
_resource_type_to_check: &str,
object_id: u32,
_object_type: &str,
) -> bool {
// Query for Access records matching the public key.
// Note: This fetches all access records for the user. For performance with many records,
// consider a more specific query if your DB supports it, or caching.
println!("Checking access for public key: {}", public_key);
// get all access records for object
let access_records = match db
.collection::<Access>()
.expect("Failed to get Access collection")
.get::<access_index::circle_pk, _>(public_key)
.get::<access_index::object_id, _>(&object_id)
{
Ok(records) => records,
Err(_e) => {
@ -107,9 +107,8 @@ pub fn can_access_resource(
}
};
if !access_records.is_empty() {
return true;
}
println!("Access records: {:#?}", access_records);
false // Default to deny if no grant is found
// if circle_pk is in access records true
return access_records.iter().any(|record| record.circle_pk == public_key)
}

View File

@ -41,6 +41,11 @@ impl Image {
Self::default()
}
/// Gets the ID of the image.
pub fn id(&self) -> u32 {
self.base_data.id
}
/// Sets the title of the image.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
@ -107,6 +112,11 @@ impl Pdf {
Self::default()
}
/// Gets the ID of the image.
pub fn id(&self) -> u32 {
self.base_data.id
}
/// Sets the title of the PDF.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
@ -134,7 +144,7 @@ impl Pdf {
/// Represents a Markdown document library item.
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)]
pub struct Markdown {
/// Base model data
pub base_data: BaseModelData,
@ -147,23 +157,17 @@ pub struct Markdown {
pub content: String,
}
impl Default for Markdown {
fn default() -> Self {
Self {
base_data: BaseModelData::new(),
title: String::new(),
description: None,
content: String::new(),
}
}
}
impl Markdown {
/// Creates a new `Markdown` document with default values.
pub fn new() -> Self {
Self::default()
}
/// Gets the ID of the image.
pub fn id(&self) -> u32 {
self.base_data.id
}
/// Sets the title of the document.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
@ -184,7 +188,7 @@ impl Markdown {
}
/// Represents a table of contents entry for a book.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)]
pub struct TocEntry {
/// Title of the chapter/section
pub title: String,
@ -194,16 +198,6 @@ pub struct TocEntry {
pub subsections: Vec<TocEntry>,
}
impl Default for TocEntry {
fn default() -> Self {
Self {
title: String::new(),
page: 0,
subsections: Vec::new(),
}
}
}
impl TocEntry {
/// Creates a new `TocEntry` with default values.
pub fn new() -> Self {
@ -231,7 +225,7 @@ impl TocEntry {
/// Represents a Book library item (collection of markdown pages with TOC).
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)]
pub struct Book {
/// Base model data
pub base_data: BaseModelData,
@ -246,24 +240,17 @@ pub struct Book {
pub pages: Vec<String>,
}
impl Default for Book {
fn default() -> Self {
Self {
base_data: BaseModelData::new(),
title: String::new(),
description: None,
table_of_contents: Vec::new(),
pages: Vec::new(),
}
}
}
impl Book {
/// Creates a new `Book` with default values.
pub fn new() -> Self {
Self::default()
}
/// Gets the ID of the book.
pub fn id(&self) -> u32 {
self.base_data.id
}
/// Sets the title of the book.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
@ -303,7 +290,7 @@ impl Book {
/// Represents a Slides library item (collection of images for slideshow).
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)]
pub struct Slides {
/// Base model data
pub base_data: BaseModelData,
@ -318,24 +305,17 @@ pub struct Slides {
pub slide_titles: Vec<Option<String>>,
}
impl Default for Slides {
fn default() -> Self {
Self {
base_data: BaseModelData::new(),
title: String::new(),
description: None,
slide_urls: Vec::new(),
slide_titles: Vec::new(),
}
}
}
impl Slides {
/// Creates a new `Slides` with default values.
pub fn new() -> Self {
Self::default()
}
/// Gets the ID of the slideshow.
pub fn id(&self) -> u32 {
self.base_data.id
}
/// Sets the title of the slideshow.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();

View File

@ -1,4 +1,2 @@
pub mod collection;
pub mod items;
pub mod rhai;
pub use rhai::register_library_rhai_module;

View File

@ -0,0 +1,3 @@
## Object Model
This is a generic object model mostly used for testing purposes.

View File

@ -0,0 +1,60 @@
use std::sync::Arc;
use crate::db::{hero::OurDB, Collection, Db};
use heromodels_core::BaseModelData;
use heromodels_derive::model;
// Temporarily removed to fix compilation issues
// use rhai_autobind_macros::rhai_model_export;
use rhai::{CustomType, TypeBuilder};
use serde::{Deserialize, Serialize};
/// Represents an event in a contact
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)]
pub struct Log {
/// Base model data
pub base_data: BaseModelData,
#[index]
pub title: String,
pub description: String,
#[index]
pub subject_pk: String,
#[index]
pub object_id: u32,
}
impl Log {
pub fn new() -> Self {
Log {
title: String::new(),
base_data: BaseModelData::new(),
description: String::new(),
subject_pk: String::new(),
object_id: 0,
}
}
pub fn id(&self) -> u32 {
self.base_data.id
}
pub fn title(mut self, title: String) -> Self {
self.title = title;
self
}
pub fn description(mut self, description: String) -> Self {
self.description = description;
self
}
pub fn subject_pk(mut self, subject_pk: String) -> Self {
self.subject_pk = subject_pk;
self
}
pub fn object_id(mut self, object_id: u32) -> Self {
self.object_id = object_id;
self
}
}

View File

@ -0,0 +1,5 @@
// Export contact module
pub mod log;
// Re-export contact, Group from the inner contact module (contact.rs) within src/models/contact/mod.rs
pub use self::log::Log;

View File

@ -12,6 +12,7 @@ pub mod flow;
pub mod governance;
pub mod legal;
pub mod library;
pub mod object;
pub mod projects;
// Re-export key types for convenience
@ -38,6 +39,4 @@ pub use circle::register_circle_rhai_module;
pub use flow::register_flow_rhai_module;
pub use legal::register_legal_rhai_module;
#[cfg(feature = "rhai")]
pub use library::register_library_rhai_module;
#[cfg(feature = "rhai")]
pub use projects::register_projects_rhai_module;

View File

@ -0,0 +1,3 @@
## Object Model
This is a generic object model mostly used for testing purposes.

View File

@ -0,0 +1,5 @@
// Export contact module
pub mod object;
// Re-export contact, Group from the inner contact module (contact.rs) within src/models/contact/mod.rs
pub use self::object::Object;

View File

@ -0,0 +1,44 @@
use std::sync::Arc;
use crate::db::{hero::OurDB, Collection, Db};
use heromodels_core::BaseModelData;
use heromodels_derive::model;
// Temporarily removed to fix compilation issues
// use rhai_autobind_macros::rhai_model_export;
use rhai::{CustomType, TypeBuilder};
use serde::{Deserialize, Serialize};
/// Represents an event in a contact
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)]
pub struct Object {
/// Base model data
pub base_data: BaseModelData,
#[index]
pub title: String,
pub description: String
}
impl Object {
pub fn new() -> Self {
Object {
title: String::new(),
base_data: BaseModelData::new(),
description: String::new(),
}
}
pub fn id(&self) -> u32 {
self.base_data.id
}
pub fn title(mut self, title: String) -> Self {
self.title = title;
self
}
pub fn description(mut self, description: String) -> Self {
self.description = description;
self
}
}