ass-editor 0.1.1

High-performance, ergonomic editor layer for ASS subtitles
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! Command system for editor operations
//!
//! Provides a trait-based command system with fluent APIs for text editing,
//! undo/redo support, and extensible command types. All commands are atomic
//! and can be undone/redone efficiently.

pub mod delta_commands;
pub mod event_commands;
pub mod karaoke_commands;
pub mod macros;
pub mod style_commands;
pub mod tag_commands;

use crate::core::{EditorDocument, EditorError, Position, Range, Result};

#[cfg(feature = "stream")]
use ass_core::parser::ScriptDeltaOwned;

// Re-export delta commands, event commands, karaoke commands, style commands, and tag commands
pub use delta_commands::*;
pub use event_commands::*;
pub use karaoke_commands::*;
pub use style_commands::*;
pub use tag_commands::*;

#[cfg(not(feature = "std"))]
use alloc::{
    boxed::Box,
    format,
    string::{String, ToString},
    vec::Vec,
};

/// Result of executing a command
///
/// Contains the modified document and optional metadata about the operation.
/// This will be used by the history system to track changes.
#[derive(Debug, Clone)]
pub struct CommandResult {
    /// Whether the command was successfully executed
    pub success: bool,

    /// Optional message about the operation
    pub message: Option<String>,

    /// The range of text that was modified (for cursor updates)
    pub modified_range: Option<Range>,

    /// New cursor position after the command
    pub new_cursor: Option<Position>,

    /// Whether the document content was changed
    pub content_changed: bool,

    /// Script delta for incremental parsing (when available)
    #[cfg(feature = "stream")]
    pub script_delta: Option<ScriptDeltaOwned>,
}

impl CommandResult {
    /// Create a successful command result
    pub fn success() -> Self {
        Self {
            success: true,
            message: None,
            modified_range: None,
            new_cursor: None,
            content_changed: false,
            #[cfg(feature = "stream")]
            script_delta: None,
        }
    }

    /// Create a successful result with content change
    pub fn success_with_change(range: Range, cursor: Position) -> Self {
        Self {
            success: true,
            message: None,
            modified_range: Some(range),
            new_cursor: Some(cursor),
            content_changed: true,
            #[cfg(feature = "stream")]
            script_delta: None,
        }
    }

    /// Create a failed command result
    pub fn failure(message: String) -> Self {
        Self {
            success: false,
            message: Some(message),
            modified_range: None,
            new_cursor: None,
            content_changed: false,
            #[cfg(feature = "stream")]
            script_delta: None,
        }
    }

    /// Add a script delta to the result
    #[cfg(feature = "stream")]
    #[must_use]
    pub fn with_delta(mut self, delta: ScriptDeltaOwned) -> Self {
        self.script_delta = Some(delta);
        self
    }

    /// Add a message to the result
    #[must_use]
    pub fn with_message(mut self, message: String) -> Self {
        self.message = Some(message);
        self
    }
}

/// Trait for editor commands that can be executed and undone
///
/// All commands implement this trait to provide a consistent interface
/// for execution, undo/redo, and introspection.
///
/// # Examples
///
/// Creating a custom command:
///
/// ```
/// use ass_editor::{EditorCommand, EditorDocument, CommandResult, Result, Position, Range};
///
/// #[derive(Debug)]
/// struct UppercaseCommand {
///     description: String,
/// }
///
/// impl UppercaseCommand {
///     fn new() -> Self {
///         Self {
///             description: "Convert to uppercase".to_string(),
///         }
///     }
/// }
///
/// impl EditorCommand for UppercaseCommand {
///     fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
///         let text = document.text().to_uppercase();
///         let range = Range::new(Position::new(0), Position::new(document.len()));
///         document.replace(range, &text)?;
///         Ok(CommandResult::success().with_message("Text converted to uppercase".to_string()))
///     }
///
///     fn description(&self) -> &str {
///         &self.description
///     }
/// }
/// ```
pub trait EditorCommand: core::fmt::Debug + Send + Sync {
    /// Execute the command on the given document
    ///
    /// Returns a result indicating success/failure and metadata about
    /// the operation for undo/redo tracking.
    fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult>;

    /// Get a human-readable description of the command
    fn description(&self) -> &str;

    /// Check if this command modifies document content
    ///
    /// Used to determine if the document should be marked as modified
    /// and whether to save undo state.
    fn modifies_content(&self) -> bool {
        true
    }

