temporal_sdk_core_protos/
utilities.rs

1use std::collections::HashMap;
2
3use prost::{EncodeError, Message};
4
5pub trait TryIntoOrNone<F, T> {
6    /// Turn an option of something into an option of another thing, trying to convert along the way
7    /// and returning `None` if that conversion fails
8    fn try_into_or_none(self) -> Option<T>;
9}
10
11impl<F, T> TryIntoOrNone<F, T> for Option<F>
12where
13    F: TryInto<T>,
14{
15    fn try_into_or_none(self) -> Option<T> {
16        self.map(TryInto::try_into).transpose().ok().flatten()
17    }
18}
19
20/// Use to encode an message into a proto `Any`.
21///
22/// Delete this once `prost_wkt_types` supports `prost` `0.12.x` which has built-in any packing.
23pub fn pack_any<T: Message>(
24    type_url: String,
25    msg: &T,
26) -> Result<prost_wkt_types::Any, EncodeError> {
27    let mut value = Vec::new();
28    Message::encode(msg, &mut value)?;
29    Ok(prost_wkt_types::Any { type_url, value })
30}
31
32/// Given a header map, lowercase all the keys and return it as a new map.
33/// Any keys that are duplicated after lowercasing will clobber each other in undefined ordering.
34pub fn normalize_http_headers(headers: HashMap<String, String>) -> HashMap<String, String> {
35    let mut new_headers = HashMap::new();
36    for (header_key, val) in headers.into_iter() {
37        new_headers.insert(header_key.to_lowercase(), val);
38    }
39    new_headers
40}