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
//! Command trait and helper functions.
//!
//!
use crate::{
error::{ErrorCode, Result},
parser::{parameters::Parameters, response::ResponseUnit},
Context, Device,
};
/// Marks the command as query only by creating a stub for [Command::meta].
///
/// ```
/// # use scpi::{cmd_qonly, tree::prelude::*};
/// # struct MyDevice;
/// # impl Device for MyDevice {
/// # fn handle_error(&mut self, _: scpi::error::Error) { todo!() }
/// # }
///
/// struct MyQuery;
/// impl Command<MyDevice> for MyQuery {
/// cmd_qonly!();
///
/// fn query(
/// &self,
/// _device: &mut MyDevice,
/// _context: &mut Context,
/// _params: Parameters,
/// _response: ResponseUnit,
/// ) -> Result<(), Error> {
/// // Do stuff
/// Ok(())
/// }
/// }
/// ```
#[macro_export]
macro_rules! cmd_qonly {
() => {
fn meta(&self) -> $crate::tree::prelude::CommandTypeMeta {
$crate::tree::prelude::CommandTypeMeta::QueryOnly
}
};
}
/// Marks the command as not queryable by creating a stub for [Command::meta].
///
/// ```
/// # use scpi::{cmd_nquery, tree::prelude::*};
/// # struct MyDevice;
/// # impl Device for MyDevice {
/// # fn handle_error(&mut self, _: scpi::error::Error) { todo!() }
/// # }
///
/// struct MyQuery;
/// impl Command<MyDevice> for MyQuery {
/// cmd_nquery!();
///
/// fn event(
/// &self,
/// _device: &mut MyDevice,
/// _context: &mut Context,
/// _params: Parameters,
/// ) -> Result<(), Error> {
/// // Do stuff
/// Ok(())
/// }
/// }
/// ```
#[macro_export]
macro_rules! cmd_nquery {
() => {
fn meta(&self) -> $crate::tree::prelude::CommandTypeMeta {
$crate::tree::prelude::CommandTypeMeta::NoQuery
}
};
}
/// Marks the command as both query and event by creating a stub for [Command::meta].
///
/// ```
/// # use scpi::{cmd_both, tree::prelude::*};
/// # struct MyDevice;
/// # impl Device for MyDevice {
/// # fn handle_error(&mut self, _: scpi::error::Error) { todo!() }
/// # }
///
/// struct MyQuery;
/// impl Command<MyDevice> for MyQuery {
/// cmd_both!();
///
/// fn query(
/// &self,
/// _device: &mut MyDevice,
/// _context: &mut Context,
/// _params: Parameters,
/// _response: ResponseUnit,
/// ) -> Result<(), Error> {
/// // Do stuff
/// Ok(())
/// }
///
/// fn event(
/// &self,
/// _device: &mut MyDevice,
/// _context: &mut Context,
/// _params: Parameters,
/// ) -> Result<(), Error> {
/// // Do stuff
/// Ok(())
/// }
/// }
/// ```
#[macro_export]
macro_rules! cmd_both {
() => {
fn meta(&self) -> $crate::tree::prelude::CommandTypeMeta {
$crate::tree::prelude::CommandTypeMeta::Both
}
};
}
/// This trait implements a command with event/query operations.
/// # Example
/// ```
/// use scpi::{error::Result, tree::prelude::*, cmd_both};
///
/// struct MyCommand;
/// impl<D> Command<D> for MyCommand
/// where
/// D: Device,// or MyDevice or Device + AdditionalTrait or ...
/// {
/// // Create a stub for Command::meta()
/// cmd_both!();
///
/// // `HELLo:WORLd`
/// fn event(&self, _device: &mut D, _context: &mut Context, _params: Parameters) -> Result<()> {
/// // Do stuff
/// println!("Hello world");
/// Ok(())
/// }
///
/// // `HELLo:WORLd?`
/// fn query(
/// &self,
/// _device: &mut D,
/// _context: &mut Context,
/// _params: Parameters,
/// mut response: ResponseUnit,
/// ) -> Result<()> {
/// response.data(&b"Hello world"[..]).finish()
/// }
/// }
/// ```
///
/// The default stubs for [Command::event] and [Command::query] returns an [ErrorCode::UndefinedHeader] error.
///
/// ## Naming convention
/// A command impl should be named in PascalCase after the shortform header mnemonics upp to the last which should be in longform.
/// Common commands should be named as-is without '*' obv.
///
/// Examples:
/// * `INITiate[:IMMediate][:ALL]` => `InitImmAllCommand`
/// * `SYSTem:ERRor[:NEXT]` => `SystErrNextCommand`
/// * `SENSe:VOLTage[:DC]:NPLCycles` => `SensVoltDcNPLCyclesCommand`
/// * `*TRG` => `TrgCommand`
///
/// Sometimes a command is re-used in multiple branches, in that case one might skip the changing branches in the name.
/// Generics may be used to specialize the command.
/// * `SENSe(:VOLTage|:CURRent|..)([:DC]|:AC):RESolution` => `SensResolutionCommand` or `SensResolutionCommand<Func>`
///
/// When instantaiting more than one command it is recommended to use a command constant/member or const generics, i.e. like this:
/// * `OUTPut[<n>]:ATTenuation` => `OutpAttenuationCommand<const N: usize = 1>`
/// * `ARM:SEQuence[<n1>]:LAYer[<n2>][:IMMediate]` => `ArmSeqLayImmediateCommand { sequence: n1, layer: n2 }`
///
/// All of these forms may also be combined for extra headache.
///
/// In the end readability overrules the above conventions if the name gets too verbose...
pub trait Command<D: Device> {
/// Hint about the allowed forms this command allows.
///
/// Not actually binding in any way but can be used to provide autocompletion and help info.
/// Use [cmd_nquery!], [cmd_qonly!], or [cmd_both!] to automatically create the corresponding stub.
fn meta(&self) -> CommandTypeMeta {
CommandTypeMeta::Unknown
}
/// Called when the event form `COMmand` is used.
///
/// Default behaviour returns a [ErrorCode::UndefinedHeader] error.
fn event(&self, _device: &mut D, _context: &mut Context, _params: Parameters) -> Result<()> {
Err(ErrorCode::UndefinedHeader.into())
}
///Called when the query form `COMmand?` is used
///
/// Default behaviour returns a [ErrorCode::UndefinedHeader] error.
fn query(
&self,
_device: &mut D,
_context: &mut Context,
_params: Parameters,
_resp: ResponseUnit,
) -> Result<()> {
Err(ErrorCode::UndefinedHeader.into())
}
}
/// Dummy node which calls [todo!] on event and query.
///
/// Indicates an unfinished command similar to the [todo!] macro.
pub struct Todo;
impl<D> Command<D> for Todo
where
D: Device,
{
fn event(&self, _device: &mut D, _context: &mut Context, _params: Parameters) -> Result<()> {
todo!()
}
fn query(
&self,
_device: &mut D,
_context: &mut Context,
_params: Parameters,
_response: ResponseUnit,
) -> Result<()> {
todo!()
}
}
/// Hint about the command forms.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum CommandTypeMeta {
/// Not known
Unknown,
/// Query not allowed
NoQuery,
/// Only query allowed
QueryOnly,
/// Both query and event are allowed
Both,
}
#[cfg(test)]
mod test_command {
use crate::tests::fixture_device;
use super::*;
struct TestCommandDevice;
fixture_device!(TestCommandDevice);
struct Query;
impl Command<TestCommandDevice> for Query {
cmd_qonly!();
fn query(
&self,
_device: &mut TestCommandDevice,
_context: &mut Context,
_params: Parameters,
_response: ResponseUnit,
) -> Result<()> {
Ok(())
}
}
#[test]
fn test_query() {
assert_eq!(Query {}.meta(), CommandTypeMeta::QueryOnly);
}
struct Event;
impl Command<TestCommandDevice> for Event {
cmd_nquery!();
fn event(
&self,
_device: &mut TestCommandDevice,
_context: &mut Context,
_params: Parameters,
) -> Result<()> {
Ok(())
}
}
#[test]
fn test_event() {
assert_eq!(Event {}.meta(), CommandTypeMeta::NoQuery);
}
struct Default;
impl Command<TestCommandDevice> for Default {
fn event(
&self,
_device: &mut TestCommandDevice,
_context: &mut Context,
_params: Parameters,
) -> Result<()> {
Ok(())
}
fn query(
&self,
_device: &mut TestCommandDevice,
_context: &mut Context,
_params: Parameters,
_response: ResponseUnit,
) -> Result<()> {
Ok(())
}
}
#[test]
fn test_default() {
assert_eq!(Default {}.meta(), CommandTypeMeta::Unknown);
}
}