summaryrefslogtreecommitdiff
path: root/rust/crates/commands/src/command.rs
blob: c13468e7c2e24aaa24c767ac96b612031ba61090 (plain) (blame)
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
use scene::{ElementRef, Project};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum CommandError {
    #[error("source track or element not found")]
    SourceNotFound,
    #[error("target track not found")]
    TargetTrackNotFound,
    #[error("{0}")]
    IncompatiblePlacement(String),
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct SelectionPatch {
    pub selected_elements: Vec<ElementRef>,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct CommandResult {
    pub selection: Option<SelectionPatch>,
}

impl CommandResult {
    pub fn element_selection(selected_elements: Vec<ElementRef>) -> Self {
        Self {
            selection: Some(SelectionPatch { selected_elements }),
        }
    }
}

pub trait Command {
    fn execute(
        &mut self,
        project: &mut Project,
    ) -> Result<Option<CommandResult>, CommandError>;

    fn undo(&mut self, project: &mut Project);

    fn redo(
        &mut self,
        project: &mut Project,
    ) -> Result<Option<CommandResult>, CommandError> {
        self.execute(project)
    }
}