1use serde::{Deserialize, Serialize};
2use std::fmt::{Display, Formatter, Result as FmtResult};
3
4#[derive(Serialize, Deserialize, PartialEq, Debug)]
5pub enum Error<'a> {
6 EnvNotSet(&'a str),
7 EnvMalformed(String, String),
8 JsonMalformed(String),
9 ServiceNotPresent(&'a str),
10 ServiceTypeNotPresent(&'a str),
11 UnknownMemoryUnit,
12}
13
14impl Display for Error<'_> {
15 fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
16 match self {
17 Self::EnvNotSet(variable_name) => write!(
18 formatter,
19 "environment variable {variable_name:?} is not set",
20 ),
21 Self::ServiceNotPresent(service_name) => write!(
22 formatter,
23 "service {service_name:?} is not present in VCAP_SERVICES",
24 ),
25 Self::ServiceTypeNotPresent(service_type_name) => write!(
26 formatter,
27 "service type {service_type_name:?} is not present in VCAP_SERVICES",
28 ),
29 Self::JsonMalformed(variable_to_parse_name) => write!(
30 formatter,
31 "the json from {variable_to_parse_name:?} could not be parsed"
32 ),
33 Self::EnvMalformed(variable_name, comment) => write!(
34 formatter,
35 "the env variable {variable_name:?} does not match the required criterial. {comment:?}",
36 ),
37 Self::UnknownMemoryUnit => write!(formatter, "memory unit unknown"),
38 }
39 }
40}
41
42#[derive(Serialize, Deserialize, PartialEq, Debug)]
43pub enum ByteUnit {
44 Gigabyte,
45 Megabyte,
46}
47
48impl<'a> ByteUnit {
49 pub fn from_string(input: String) -> Result<Self, Error<'a>> {
50 let last_char = input.chars().next_back().unwrap();
51
52 match last_char {
53 'M' | 'm' => Ok(Self::Megabyte),
54 'G' | 'g' => Ok(Self::Gigabyte),
55 _ => Err(Error::UnknownMemoryUnit),
56 }
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 #[test]
63 fn byte_unit_gigabyte() {
64 let unit = crate::ByteUnit::from_string("2G".to_string());
65
66 assert!(unit.is_ok());
67 assert_eq!(unit.unwrap(), crate::ByteUnit::Gigabyte);
68 }
69
70 #[test]
71 fn display_env_not_set() {
72 assert_eq!(
73 format!("{}", crate::Error::EnvNotSet(crate::USER)),
74 format!("environment variable {:?} is not set", crate::USER)
75 );
76 }
77
78 #[test]
79 fn display_json_mal_formed() {
80 assert_eq!(
81 format!("{}", crate::Error::JsonMalformed(crate::USER.to_string())),
82 format!("the json from {:?} could not be parsed", crate::USER)
83 );
84 }
85
86 #[test]
87 fn display_service_not_present() {
88 assert_eq!(
89 format!("{}", crate::Error::ServiceNotPresent(crate::USER)),
90 format!("service {:?} is not present in VCAP_SERVICES", crate::USER)
91 );
92 }
93
94 #[test]
95 fn display_service_type_not_present() {
96 assert_eq!(
97 format!("{}", crate::Error::ServiceTypeNotPresent(crate::USER)),
98 format!(
99 "service type {:?} is not present in VCAP_SERVICES",
100 crate::USER
101 )
102 );
103 }
104
105 #[test]
106 fn display_memory_unit_unknown() {
107 assert_eq!(
108 format!("{}", crate::Error::UnknownMemoryUnit),
109 "memory unit unknown".to_string()
110 );
111 }
112
113 #[test]
114 fn display_env_mal_formed() {
115 assert_eq!(
116 format!(
117 "{}",
118 crate::Error::EnvMalformed(crate::USER.to_string(), "none".to_string())
119 ),
120 format!(
121 "the env variable {:?} does not match the required criterial. \"none\"",
122 crate::USER
123 )
124 );
125 }
126}