Skip to main content

ass_editor/commands/
command_result.rs

1//! Result type produced by executing editor commands.
2//!
3//! `CommandResult` carries success status, optional messaging, the modified
4//! range, the new cursor position, and (when the `stream` feature is enabled)
5//! an incremental `ScriptDeltaOwned` for partial re-parsing.
6
7use crate::core::{Position, Range};
8
9#[cfg(feature = "stream")]
10use ass_core::parser::ScriptDeltaOwned;
11
12#[cfg(not(feature = "std"))]
13use alloc::string::String;
14
15/// Result of executing a command
16///
17/// Contains the modified document and optional metadata about the operation.
18/// This will be used by the history system to track changes.
19#[derive(Debug, Clone)]
20pub struct CommandResult {
21    /// Whether the command was successfully executed
22    pub success: bool,
23
24    /// Optional message about the operation
25    pub message: Option<String>,
26
27    /// The range of text that was modified (for cursor updates)
28    pub modified_range: Option<Range>,
29
30    /// New cursor position after the command
31    pub new_cursor: Option<Position>,
32
33    /// Whether the document content was changed
34    pub content_changed: bool,
35
36    /// Script delta for incremental parsing (when available)
37    #[cfg(feature = "stream")]
38    pub script_delta: Option<ScriptDeltaOwned>,
39}
40
41impl CommandResult {
42    /// Create a successful command result
43    pub fn success() -> Self {
44        Self {
45            success: true,
46            message: None,
47            modified_range: None,
48            new_cursor: None,
49            content_changed: false,
50            #[cfg(feature = "stream")]
51            script_delta: None,
52        }
53    }
54
55    /// Create a successful result with content change
56    pub fn success_with_change(range: Range, cursor: Position) -> Self {
57        Self {
58            success: true,
59            message: None,
60            modified_range: Some(range),
61            new_cursor: Some(cursor),
62            content_changed: true,
63            #[cfg(feature = "stream")]
64            script_delta: None,
65        }
66    }
67
68    /// Create a failed command result
69    pub fn failure(message: String) -> Self {
70        Self {
71            success: false,
72            message: Some(message),
73            modified_range: None,
74            new_cursor: None,
75            content_changed: false,
76            #[cfg(feature = "stream")]
77            script_delta: None,
78        }
79    }
80
81    /// Add a script delta to the result
82    #[cfg(feature = "stream")]
83    #[must_use]
84    pub fn with_delta(mut self, delta: ScriptDeltaOwned) -> Self {
85        self.script_delta = Some(delta);
86        self
87    }
88
89    /// Add a message to the result
90    #[must_use]
91    pub fn with_message(mut self, message: String) -> Self {
92        self.message = Some(message);
93        self
94    }
95}