gearbox/net/http/request/header/
value.rs

1use alloc::{string::String, vec::Vec};
2use serde_derive::{Deserialize, Serialize};
3
4#[derive(Clone, Debug, Serialize, Deserialize)]
5pub struct Value(pub Vec<u8>);
6
7impl Value {
8    pub fn as_bytes(&self) -> &[u8] {
9        &self.0
10    }
11    pub fn to_vec(&self) -> Vec<u8> {
12        self.0.clone()
13    }
14}
15
16impl ToString for Value {
17    fn to_string(&self) -> String {
18        String::from_utf8_lossy(&self.0).to_string()
19    }
20}
21
22impl From<String> for Value {
23    fn from(v: String) -> Self {
24        Value(v.as_bytes().to_vec())
25    }
26}
27
28impl From<&String> for Value {
29    fn from(v: &String) -> Self {
30        Value(v.as_bytes().to_vec())
31    }
32}
33
34impl From<&str> for Value {
35    fn from(v: &str) -> Self {
36        Value(v.as_bytes().to_vec())
37    }
38}
39
40impl TryFrom<Value> for String {
41    type Error = alloc::string::FromUtf8Error;
42    fn try_from(v: Value) -> Result<Self, Self::Error> {
43        String::from_utf8(v.0)
44    }
45}
46
47impl TryFrom<Value> for reqwest::header::HeaderValue {
48    type Error = reqwest::header::InvalidHeaderValue;
49    fn try_from(value: Value) -> Result<Self, Self::Error> {
50        reqwest::header::HeaderValue::try_from(&value)
51    }
52}
53impl TryFrom<&Value> for reqwest::header::HeaderValue {
54    type Error = reqwest::header::InvalidHeaderValue;
55    fn try_from(value: &Value) -> Result<Self, Self::Error> {
56        reqwest::header::HeaderValue::from_bytes(&value.0)
57    }
58}
59
60impl From<reqwest::header::HeaderValue> for Value {
61    fn from(value: reqwest::header::HeaderValue) -> Self {
62        Self::from(&value)
63    }
64}
65impl From<&reqwest::header::HeaderValue> for Value {
66    fn from(value: &reqwest::header::HeaderValue) -> Self {
67        Value(value.as_bytes().to_vec())
68    }
69}