azure_core/request_options/
metadata.rs
1use crate::headers;
2use crate::headers::Headers;
3use crate::Header;
4use bytes::Bytes;
5use std::collections::HashMap;
6
7#[derive(Debug, Clone)]
8pub struct Metadata(HashMap<String, Bytes>);
9
10impl Default for Metadata {
11 fn default() -> Self {
12 Self::new()
13 }
14}
15
16impl AsMut<HashMap<String, Bytes>> for Metadata {
17 fn as_mut(&mut self) -> &mut HashMap<String, Bytes> {
18 &mut self.0
19 }
20}
21
22impl Metadata {
23 pub fn new() -> Self {
24 Self(HashMap::new())
25 }
26
27 pub fn insert<K, V>(&mut self, k: K, v: V) -> Option<Bytes>
28 where
29 K: Into<String>,
30 V: Into<Bytes>,
31 {
32 self.0.insert(k.into(), v.into())
33 }
34
35 pub fn len(&self) -> usize {
36 self.0.len()
37 }
38
39 pub fn is_empty(&self) -> bool {
40 self.0.is_empty()
41 }
42
43 pub fn get(&self, k: &str) -> Option<Bytes> {
44 self.0.get(k).cloned()
45 }
46
47 pub fn iter(&self) -> impl Iterator<Item = Metadatum> + '_ {
48 self.0.iter().map(|(key, value)| {
49 Metadatum(
50 key.clone(),
51 std::str::from_utf8(value)
52 .expect("non-utf8 header value")
53 .into(),
54 )
55 })
56 }
57}
58
59#[derive(Debug)]
60pub struct Metadatum(String, String);
61
62impl Header for Metadatum {
63 fn name(&self) -> headers::HeaderName {
64 format!("x-ms-meta-{}", self.0).into()
65 }
66
67 fn value(&self) -> headers::HeaderValue {
68 self.1.clone().into()
69 }
70}
71
72impl From<&Headers> for Metadata {
73 fn from(header_map: &Headers) -> Self {
74 let mut metadata = Metadata::new();
75 header_map.iter().for_each(|(name, value)| {
76 let name = name.as_str();
77 let value = value.as_str();
78 if let Some(name) = name.strip_prefix("x-ms-meta-") {
79 metadata.insert(name.to_owned(), value.to_owned());
80 }
81 });
82
83 metadata
84 }
85}