Skip to main content

common/
cli_actions.rs

1use std::collections::HashMap;
2use std::fmt::{Display, Formatter};
3use serde::{Deserialize, Serialize};
4use crate::fixture::{ChannelIndex, ChannelParameter, ChannelValue, FixtureError, PropertyType};
5
6/// Represents the available command-line interface (CLI) actions that can be dispatched.
7#[derive(Serialize, Deserialize, Debug)]
8pub enum CliAction {
9    /// Displays help information.
10    Help,
11
12    /// Creates a new custom fixture template type with specific properties.
13    FixtureNew {
14        /// Name of the new fixture type template.
15        name: String,
16        /// Mapping of property types to their respective channel parameters.
17        channels: HashMap<PropertyType, ChannelParameter>,
18    },
19
20    /// Spawns an instance of a fixture into a specific universe and channel.
21    FixtureAdd {
22        /// Unique name for the fixture instance.
23        name: String,
24        /// Name of the registered fixture type template to use.
25        fixture_type_name: String,
26        /// Target DMX universe index.
27        universe: usize,
28        /// Starting DMX channel index within the universe.
29        channel: ChannelIndex,
30    },
31
32    /// Relocates an existing fixture instance to a new universe and/or channel.
33    FixtureMove {
34        /// Name of the registered fixture instance to move.
35        fixture_name: String,
36        /// New target DMX universe index.
37        new_universe: usize,
38        /// New starting DMX channel index.
39        new_channel: ChannelIndex,
40    },
41
42    /// Removes an existing fixture instance.
43    FixtureRemove {
44        /// Name of the fixture instance to remove.
45        fixture_name: String,
46    },
47
48    /// Updates a specific property value of a fixture.
49    FixtureSet {
50        /// Name of the target fixture instance.
51        name: String,
52        /// The property type to update.
53        property_type: PropertyType,
54        /// New raw channel value to assign.
55        value: ChannelValue,
56    },
57
58    /// Queries the fixture type name for a given fixture instance.
59    FixtureGetType {
60        /// Name of the fixture instance to query.
61        fixture_name: String,
62    },
63
64    /// Shuts down the application, optionally saving configuration changes.
65    ///
66    /// **Behavior based on `save_changes`:**
67    /// * `Some(false)` – Shuts down immediately without saving changes.
68    /// * `Some(true)` – Saves changes and then shuts down.
69    /// * `None` – Prompts the user interactively to confirm whether to save changes.
70    ///
71    /// **Note:** This command can only be executed within the kernel console.
72    Exit {
73        /// Optional flag indicating whether changes should be saved upon exit.
74        save_changes: Option<bool>,
75    },
76
77    /// Fallback for custom or unparsed command strings, mainly used for debug-commands
78    OtherCommands {
79        /// The raw unparsed command string.
80        command: String,
81    }
82}
83
84/// Represents the response returned after the implicit executing of a [`CliAction`].
85#[derive(Clone, Serialize, Deserialize, Debug)]
86pub enum CliActionResponse {
87    /// Acknowledges successful execution without specific data.
88    Ack,
89
90    /// Returns metadata or type information about a fixture.
91    FixtureTypeInfo(String),
92
93    /// Returned when a fixture-related error occurs.
94    FixtureError(FixtureError),
95
96    /// Returned when the executed command is unrecognized or not supported via implicit command-executing.
97    UnsupportedCommand
98}
99
100impl Display for CliAction {
101    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
102        match self {
103            CliAction::Help =>
104                write!(f, "help"),
105            CliAction::FixtureNew { name, channels } =>
106                write!(f, "new {} {:?}", name, channels),
107            CliAction::FixtureAdd { name, fixture_type_name, universe, channel } =>
108                write!(f, "add {} {} {} {}", name, fixture_type_name, universe, channel),
109            CliAction::FixtureMove { fixture_name, new_universe, new_channel } =>
110                write!(f, "move {} {} {}", fixture_name, new_universe, new_channel),
111            CliAction::FixtureRemove { fixture_name } =>
112                write!(f, "remove {}", fixture_name),
113            CliAction::FixtureSet { name, property_type, value } =>
114                write!(f, "set {} {} {}", name, property_type, value),
115            CliAction::FixtureGetType {fixture_name} =>
116                write!(f, "type {}", fixture_name),
117            CliAction::Exit {save_changes} =>
118                write!(f, "exit {:?}", save_changes),
119            CliAction::OtherCommands {command} =>
120                write!(f, "{}", command),
121        }
122    }
123}
124
125impl Display for CliActionResponse {
126    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
127        match self {
128            CliActionResponse::Ack => write!(f, "ack"),
129            CliActionResponse::FixtureTypeInfo(type_info) => write!(f, "FixtureTypeInfo {}", type_info),
130            CliActionResponse::FixtureError(e) => write!(f, "FixtureError {:?}", e),
131            CliActionResponse::UnsupportedCommand => write!(f, "Unsupported Command"),
132        }
133    }
134}
135