first commit

This commit is contained in:
Timur Gordon
2025-10-20 22:24:25 +02:00
commit 097360ad12
48 changed files with 6712 additions and 0 deletions

139
src/objects/event/mod.rs Normal file
View File

@@ -0,0 +1,139 @@
use crate::store::BaseData;
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
#[cfg(feature = "rhai-support")]
pub mod rhai;
/// Event status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub enum EventStatus {
#[default]
Draft,
Published,
Cancelled,
}
/// A calendar event object
#[derive(Debug, Clone, Serialize, Deserialize, crate::DeriveObject)]
pub struct Event {
/// Base data
pub base_data: BaseData,
/// Title of the event
#[index]
pub title: String,
/// Optional description
pub description: Option<String>,
/// Start time
#[index]
#[serde(with = "time::serde::timestamp")]
pub start_time: OffsetDateTime,
/// End time
#[serde(with = "time::serde::timestamp")]
pub end_time: OffsetDateTime,
/// Optional location
#[index]
pub location: Option<String>,
/// Event status
#[index]
pub status: EventStatus,
/// Whether this is an all-day event
pub all_day: bool,
/// Optional category
#[index]
pub category: Option<String>,
}
impl Event {
/// Create a new event
pub fn new(ns: String, title: impl ToString) -> Self {
let now = OffsetDateTime::now_utc();
Self {
base_data: BaseData::new(ns),
title: title.to_string(),
description: None,
start_time: now,
end_time: now,
location: None,
status: EventStatus::default(),
all_day: false,
category: None,
}
}
/// Create an event with specific ID
pub fn with_id(id: String, ns: String, title: impl ToString) -> Self {
let now = OffsetDateTime::now_utc();
Self {
base_data: BaseData::with_id(id, ns),
title: title.to_string(),
description: None,
start_time: now,
end_time: now,
location: None,
status: EventStatus::default(),
all_day: false,
category: None,
}
}
/// Set the description
pub fn set_description(mut self, description: impl ToString) -> Self {
self.description = Some(description.to_string());
self.base_data.update_modified();
self
}
/// Set the start time
pub fn set_start_time(mut self, start_time: OffsetDateTime) -> Self {
self.start_time = start_time;
self.base_data.update_modified();
self
}
/// Set the end time
pub fn set_end_time(mut self, end_time: OffsetDateTime) -> Self {
self.end_time = end_time;
self.base_data.update_modified();
self
}
/// Set the location
pub fn set_location(mut self, location: impl ToString) -> Self {
self.location = Some(location.to_string());
self.base_data.update_modified();
self
}
/// Set the status
pub fn set_status(mut self, status: EventStatus) -> Self {
self.status = status;
self.base_data.update_modified();
self
}
/// Set as all-day event
pub fn set_all_day(mut self, all_day: bool) -> Self {
self.all_day = all_day;
self.base_data.update_modified();
self
}
/// Set the category
pub fn set_category(mut self, category: impl ToString) -> Self {
self.category = Some(category.to_string());
self.base_data.update_modified();
self
}
}
// Object trait implementation is auto-generated by #[derive(DeriveObject)]
// The derive macro generates: object_type(), base_data(), base_data_mut(), index_keys(), indexed_fields()

64
src/objects/event/rhai.rs Normal file
View File

@@ -0,0 +1,64 @@
use crate::objects::Event;
use rhai::{CustomType, Engine, TypeBuilder};
impl CustomType for Event {
fn build(mut builder: TypeBuilder<Self>) {
builder
.with_name("Event")
.with_fn("new", |ns: String, title: String| Event::new(ns, title))
.with_fn("set_description", |event: &mut Event, desc: String| {
event.description = Some(desc);
event.base_data.update_modified();
})
.with_fn("set_location", |event: &mut Event, location: String| {
event.location = Some(location);
event.base_data.update_modified();
})
.with_fn("set_category", |event: &mut Event, category: String| {
event.category = Some(category);
event.base_data.update_modified();
})
.with_fn("set_all_day", |event: &mut Event, all_day: bool| {
event.all_day = all_day;
event.base_data.update_modified();
})
.with_fn("get_id", |event: &mut Event| event.base_data.id.clone())
.with_fn("get_title", |event: &mut Event| event.title.clone())
.with_fn("to_json", |event: &mut Event| {
serde_json::to_string_pretty(event).unwrap_or_default()
});
}
}
/// Register Event API in Rhai engine
pub fn register_event_api(engine: &mut Engine) {
engine.build_type::<Event>();
// Register builder-style constructor
engine.register_fn("event", |ns: String, title: String| Event::new(ns, title));
// Register chainable methods that return Self
engine.register_fn("description", |mut event: Event, desc: String| {
event.description = Some(desc);
event.base_data.update_modified();
event
});
engine.register_fn("location", |mut event: Event, location: String| {
event.location = Some(location);
event.base_data.update_modified();
event
});
engine.register_fn("category", |mut event: Event, category: String| {
event.category = Some(category);
event.base_data.update_modified();
event
});
engine.register_fn("all_day", |mut event: Event, all_day: bool| {
event.all_day = all_day;
event.base_data.update_modified();
event
});
}