    /// Get the estimated memory usage of this command
    ///
    /// Used for memory management in undo stacks with limited capacity.
    /// Default implementation provides a conservative estimate.
    fn memory_usage(&self) -> usize {
        64 // Conservative default estimate for command overhead
    }
}

/// Text insertion command
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InsertTextCommand {
    /// Position to insert text at
    pub position: Position,
    /// Text to insert
    pub text: String,
    /// Optional description override
    pub description: Option<String>,
}

impl InsertTextCommand {
    /// Create a new insert text command
    ///
    /// # Examples
    ///
    /// ```
    /// use ass_editor::{InsertTextCommand, EditorDocument, Position, EditorCommand};
    ///
    /// let mut doc = EditorDocument::new();
    /// let command = InsertTextCommand::new(Position::new(0), "Hello World".to_string());
    ///
    /// let result = command.execute(&mut doc).unwrap();
    /// assert!(result.success);
    /// assert_eq!(doc.text(), "Hello World");
    /// ```
    pub fn new(position: Position, text: String) -> Self {
        Self {
            position,
            text,
            description: None,
        }
    }

    /// Set a custom description for this command
    #[must_use]
    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }
}

impl EditorCommand for InsertTextCommand {
    fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
        document.insert_raw(self.position, &self.text)?;

        let end_pos = Position::new(self.position.offset + self.text.len());
        let range = Range::new(self.position, end_pos);

        Ok(CommandResult::success_with_change(range, end_pos))
    }

    fn description(&self) -> &str {
        self.description.as_deref().unwrap_or("Insert text")
    }

    fn memory_usage(&self) -> usize {
        core::mem::size_of::<Self>()
            + self.text.len()
            + self.description.as_ref().map_or(0, |d| d.len())
    }
}

/// Text deletion command
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeleteTextCommand {
    /// Range of text to delete
    pub range: Range,
    /// Optional description override
    pub description: Option<String>,
}

impl DeleteTextCommand {
    /// Create a new delete text command
    pub fn new(range: Range) -> Self {
        Self {
            range,
            description: None,
        }
    }

    /// Set a custom description for this command
    #[must_use]
    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }
}

impl EditorCommand for DeleteTextCommand {
    fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
        document.delete_raw(self.range)?;

        let cursor_pos = self.range.start;
        let range = Range::new(self.range.start, self.range.start);

        Ok(CommandResult::success_with_change(range, cursor_pos))
    }

    fn description(&self) -> &str {
        self.description.as_deref().unwrap_or("Delete text")
    }
}

/// Text replacement command
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplaceTextCommand {
    /// Range of text to replace
    pub range: Range,
    /// New text to insert
    pub new_text: String,
    /// Optional description override
    pub description: Option<String>,
}

impl ReplaceTextCommand {
    /// Create a new replace text command
    pub fn new(range: Range, new_text: String) -> Self {
        Self {
            range,
            new_text,
            description: None,
        }
    }

    /// Set a custom description for this command
    #[must_use]
    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }
}

impl EditorCommand for ReplaceTextCommand {
    fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
        document.replace_raw(self.range, &self.new_text)?;

        let end_pos = Position::new(self.range.start.offset + self.new_text.len());
        let range = Range::new(self.range.start, end_pos);

        Ok(CommandResult::success_with_change(range, end_pos))
    }

    fn description(&self) -> &str {
        self.description.as_deref().unwrap_or("Replace text")
    }

    fn memory_usage(&self) -> usize {
        core::mem::size_of::<Self>()
            + self.new_text.len()
            + self.description.as_ref().map_or(0, |d| d.len())
    }
}

/// Batch command that executes multiple commands as a single atomic operation
#[derive(Debug)]
pub struct BatchCommand {
    /// Commands to execute in order
    pub commands: Vec<Box<dyn EditorCommand>>,
    /// Description of the batch operation
    pub description: String,
}

