1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub enum Error {
EnvNotSet(String),
EnvMalformed(String, String),
JsonMalformed(String),
ServiceNotPresent(String),
ServiceTypeNotPresent(String),
UnkownMemoryUnit,
}
impl Display for Error {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
match &*self {
Self::EnvNotSet(variable_name) => write!(
formatter,
"environment variable {:?} is not set",
variable_name
),
Self::ServiceNotPresent(service_name) => write!(
formatter,
"service {:?} is not present in VCAP_SERVICES",
service_name
),
Self::ServiceTypeNotPresent(service_type_name) => write!(
formatter,
"service type {:?} is not present in VCAP_SERVICES",
service_type_name
),
Self::JsonMalformed(variable_to_parse_name) => write!(
formatter,
"the json from {:?} could not be parsed",
variable_to_parse_name
),
Self::EnvMalformed(variable_name, comment) => write!(
formatter,
"the env variable {:?} does not match the required criterial. {:?}",
variable_name, comment
),
Self::UnkownMemoryUnit => write!(formatter, "memory unit unkown"),
}
}
}
pub enum ByteUnit {
Gigabyte,
Megabyte,
}
impl ByteUnit {
pub fn from_string(input: String) -> Result<Self, Error> {
let last_char = input.chars().rev().next().unwrap();
match last_char {
'M' | 'm' => Ok(Self::Megabyte),
'G' | 'g' => Ok(Self::Gigabyte),
_ => Err(Error::UnkownMemoryUnit),
}
}
}