Skip to main content

time_tracker_plugin_sdk/
extensions.rs

1//! Extension types for plugins to extend Core entities
2
3/// Entity types that can be extended
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum EntityType {
6    Activity,
7    ManualEntry,
8    Category,
9}
10
11/// Types of extensions
12#[derive(Debug, Clone, PartialEq)]
13pub enum ExtensionType {
14    DatabaseSchema,
15    Model,
16    DataHook,
17    Query,
18    UIForm,
19}
20
21/// Schema change operations
22#[derive(Debug, Clone)]
23pub enum SchemaChange {
24    /// Create a new table
25    CreateTable {
26        table: String,
27        columns: Vec<TableColumn>,
28    },
29    /// Add a column to an existing table
30    AddColumn {
31        table: String,
32        column: String,
33        column_type: String,
34        default: Option<String>,
35        foreign_key: Option<ForeignKey>,
36    },
37    /// Add an index
38    AddIndex {
39        table: String,
40        index: String,
41        columns: Vec<String>,
42    },
43    /// Add a foreign key constraint
44    AddForeignKey {
45        table: String,
46        column: String,
47        foreign_table: String,
48        foreign_column: String,
49    },
50}
51
52/// Marks a column as automatically set on insert and/or update (e.g. created_at, updated_at).
53#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54pub enum AutoTimestamp {
55    /// Set once when the row is inserted (e.g. created_at).
56    Created,
57    /// Set on insert and updated on every update (e.g. updated_at).
58    Updated,
59}
60
61/// Table column definition for CreateTable
62#[derive(Debug, Clone)]
63pub struct TableColumn {
64    pub name: String,
65    pub column_type: String,
66    pub primary_key: bool,
67    pub nullable: bool,
68    pub default: Option<String>,
69    pub foreign_key: Option<ForeignKey>,
70    /// If set, the core will automatically set this column on insert/update (Unix timestamp).
71    pub auto_timestamp: Option<AutoTimestamp>,
72}
73
74/// Foreign key definition
75#[derive(Debug, Clone)]
76pub struct ForeignKey {
77    pub table: String,
78    pub column: String,
79}
80
81/// Model field definition
82#[derive(Debug, Clone)]
83pub struct ModelField {
84    pub name: String,
85    pub type_: String,
86    pub optional: bool,
87}
88
89/// Query filter function type
90pub type QueryFilterFn = Box<dyn Fn(Vec<serde_json::Value>, std::collections::HashMap<String, serde_json::Value>) -> Result<Vec<serde_json::Value>, String> + Send + Sync>;
91
92/// Query filter
93pub struct QueryFilter {
94    pub name: String,
95    pub filter_fn: QueryFilterFn,
96}
97
98impl std::fmt::Debug for QueryFilter {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        f.debug_struct("QueryFilter")
101            .field("name", &self.name)
102            .field("filter_fn", &"<function>")
103            .finish()
104    }
105}
106
107/// Schema extension definition (used by plugins to declare their schema)
108#[derive(Debug, Clone)]
109pub struct SchemaExtension {
110    pub entity_type: EntityType,
111    pub schema_changes: Vec<SchemaChange>,
112}