move repos into monorepo
This commit is contained in:
78
lib/osiris/core/objects/note/mod.rs
Normal file
78
lib/osiris/core/objects/note/mod.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use crate::store::BaseData;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
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::with_ns(ns),
|
||||
title: None,
|
||||
content: None,
|
||||
tags: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a note with specific ID
|
||||
pub fn with_id(id: String, ns: String) -> Self {
|
||||
let id_u32 = id.parse::<u32>().unwrap_or(0);
|
||||
Self {
|
||||
base_data: BaseData::with_id(id_u32, 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()
|
||||
107
lib/osiris/core/objects/note/rhai.rs
Normal file
107
lib/osiris/core/objects/note/rhai.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
use crate::objects::Note;
|
||||
use rhai::{CustomType, Engine, TypeBuilder, Module, FuncRegistration};
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
/// Register Note functions into a module (for use in packages)
|
||||
pub fn register_note_functions(module: &mut Module) {
|
||||
// Register Note type
|
||||
module.set_custom_type::<Note>("Note");
|
||||
|
||||
// Register builder-style constructor
|
||||
FuncRegistration::new("note")
|
||||
.set_into_module(module, |ns: String| Note::new(ns));
|
||||
|
||||
// Register chainable methods that return Self
|
||||
FuncRegistration::new("title")
|
||||
.set_into_module(module, |mut note: Note, title: String| {
|
||||
note.title = Some(title);
|
||||
note.base_data.update_modified();
|
||||
note
|
||||
});
|
||||
|
||||
FuncRegistration::new("content")
|
||||
.set_into_module(module, |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
|
||||
});
|
||||
|
||||
FuncRegistration::new("tag")
|
||||
.set_into_module(module, |mut note: Note, key: String, value: String| {
|
||||
note.tags.insert(key, value);
|
||||
note.base_data.update_modified();
|
||||
note
|
||||
});
|
||||
|
||||
FuncRegistration::new("mime")
|
||||
.set_into_module(module, |mut note: Note, mime: String| {
|
||||
note.base_data.set_mime(Some(mime));
|
||||
note
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user