final db models wip

This commit is contained in:
timurgordon
2025-06-03 21:51:21 +03:00
parent abbed9a1a1
commit c3f6b91aa0
13 changed files with 1251 additions and 11 deletions

507
_archive/adapter_macros/Cargo.lock generated Normal file
View File

@@ -0,0 +1,507 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "adapter_macros"
version = "0.1.0"
dependencies = [
"chrono",
"rhai",
]
[[package]]
name = "ahash"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
dependencies = [
"cfg-if",
"const-random",
"getrandom 0.3.3",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "bumpalo"
version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]]
name = "cc"
version = "1.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f4ac86a9e5bc1e2b3449ab9d7d3a6a405e3d1bb28d7b9be8614f55846ae3766"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-link",
]
[[package]]
name = "const-random"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359"
dependencies = [
"const-random-macro",
]
[[package]]
name = "const-random-macro"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e"
dependencies = [
"getrandom 0.2.16",
"once_cell",
"tiny-keccak",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "crunchy"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
[[package]]
name = "getrandom"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
dependencies = [
"cfg-if",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi 0.14.2+wasi-0.2.4",
]
[[package]]
name = "iana-time-zone"
version = "0.1.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "instant"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
"cfg-if",
]
[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]]
name = "log"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
dependencies = [
"portable-atomic",
]
[[package]]
name = "portable-atomic"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e"
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
[[package]]
name = "rhai"
version = "1.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce4d759a4729a655ddfdbb3ff6e77fb9eadd902dae12319455557796e435d2a6"
dependencies = [
"ahash",
"bitflags",
"instant",
"num-traits",
"once_cell",
"rhai_codegen",
"smallvec",
"smartstring",
"thin-vec",
]
[[package]]
name = "rhai_codegen"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5a11a05ee1ce44058fa3d5961d05194fdbe3ad6b40f904af764d81b86450e6b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "rustversion"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "smallvec"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9"
[[package]]
name = "smartstring"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
dependencies = [
"autocfg",
"static_assertions",
"version_check",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "2.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thin-vec"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "144f754d318415ac792f9d69fc87abbbfc043ce2ef041c60f16ad828f638717d"
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "windows-core"
version = "0.61.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46ec44dc15085cea82cf9c78f85a9114c463a369786585ad2882d1ff0b0acf40"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-implement"
version = "0.60.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38"
[[package]]
name = "windows-result"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b895b5356fc36103d0f64dd1e94dfa7ac5633f1c9dd6e80fe9ec4adef69e09d"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a7ab927b2637c19b3dbe0965e75d8f2d30bdd697a1516191cad2ec4df8fb28a"
dependencies = [
"windows-link",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@@ -0,0 +1,8 @@
[package]
name = "adapter_macros"
version = "0.1.0"
edition = "2021"
[dependencies]
rhai = "1.21.0"
chrono = "0.4"

View File

@@ -0,0 +1,77 @@
# Rhai Adapter Macros (`adapter_macros`)
This crate provides utility macros to facilitate the integration of Rust code with the Rhai scripting engine, particularly for adapting function and method signatures.
## Purpose
Rhai often uses `i64` as its default integer type. When exposing Rust functions or methods that use other integer types (e.g., `u32`, `usize`), direct registration can lead to type mismatches or require manual conversion boilerplate in each registered function.
These macros help bridge this gap by wrapping your Rust functions/methods, automatically handling the conversion from Rhai's `i64` to the Rust-native integer type and providing more informative error messages if the conversion fails (e.g., due to overflow).
## Macros
### 1. `adapt_rhai_i64_input_fn!(rust_fn:path, rust_int_ty:ty)`
Adapts a standalone Rust function that takes a single argument of `rust_int_ty` and returns `Result<_, Box<rhai::EvalAltResult>>`.
- `rust_fn`: The path to your Rust function (e.g., `my_module::my_function`).
- `rust_int_ty`: The integer type your Rust function expects (e.g., `u32`).
**Example Usage in Rust (when registering with Rhai Engine):**
```rust
// In your Rust code where you set up the Rhai engine:
// Assuming your_function(val: u32) -> Result<Something, Error> exists
// and adapter_macros is a dependency.
engine.register_fn("my_rhai_func", adapter_macros::adapt_rhai_i64_input_fn!(my_module::your_function, u32));
// In Rhai script:
// my_rhai_func(10); // 10 (i64) will be converted to u32 for your_function
```
### 2. `adapt_rhai_i64_input_method!(struct_ty:ty, rust_method_name:ident, rust_int_ty:ty)`
Adapts a Rust instance method that takes `self` by value, a single integer argument of `rust_int_ty`, and returns `Self`. This is useful for builder-like patterns or methods that modify and return the instance.
- `struct_ty`: The type of the struct on which the method is defined (e.g., `MyStruct`).
- `rust_method_name`: The identifier of the Rust method (e.g., `with_value`).
- `rust_int_ty`: The integer type your Rust method's argument expects (e.g., `u16`).
**Example Usage in Rust (when registering with Rhai Engine):**
```rust
// In your Rust code:
// Assuming MyStruct has a method: fn with_value(self, val: u16) -> Self
// and adapter_macros is a dependency.
engine.register_fn("with_value", adapter_macros::adapt_rhai_i64_input_method!(MyStruct, with_value, u16));
// In Rhai script:
// let my_obj = MyStruct::new();
// my_obj.with_value(5); // 5 (i64) will be converted to u16 for MyStruct::with_value
```
## Error Handling
If the `i64` value from Rhai cannot be converted to `rust_int_ty` (e.g., an `i64` value of -1 is passed when `u32` is expected, or a value too large for `u16`), the macros will generate a `rhai::EvalAltResult::ErrorArithmetic` with a descriptive message and the script position.
## How It Works
The macros generate a closure that:
1. Takes a `rhai::NativeCallContext` and an `i64` from the Rhai engine.
2. Attempts to convert the `i64` to the specified `rust_int_ty` using `try_into()`.
3. If conversion fails, it returns an `ErrorArithmetic` detailing the function/method name and the type conversion that failed.
4. If conversion succeeds, it calls the original Rust function/method with the converted value.
## Adding to Your Project
Add this to your `Cargo.toml`:
```toml
[dependencies]
adapter_macros = { path = "../adapter_macros" } # Or version = "0.1.0" if published
rhai = "x.y.z" # Your rhai version
```
Ensure the path to `adapter_macros` is correct if used locally.

View File

@@ -0,0 +1,184 @@
/// Macro to adapt a Rust function taking a specific integer type (e.g., u32)
/// to a Rhai function that expects an i64 argument.
#[macro_export]
macro_rules! adapt_rhai_i64_input_fn {
($rust_fn:path, $rust_int_ty:ty) => {
move |context: ::rhai::NativeCallContext, rhai_val: i64| -> Result<_, Box<::rhai::EvalAltResult>> {
let rust_val: $rust_int_ty = rhai_val.try_into().map_err(|_e| {
Box::new(::rhai::EvalAltResult::ErrorArithmetic(
format!("Conversion error for arg in '{}' from i64 to {}", stringify!($rust_fn), stringify!($rust_int_ty)),
context.position(),
))
})?;
Ok($rust_fn(rust_val))
}
};
}
/// Macro to adapt a Rust instance method (taking self by value, one int arg, returns Self)
/// to a Rhai function that expects an i64 for that integer argument.
#[macro_export]
macro_rules! adapt_rhai_i64_input_method {
($struct_ty:ty, $rust_method_name:ident, $rust_int_ty:ty) => {
move |context: ::rhai::NativeCallContext, instance: $struct_ty, rhai_val: i64| -> Result<$struct_ty, Box<::rhai::EvalAltResult>> {
let rust_val: $rust_int_ty = rhai_val.try_into().map_err(|_e| {
Box::new(::rhai::EvalAltResult::ErrorArithmetic(
format!("Conversion error for arg in '{}::{}' from i64 to {}", stringify!($struct_ty), stringify!($rust_method_name), stringify!($rust_int_ty)),
context.position(),
))
})?;
Ok(instance.$rust_method_name(rust_val))
}
};
}
// --- Rhai Timestamp Helper Functions ---
pub mod rhai_timestamp_helpers {
use rhai::{INT, EvalAltResult, Position};
use chrono::{DateTime, Utc, TimeZone};
pub fn datetime_to_rhai_timestamp(dt: &DateTime<Utc>) -> INT {
dt.timestamp()
}
pub fn option_datetime_to_rhai_timestamp(dt_opt: &Option<DateTime<Utc>>) -> Option<INT> {
dt_opt.as_ref().map(datetime_to_rhai_timestamp)
}
pub fn rhai_timestamp_to_datetime(ts: INT) -> Result<DateTime<Utc>, Box<EvalAltResult>> {
Utc.timestamp_opt(ts, 0).single()
.ok_or_else(|| Box::new(EvalAltResult::ErrorArithmetic(format!("Invalid Unix timestamp: {}", ts), Position::NONE)))
}
pub fn option_rhai_timestamp_to_datetime(ts_opt: Option<INT>) -> Result<Option<DateTime<Utc>>, Box<EvalAltResult>> {
match ts_opt {
Some(ts) => rhai_timestamp_to_datetime(ts).map(Some),
None => Ok(None),
}
}
}
// --- Macro for Enum Accessors (String Conversion) ---
#[macro_export]
macro_rules! register_rhai_enum_accessors {
($engine:expr, $struct_type:ty, $field_name:ident, $rhai_name:expr, $to_string_fn:path, $from_string_fn:path) => {
$engine.register_get_set(
$rhai_name,
move |obj: &mut $struct_type| -> rhai::ImmutableString { $to_string_fn(&obj.$field_name) },
move |obj: &mut $struct_type, val: rhai::ImmutableString| -> Result<(), Box<rhai::EvalAltResult>> {
obj.$field_name = $from_string_fn(val.as_str())?;
Ok(())
}
);
};
}
// --- Macro for DateTime Accessors (Unix Timestamp INT) ---
#[macro_export]
macro_rules! register_rhai_datetime_accessors {
($engine:expr, $struct_type:ty, $field_path:ident, $rhai_name:expr, _required) => {
$engine.register_get_set(
$rhai_name,
move |obj: &mut $struct_type| -> rhai::INT {
let field_value = &obj.$field_path;
$crate::rhai_timestamp_helpers::datetime_to_rhai_timestamp(field_value)
},
move |obj: &mut $struct_type, val: rhai::INT| -> Result<(), Box<rhai::EvalAltResult>> {
obj.$field_path = $crate::rhai_timestamp_helpers::rhai_timestamp_to_datetime(val)?;
Ok(())
}
);
};
($engine:expr, $struct_type:ty, base_data.$field_name:ident, $rhai_name:expr, _required) => {
$engine.register_get_set(
$rhai_name,
move |obj: &mut $struct_type| -> rhai::INT {
let field_value = &obj.base_data.$field_name;
$crate::rhai_timestamp_helpers::datetime_to_rhai_timestamp(field_value)
},
move |obj: &mut $struct_type, val: rhai::INT| -> Result<(), Box<rhai::EvalAltResult>> {
obj.base_data.$field_name = $crate::rhai_timestamp_helpers::rhai_timestamp_to_datetime(val)?;
Ok(())
}
);
};
($engine:expr, $struct_type:ty, $field_path:ident, $rhai_name:expr) => {
$engine.register_get_set(
$rhai_name,
move |obj: &mut $struct_type| -> Option<rhai::INT> {
let field_value = &obj.$field_path;
$crate::rhai_timestamp_helpers::option_datetime_to_rhai_timestamp(field_value)
},
move |obj: &mut $struct_type, val_opt: Option<rhai::INT>| -> Result<(), Box<rhai::EvalAltResult>> {
obj.$field_path = $crate::rhai_timestamp_helpers::option_rhai_timestamp_to_datetime(val_opt)?;
Ok(())
}
);
};
($engine:expr, $struct_type:ty, base_data.$field_name:ident, $rhai_name:expr) => {
$engine.register_get_set(
$rhai_name,
move |obj: &mut $struct_type| -> Option<rhai::INT> {
let field_value = &obj.base_data.$field_name;
$crate::rhai_timestamp_helpers::option_datetime_to_rhai_timestamp(field_value)
},
move |obj: &mut $struct_type, val_opt: Option<rhai::INT>| -> Result<(), Box<rhai::EvalAltResult>> {
obj.base_data.$field_name = $crate::rhai_timestamp_helpers::option_rhai_timestamp_to_datetime(val_opt)?;
Ok(())
}
);
};
}
// --- Macro for Vec<String> Accessors ---
#[macro_export]
macro_rules! register_rhai_vec_string_accessors {
($engine:expr, $struct_type:ty, $field_name:ident, $rhai_name:expr) => {
$engine.register_get_set(
$rhai_name,
move |obj: &mut $struct_type| -> rhai::Array {
obj.$field_name.iter().map(|s| rhai::Dynamic::from(rhai::ImmutableString::from(s.as_str()))).collect()
},
move |obj: &mut $struct_type, val: rhai::Array| {
obj.$field_name = val.into_iter().map(|d| d.into_string().unwrap_or_default()).collect();
}
);
};
}
// --- Macro for Generic Field Accessors (Example: ImmutableString) ---
#[macro_export]
macro_rules! register_rhai_field_accessors {
($engine:expr, $struct_type:ty, $field_name:ident, $rhai_name:expr) => {
$engine.register_get_set(
$rhai_name,
move |obj: &mut $struct_type| obj.$field_name.clone(), // Assuming cloneable and directly Rhai compatible
move |obj: &mut $struct_type, val: rhai::Dynamic| { // Or specific type like ImmutableString
// This part would need more specific handling based on expected type
// For example, if it's always ImmutableString:
// if let Ok(s) = val.into_immutable_string() { obj.$field_name = s.into_owned(); }
// For now, let's assume it's a type that can be directly assigned from Dynamic if Dynamic holds the right type
// This is a simplification; real use might need obj.$field_name = val.try_cast().unwrap_or_default();
// However, register_get_set usually infers setter type from getter type.
// If getter is T, setter is fn(&mut S, T)
// So if getter is |obj| obj.field.clone() -> String, setter should be |obj, val: String|
// Let's assume string for now if using ImmutableString for Rhai
if let Ok(s_val) = val.into_immutable_string() {
obj.$field_name = s_val.into(); // Assumes field_name is String
} else {
// Handle error or default
eprintln!("Failed to cast for field {}", $rhai_name);
}
}
);
};
($engine:expr, $struct_type:ty, $field_name:ident, $rhai_name:expr, $rhai_type:ty) => {
$engine.register_get_set(
$rhai_name,
move |obj: &mut $struct_type| obj.$field_name.clone(),
move |obj: &mut $struct_type, val: $rhai_type| {
obj.$field_name = val;
}
);
};
}