impl BatchCommand {
    /// Create a new batch command
    ///
    /// # Examples
    ///
    /// ```
    /// use ass_editor::{BatchCommand, InsertTextCommand, DeleteTextCommand, Position, Range, EditorDocument, EditorCommand};
    ///
    /// let mut doc = EditorDocument::from_content("Hello World").unwrap();
    ///
    /// let batch = BatchCommand::new("Multiple operations".to_string())
    ///     .add_command(Box::new(InsertTextCommand::new(Position::new(5), " beautiful".to_string())))
    ///     .add_command(Box::new(DeleteTextCommand::new(Range::new(Position::new(15), Position::new(21)))));
    ///
    /// let result = batch.execute(&mut doc).unwrap();
    /// assert!(result.success);
    /// assert_eq!(doc.text(), "Hello beautiful");
    /// ```
    pub fn new(description: String) -> Self {
        Self {
            commands: Vec::new(),
            description,
        }
    }

    /// Add a command to the batch
    pub fn add_command(mut self, command: Box<dyn EditorCommand>) -> Self {
        self.commands.push(command);
        self
    }

    /// Add multiple commands to the batch
    pub fn add_commands(mut self, commands: Vec<Box<dyn EditorCommand>>) -> Self {
        self.commands.extend(commands);
        self
    }
}

impl EditorCommand for BatchCommand {
    fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
        let mut overall_result = CommandResult::success();
        let mut first_range: Option<Range> = None;
        let mut last_cursor: Option<Position> = None;

        for command in &self.commands {
            let result = command.execute(document)?;

            if !result.success {
                return Ok(CommandResult::failure(format!(
                    "Batch command failed at: {}",
                    command.description()
                )));
            }

            // Track the overall range of changes
            if let Some(range) = result.modified_range {
                first_range = Some(match first_range {
                    Some(existing) => existing.union(&range),
                    None => range,
                });
            }

            if result.new_cursor.is_some() {
                last_cursor = result.new_cursor;
            }

            if result.content_changed {
                overall_result.content_changed = true;
            }
        }

        overall_result.modified_range = first_range;
        overall_result.new_cursor = last_cursor;

        Ok(overall_result)
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn memory_usage(&self) -> usize {
        core::mem::size_of::<Self>()
            + self.description.len()
            + self
                .commands
                .iter()
                .map(|c| c.memory_usage())
                .sum::<usize>()
    }
}

/// Fluent API builder for creating and executing commands
///
/// Provides an ergonomic way to build and execute commands:
/// ```
/// use ass_editor::{EditorDocument, Position, TextCommand};
///
/// let mut doc = EditorDocument::from_content("Hello world!").unwrap();
/// let position = Position::new(5);
///
/// let result = TextCommand::new(&mut doc)
///     .at(position)
///     .insert(" beautiful")
///     .unwrap();
///
/// assert_eq!(doc.text(), "Hello beautiful world!");
/// ```
pub struct TextCommand<'a> {
    document: &'a mut EditorDocument,
    position: Option<Position>,
    range: Option<Range>,
}

impl<'a> TextCommand<'a> {
    /// Create a new text command builder
    pub fn new(document: &'a mut EditorDocument) -> Self {
        Self {
            document,
            position: None,
            range: None,
        }
    }

    /// Set the position for the operation
    #[must_use]
    pub fn at(mut self, position: Position) -> Self {
        self.position = Some(position);
        self
    }

    /// Set the range for the operation
    #[must_use]
    pub fn range(mut self, range: Range) -> Self {
        self.range = Some(range);
        self
    }

    /// Insert text at the current position
    pub fn insert(self, text: &str) -> Result<CommandResult> {
        let position = self
            .position
            .ok_or_else(|| EditorError::command_failed("Position not set for insert operation"))?;

        let command = InsertTextCommand::new(position, text.to_string());
        command.execute(self.document)
    }

    /// Delete text in the current range
    pub fn delete(self) -> Result<CommandResult> {
        let range = self
            .range
            .ok_or_else(|| EditorError::command_failed("Range not set for delete operation"))?;

        let command = DeleteTextCommand::new(range);
        command.execute(self.document)
    }

    /// Replace text in the current range
    pub fn replace(self, new_text: &str) -> Result<CommandResult> {
        let range = self
            .range
            .ok_or_else(|| EditorError::command_failed("Range not set for replace operation"))?;

        let command = ReplaceTextCommand::new(range, new_text.to_string());
        command.execute(self.document)
    }
}

// Extension trait to add fluent command methods to EditorDocument
pub trait DocumentCommandExt {
    /// Start a fluent command chain
    fn command(&mut self) -> TextCommand<'_>;

