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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#[cfg(feature = "read_write")]
use crate::read_write::component::Component;
#[cfg(feature = "read_write")]
use crate::read_write::error::FromComponentError;
#[cfg(feature = "sql")]
use crate::SqlConversionError;
#[cfg(feature = "read_write")]
use regex::Regex;
use std::convert::TryFrom;
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Uri {
Url { url: String },
Bytes { val: Vec<u8>, mime: String },
}
impl Uri {
#[must_use]
pub const fn new_url(string: String) -> Self {
Self::Url { url: string }
}
#[must_use]
pub const fn new_bytes(bytes: Vec<u8>, mime: String) -> Self {
Self::Bytes { val: bytes, mime }
}
}
#[cfg(feature = "sql")]
impl Uri {
#[must_use]
pub fn to_sql_raw(
this: Option<Self>,
) -> (Option<String>, Option<Vec<u8>>, Option<String>) {
match this {
Some(photo) => match photo {
Uri::Url { url } => (Some(url), None, None),
Uri::Bytes { val, mime } => (None, Some(val), Some(mime)),
},
None => (None, None, None),
}
}
pub fn try_from_sql_raw(
tup: (Option<String>, Option<Vec<u8>>, Option<String>, String),
) -> Result<Option<Self>, SqlConversionError> {
match tup.0 {
Some(val) => Ok(Some(Self::new_url(val))),
None => match tup.1 {
Some(bin) => match tup.2 {
Some(mime) => Ok(Some(Self::new_bytes(bin, mime))),
None => Err(SqlConversionError::InvalidProperty(format!(
"{3}(Uri) {} is set to null, \n\
but {3}(Uri) {} has the value of {:#?}. A Uri url value has also not been \n\
given and so the enum is invalid.",
"mime", "bin", bin, tup.3
))),
},
None => match tup.2 {
None => Ok(None),
Some(mime) => Err(SqlConversionError::InvalidProperty(format!(
"{3}(Uri) {} is set to null, \n\
but {3}(Uri) {} has the value of {:#?}. A Uri url value has also not been \n\
given and so the enum is invalid.",
"bin", "mime", mime, tup.3
))),
},
},
}
}
}
#[cfg(feature = "read_write")]
impl From<Uri> for Component {
fn from(uri: Uri) -> Self {
match uri {
Uri::Url { url } => Self {
name: "_URI".to_string(),
values: vec![vec![url]],
..Self::default()
},
Uri::Bytes { val, mime } => Self {
name: "_URI".to_string(),
values: vec![
vec![format!("data:{}", mime)],
vec!["base64".to_string(), base64::encode(val)],
],
..Self::default()
},
}
}
}
#[cfg(feature = "read_write")]
impl TryFrom<Component> for Uri {
type Error = FromComponentError;
fn try_from(comp: Component) -> Result<Self, Self::Error> {
lazy_static! {
static ref RE_BIN_1: Regex =
Regex::new(r#"^data:\w+/[\w\+\.-]+$"#).unwrap();
}
match comp.values.len() {
2 => {
let mime = comp.values[0]
.get(0)
.ok_or(FromComponentError::NotEnoughValues)?;
if RE_BIN_1.is_match(mime) {
Ok(Self::Bytes {
mime: mime[5..].to_string(),
val: base64::decode(
comp.values[1]
.get(1)
.ok_or(FromComponentError::NotEnoughValues)?,
).map_err(FromComponentError::Base64DecodeError)?,
})
} else {
Err(FromComponentError::InvalidRegex)
}
}
1 => Ok(Self::Url {
url: comp.values[0]
.get(0)
.ok_or(FromComponentError::NotEnoughValues)?
.to_string(),
}),
_ => Err(FromComponentError::NotEnoughValues),
}
}
}