Skip to main content

signstar_yubihsm2/automation/
error.rs

1//! Error handling for automation actions.
2
3#[cfg(feature = "cli")]
4use std::fmt::Display;
5use std::path::PathBuf;
6
7#[cfg(feature = "cli")]
8use crate::automation::command::CommandName;
9use crate::automation::command::OpaqueData;
10
11/// A mismatch between a file backed scenario and its return value.
12///
13/// The mismatch is based on the [`CommandName`] used by the file backed scenario and the return
14/// value.
15#[cfg(feature = "cli")]
16#[derive(Debug)]
17pub struct FileBackedScenarioReturnValueMismatch {
18    /// The command name of the file backed scenario.
19    pub(crate) file_backed_scenario_command: CommandName,
20
21    /// The command name of the return value.
22    pub(crate) command_return_value: CommandName,
23}
24
25#[cfg(feature = "cli")]
26impl Display for FileBackedScenarioReturnValueMismatch {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(
29            f,
30            "{} -> {}",
31            self.file_backed_scenario_command, self.command_return_value
32        )
33    }
34}
35
36/// The error that may occur when automating actions against a YubiHSM2 device.
37#[derive(Debug, thiserror::Error)]
38pub enum Error {
39    /// There are mismatches between commands in a file backed scenario and command return values.
40    #[cfg(feature = "cli")]
41    #[error(
42        "Mismatches between commands in file backed scenarios and the collected return values exist:\n{}",
43        mismatches
44            .iter()
45            .map(ToString::to_string)
46            .collect::<Vec<_>>()
47            .join("\n")
48    )]
49    MismatchingReturnValueForFileBackedScenario {
50        /// The mismatches between file backed scenarios and their respective return values.
51        mismatches: Vec<FileBackedScenarioReturnValueMismatch>,
52    },
53
54    /// The number of authenticated command chains differ in a scenario and scenario return value.
55    #[error(
56        "The scenario tracks {scenario} authenticated command chains and the scenario return value {scenario_return_value}"
57    )]
58    MismatchingNumberOfAuthenticatedCommandChains {
59        /// The number of authenticated command chains in a scenario.
60        scenario: usize,
61
62        /// The number of authenticated command chains in a scenario return values.
63        scenario_return_value: usize,
64    },
65
66    /// The number of commands in an authenticated command chain differ from those in a list of
67    /// command return values.
68    #[error(
69        "The authenticated command chain tracks {authenticated_command_chain} commands while the list of command return values is {command_return_values}"
70    )]
71    MismatchingNumberOfCommands {
72        /// The number of commands in an authenticated command chain.
73        authenticated_command_chain: usize,
74
75        /// The number of authenticated command chains in a scenario return values.
76        command_return_values: usize,
77    },
78
79    /// Data for an opaque object is too large.
80    #[error(
81        "The size of the data for an opaque object ({data_length} bytes) is larger than the maximum allowed value ({} bytes)",
82        OpaqueData::MAX_DATA_SIZE
83    )]
84    OpaqueDataLength {
85        /// The length of the data (in bytes).
86        data_length: usize,
87    },
88
89    /// A data file for an opaque object is too large.
90    #[error(
91        "The size of the data file {path} for an opaque object ({data_length} bytes) is larger than the maximum allowed value ({} bytes)",
92        OpaqueData::MAX_DATA_SIZE
93    )]
94    OpaqueDataFileLength {
95        /// The file path.
96        path: PathBuf,
97
98        /// The length of the data (in bytes).
99        data_length: usize,
100    },
101
102    /// A path used for opaque data is not a file.
103    #[error("The path used for opaque data is not a file: {path}")]
104    OpaqueDataNotAFile {
105        /// The file path.
106        path: PathBuf,
107    },
108}