5
src/objects/mod.rs Normal file
View File

@@ -0,0 +1,5 @@
pub mod note;
pub mod event;
pub use note::Note;
pub use event::Event;

78
src/objects/note/mod.rs Normal file
View File

@@ -0,0 +1,78 @@
use crate::store::BaseData;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[cfg(feature = "rhai-support")]
pub mod rhai;
/// A simple note object
#[derive(Debug, Clone, Serialize, Deserialize, crate::DeriveObject)]
pub struct Note {
/// Base data
pub base_data: BaseData,
/// Title of the note
#[index]
pub title: Option<String>,
/// Content of the note (searchable but not indexed)
pub content: Option<String>,
/// Tags for categorization
#[index]
pub tags: BTreeMap<String, String>,
}
impl Note {
/// Create a new note
pub fn new(ns: String) -> Self {
Self {
base_data: BaseData::new(ns),
title: None,
content: None,
tags: BTreeMap::new(),
}
}
/// Create a note with specific ID
pub fn with_id(id: String, ns: String) -> Self {
Self {
base_data: BaseData::with_id(id, ns),
title: None,
content: None,
tags: BTreeMap::new(),
}
}
/// Set the title
pub fn set_title(mut self, title: impl ToString) -> Self {
self.title = Some(title.to_string());
self.base_data.update_modified();
self
}
/// Set the content
pub fn set_content(mut self, content: impl ToString) -> Self {
let content_str = content.to_string();
self.base_data.set_size(Some(content_str.len() as u64));
self.content = Some(content_str);
self.base_data.update_modified();
self
}
/// Add a tag
pub fn add_tag(mut self, key: impl ToString, value: impl ToString) -> Self {
self.tags.insert(key.to_string(), value.to_string());
self.base_data.update_modified();
self
}
/// Set MIME type
pub fn set_mime(mut self, mime: impl ToString) -> Self {
self.base_data.set_mime(Some(mime.to_string()));
self
}
}
// Object trait implementation is auto-generated by #[derive(DeriveObject)]
// The derive macro generates: object_type(), base_data(), base_data_mut(), index_keys(), indexed_fields()

67
src/objects/note/rhai.rs Normal file
View File

@@ -0,0 +1,67 @@
use crate::objects::Note;
use rhai::{CustomType, Engine, TypeBuilder};
impl CustomType for Note {
fn build(mut builder: TypeBuilder<Self>) {
builder
.with_name("Note")
.with_fn("new", |ns: String| Note::new(ns))
.with_fn("set_title", |note: &mut Note, title: String| {
note.title = Some(title);
note.base_data.update_modified();
})
.with_fn("set_content", |note: &mut Note, content: String| {
let size = content.len() as u64;
note.content = Some(content);
note.base_data.set_size(Some(size));
note.base_data.update_modified();
})
.with_fn("add_tag", |note: &mut Note, key: String, value: String| {
note.tags.insert(key, value);
note.base_data.update_modified();
})
.with_fn("set_mime", |note: &mut Note, mime: String| {
note.base_data.set_mime(Some(mime));
})
.with_fn("get_id", |note: &mut Note| note.base_data.id.clone())
.with_fn("get_title", |note: &mut Note| note.title.clone().unwrap_or_default())
.with_fn("get_content", |note: &mut Note| note.content.clone().unwrap_or_default())
.with_fn("to_json", |note: &mut Note| {
serde_json::to_string_pretty(note).unwrap_or_default()
});
}
}
/// Register Note API in Rhai engine
pub fn register_note_api(engine: &mut Engine) {
engine.build_type::<Note>();
// Register builder-style constructor
engine.register_fn("note", |ns: String| Note::new(ns));
// Register chainable methods that return Self
engine.register_fn("title", |mut note: Note, title: String| {
note.title = Some(title);
note.base_data.update_modified();
note
});
engine.register_fn("content", |mut note: Note, content: String| {
let size = content.len() as u64;
note.content = Some(content);
note.base_data.set_size(Some(size));
note.base_data.update_modified();
note
});
engine.register_fn("tag", |mut note: Note, key: String, value: String| {
note.tags.insert(key, value);
note.base_data.update_modified();
note
});
engine.register_fn("mime", |mut note: Note, mime: String| {
note.base_data.set_mime(Some(mime));
note
});
}