Skip to main content

ass_editor/extensions/
command.rs

1//! Extension lifecycle state, commands, and result types.
2//!
3//! Defines the runtime state machine for extensions, the command descriptors
4//! they expose, user-facing message levels, and command execution results.
5
6#[cfg(feature = "std")]
7use std::collections::HashMap;
8
9#[cfg(not(feature = "std"))]
10use alloc::collections::BTreeMap as HashMap;
11
12#[cfg(not(feature = "std"))]
13use alloc::string::{String, ToString};
14
15/// Extension lifecycle state
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum ExtensionState {
18    /// Extension is uninitialized
19    Uninitialized,
20    /// Extension is being initialized
21    Initializing,
22    /// Extension is active and running
23    Active,
24    /// Extension is paused/suspended
25    Paused,
26    /// Extension encountered an error
27    Error,
28    /// Extension is being shut down
29    ShuttingDown,
30    /// Extension has been shut down
31    Shutdown,
32}
33
34impl ExtensionState {
35    /// Check if the extension is in an active state
36    pub fn is_active(&self) -> bool {
37        matches!(self, Self::Active)
38    }
39
40    /// Check if the extension can be used
41    pub fn is_usable(&self) -> bool {
42        matches!(self, Self::Active | Self::Paused)
43    }
44
45    /// Check if the extension is in an error state
46    pub fn is_error(&self) -> bool {
47        matches!(self, Self::Error)
48    }
49}
50
51/// Extension command that can be executed
52#[derive(Debug, Clone)]
53pub struct ExtensionCommand {
54    /// Command identifier
55    pub id: String,
56    /// Human-readable command name
57    pub name: String,
58    /// Command description
59    pub description: String,
60    /// Keyboard shortcut (if any)
61    pub shortcut: Option<String>,
62    /// Command category for organization
63    pub category: String,
64    /// Whether the command requires a document
65    pub requires_document: bool,
66}
67
68impl ExtensionCommand {
69    /// Create a new extension command
70    pub fn new(id: String, name: String, description: String) -> Self {
71        Self {
72            id,
73            name,
74            description,
75            shortcut: None,
76            category: "General".to_string(),
77            requires_document: true,
78        }
79    }
80
81    /// Set the keyboard shortcut
82    pub fn with_shortcut(mut self, shortcut: String) -> Self {
83        self.shortcut = Some(shortcut);
84        self
85    }
86
87    /// Set the command category
88    pub fn with_category(mut self, category: String) -> Self {
89        self.category = category;
90        self
91    }
92
93    /// Set whether the command requires a document
94    pub fn requires_document(mut self, requires: bool) -> Self {
95        self.requires_document = requires;
96        self
97    }
98}
99
100/// Message levels for user notifications
101#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102pub enum MessageLevel {
103    /// Informational message
104    Info,
105    /// Warning message
106    Warning,
107    /// Error message
108    Error,
109    /// Success message
110    Success,
111}
112
113/// Result of extension command execution
114#[derive(Debug, Clone)]
115pub struct ExtensionResult {
116    /// Whether the command succeeded
117    pub success: bool,
118    /// Optional result message
119    pub message: Option<String>,
120    /// Optional result data
121    pub data: HashMap<String, String>,
122}
123
124impl ExtensionResult {
125    /// Create a successful result
126    pub fn success() -> Self {
127        Self {
128            success: true,
129            message: None,
130            data: HashMap::new(),
131        }
132    }
133
134    /// Create a successful result with message
135    pub fn success_with_message(message: String) -> Self {
136        Self {
137            success: true,
138            message: Some(message),
139            data: HashMap::new(),
140        }
141    }
142
143    /// Create a failure result
144    pub fn failure(message: String) -> Self {
145        Self {
146            success: false,
147            message: Some(message),
148            data: HashMap::new(),
149        }
150    }
151
152    /// Add data to the result
153    pub fn with_data(mut self, key: String, value: String) -> Self {
154        self.data.insert(key, value);
155        self
156    }
157}