xref: /aosp_15_r20/tools/security/remote_provisioning/hwtrust/src/dice/profile.rs (revision d9ecfb0f4d734c9ce41cde8ac4d585b094fd4222)
1 use std::error::Error;
2 use std::fmt::{self, Display, Formatter};
3 use std::str::FromStr;
4 
5 /// Versions of the Android Profile for DICE.
6 #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
7 pub enum ProfileVersion {
8     /// The version of the Android Profile for DICE that aligns with Android 13.
9     Android13,
10     /// The version of the Android Profile for DICE that aligns with Android 14.
11     #[default]
12     Android14,
13     /// The version of the Android Profile for DICE that aligns with Android 15.
14     Android15,
15     /// The version of the Android Profile for DICE that aligns with Android 16.
16     Android16,
17 }
18 
19 impl Display for ProfileVersion {
fmt(&self, f: &mut Formatter) -> fmt::Result20     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
21         let profile_name = match self {
22             Self::Android13 => "android.13",
23             Self::Android14 => "android.14",
24             Self::Android15 => "android.15",
25             Self::Android16 => "android.16",
26         };
27         write!(f, "{profile_name}",)
28     }
29 }
30 
31 /// An error which can be returned when parsing an Android Profile for DICE version.
32 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
33 pub struct ParseProfileVersionError(());
34 
35 impl fmt::Display for ParseProfileVersionError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result36     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
37         write!(fmt, "unknown profile version")
38     }
39 }
40 
41 impl Error for ParseProfileVersionError {}
42 
43 impl FromStr for ProfileVersion {
44     type Err = ParseProfileVersionError;
45 
from_str(s: &str) -> Result<Self, Self::Err>46     fn from_str(s: &str) -> Result<Self, Self::Err> {
47         Ok(match s {
48             "android.14" => Self::Android14,
49             "android.15" => Self::Android15,
50             "android.16" => Self::Android16,
51             _ => return Err(ParseProfileVersionError(())),
52         })
53     }
54 }
55