Skip to main content

signstar_yubihsm2/automation/
error.rs

1//! Error handling for automation actions.
2
3#[cfg(feature = "cli")]
4use std::fmt::Display;
5
6#[cfg(feature = "cli")]
7use crate::automation::command::CommandName;
8
9/// A mismatch between a file backed scenario and its return value.
10///
11/// The mismatch is based on the [`CommandName`] used by the file backed scenario and the return
12/// value.
13#[cfg(feature = "cli")]
14#[derive(Debug)]
15pub struct FileBackedScenarioReturnValueMismatch {
16    /// The command name of the file backed scenario.
17    pub(crate) file_backed_scenario_command: CommandName,
18
19    /// The command name of the return value.
20    pub(crate) command_return_value: CommandName,
21}
22
23#[cfg(feature = "cli")]
24impl Display for FileBackedScenarioReturnValueMismatch {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        write!(
27            f,
28            "{} -> {}",
29            self.file_backed_scenario_command, self.command_return_value
30        )
31    }
32}
33
34/// The error that may occur when automating actions against a YubiHSM2 device.
35#[derive(Debug, thiserror::Error)]
36pub enum Error {
37    /// There are mismatches between commands in a file backed scenario and command return values.
38    #[cfg(feature = "cli")]
39    #[error(
40        "Mismatches between commands in file backed scenarios and the collected return values exist:\n{}",
41        mismatches
42            .iter()
43            .map(ToString::to_string)
44            .collect::<Vec<_>>()
45            .join("\n")
46    )]
47    MismatchingReturnValueForFileBackedScenario {
48        /// The mismatches between file backed scenarios and their respective return values.
49        mismatches: Vec<FileBackedScenarioReturnValueMismatch>,
50    },
51
52    /// The number of authenticated command chains differ in a scenario and scenario return value.
53    #[error(
54        "The scenario tracks {scenario} authenticated command chains and the scenario return value {scenario_return_value}"
55    )]
56    MismatchingNumberOfAuthenticatedCommandChains {
57        /// The number of authenticated command chains in a scenario.
58        scenario: usize,
59
60        /// The number of authenticated command chains in a scenario return values.
61        scenario_return_value: usize,
62    },
63
64    /// The number of commands in an authenticated command chain differ from those in a list of
65    /// command return values.
66    #[error(
67        "The authenticated command chain tracks {authenticated_command_chain} commands while the list of command return values is {command_return_values}"
68    )]
69    MismatchingNumberOfCommands {
70        /// The number of commands in an authenticated command chain.
71        authenticated_command_chain: usize,
72
73        /// The number of authenticated command chains in a scenario return values.
74        command_return_values: usize,
75    },
76}