    /// Quick insert at position
    fn insert_at(&mut self, position: Position, text: &str) -> Result<CommandResult>;

    /// Quick delete range
    fn delete_range(&mut self, range: Range) -> Result<CommandResult>;

    /// Quick replace range
    fn replace_range(&mut self, range: Range, text: &str) -> Result<CommandResult>;
}

impl DocumentCommandExt for EditorDocument {
    fn command(&mut self) -> TextCommand<'_> {
        TextCommand::new(self)
    }

    fn insert_at(&mut self, position: Position, text: &str) -> Result<CommandResult> {
        let command = InsertTextCommand::new(position, text.to_string());
        command.execute(self)
    }

    fn delete_range(&mut self, range: Range) -> Result<CommandResult> {
        let command = DeleteTextCommand::new(range);
        command.execute(self)
    }

    fn replace_range(&mut self, range: Range, text: &str) -> Result<CommandResult> {
        let command = ReplaceTextCommand::new(range, text.to_string());
        command.execute(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::EditorDocument;
    #[cfg(not(feature = "std"))]
    use alloc::string::ToString;
    #[cfg(not(feature = "std"))]
    #[test]
    fn insert_command_execution() {
        let mut doc = EditorDocument::new();
        let command = InsertTextCommand::new(Position::new(0), "Hello".to_string());

        let result = command.execute(&mut doc).unwrap();
        assert!(result.success);
        assert!(result.content_changed);
        assert_eq!(doc.text(), "Hello");
    }

    #[test]
    fn delete_command_execution() {
        let mut doc = EditorDocument::from_content("Hello World").unwrap();
        let range = Range::new(Position::new(6), Position::new(11));
        let command = DeleteTextCommand::new(range);

        let result = command.execute(&mut doc).unwrap();
        assert!(result.success);
        assert!(result.content_changed);
        assert_eq!(doc.text(), "Hello ");
    }

    #[test]
    fn replace_command_execution() {
        let mut doc = EditorDocument::from_content("Hello World").unwrap();
        let range = Range::new(Position::new(6), Position::new(11));
        let command = ReplaceTextCommand::new(range, "Rust".to_string());

        let result = command.execute(&mut doc).unwrap();
        assert!(result.success);
        assert!(result.content_changed);
        assert_eq!(doc.text(), "Hello Rust");
    }

    #[test]
    fn batch_command_execution() {
        let mut doc = EditorDocument::from_content("Hello").unwrap();

        let batch = BatchCommand::new("Insert and replace".to_string())
            .add_command(Box::new(InsertTextCommand::new(
                Position::new(5),
                " World".to_string(),
            )))
            .add_command(Box::new(ReplaceTextCommand::new(
                Range::new(Position::new(0), Position::new(5)),
                "Hi".to_string(),
            )));

        let result = batch.execute(&mut doc).unwrap();
        assert!(result.success);
        assert!(result.content_changed);
        assert_eq!(doc.text(), "Hi World");
    }

    #[test]
    fn fluent_api_usage() {
        let mut doc = EditorDocument::new();

        // Test fluent insertion
        let result = doc.command().at(Position::new(0)).insert("Hello").unwrap();

        assert!(result.success);
        assert_eq!(doc.text(), "Hello");

        // Test fluent replacement
        let range = Range::new(Position::new(0), Position::new(5));
        let result = doc.command().range(range).replace("Hi").unwrap();

        assert!(result.success);
        assert_eq!(doc.text(), "Hi");
    }

    #[test]
    fn document_extension_methods() {
        let mut doc = EditorDocument::new();

        // Test insert_at
        doc.insert_at(Position::new(0), "Hello").unwrap();
        assert_eq!(doc.text(), "Hello");

        // Test replace_range
        let range = Range::new(Position::new(0), Position::new(5));
        doc.replace_range(range, "Hi").unwrap();
        assert_eq!(doc.text(), "Hi");

        // Test delete_range
        let range = Range::new(Position::new(0), Position::new(2));
        doc.delete_range(range).unwrap();
        assert_eq!(doc.text(), "");
    }

    #[test]
    fn command_memory_usage() {
        let insert_cmd = InsertTextCommand::new(Position::new(0), "Hello".to_string());
        let usage = insert_cmd.memory_usage();

        // Should account for the struct size plus string length
        assert!(usage >= core::mem::size_of::<InsertTextCommand>() + 5);
    }
}