1#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
4pub enum Operation {
5 MakeCredential,
6 GetAssertion,
7 GetNextAssertion,
8 GetInfo,
9 ClientPin,
10 Reset,
11 BioEnrollment,
13 CredentialManagement,
14 Selection,
15 LargeBlobs,
16 Config,
17 PreviewBioEnrollment,
18 PreviewCredentialManagement,
19 Vendor(VendorOperation),
21}
22
23impl From<Operation> for u8 {
24 fn from(operation: Operation) -> u8 {
25 use Operation::*;
26 match operation {
27 MakeCredential => 0x01,
28 GetAssertion => 0x02,
29 GetNextAssertion => 0x08,
30 GetInfo => 0x04,
31 ClientPin => 0x06,
32 Reset => 0x07,
33 BioEnrollment => 0x09,
34 CredentialManagement => 0x0A,
35 Selection => 0x0B,
36 LargeBlobs => 0x0C,
37 Config => 0x0D,
38 PreviewBioEnrollment => 0x40,
39 PreviewCredentialManagement => 0x41,
40 Vendor(operation) => operation.into(),
41 }
42 }
43}
44
45impl Operation {
46 pub fn into_u8(self) -> u8 {
47 self.into()
48 }
49}
50
51#[derive(Copy, Clone, Debug, Eq, PartialEq)]
53#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
54pub struct VendorOperation(u8);
55
56impl VendorOperation {
57 pub const FIRST: u8 = 0x40;
58 pub const LAST: u8 = 0x7f;
59}
60
61impl TryFrom<u8> for VendorOperation {
62 type Error = ();
63
64 fn try_from(from: u8) -> core::result::Result<Self, ()> {
65 match from {
66 code @ Self::FIRST..=Self::LAST => Ok(VendorOperation(code)),
67 _ => Err(()),
68 }
69 }
70}
71
72impl From<VendorOperation> for u8 {
73 fn from(operation: VendorOperation) -> u8 {
74 operation.0
75 }
76}
77
78impl TryFrom<u8> for Operation {
79 type Error = ();
80
81 fn try_from(from: u8) -> core::result::Result<Operation, ()> {
82 use Operation::*;
83 Ok(match from {
84 0x01 => MakeCredential,
85 0x02 => GetAssertion,
86 0x08 => GetNextAssertion,
87 0x04 => GetInfo,
88 0x06 => ClientPin,
89 0x07 => Reset,
90 0x09 => BioEnrollment,
91 0x0A => CredentialManagement,
92 0x0B => Selection,
93 0x0C => LargeBlobs,
94 0x0D => Config,
95 0x40 => PreviewBioEnrollment,
96 0x41 => PreviewCredentialManagement,
97 code @ VendorOperation::FIRST..=VendorOperation::LAST => {
98 Vendor(VendorOperation::try_from(code)?)
99 }
100 _ => return Err(()),
101 })
102 }
103}