forked from loco-rs/loco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontents.rs
37 lines (32 loc) · 927 Bytes
/
contents.rs
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
use bytes::Bytes;
pub struct Contents {
data: Bytes,
}
impl From<Bytes> for Contents {
/// Converts a `Vec<u8>` into a `Contents` instance.
///
/// # Returns
///
/// Returns a `Contents` instance with the provided byte data.
fn from(data: Bytes) -> Self {
Self { data }
}
}
impl From<Contents> for Vec<u8> {
/// Convert `Contents` instance int a Vec<u8
fn from(contents: Contents) -> Self {
contents.data.to_vec()
}
}
impl TryFrom<Contents> for String {
type Error = std::string::FromUtf8Error;
/// Tries to convert a `Contents` instance into a `String`.
///
/// # Returns
///
/// Returns a `Result` containing a `String` with the UTF-8 representation
/// of the byte data, or an error if the conversion fails.
fn try_from(contents: Contents) -> Result<Self, Self::Error> {
Self::from_utf8(contents.data.to_vec())
}
}