junction_api/xds/
shared.rs1use std::str::FromStr;
2
3use crate::{error::Error, shared::Regex, Duration};
4use std::time::Duration as StdDuration;
5
6pub(crate) fn parse_xds_regex(
7 p: &xds_api::pb::envoy::r#type::matcher::v3::RegexMatcher,
8) -> Result<Regex, Error> {
9 Regex::from_str(&p.regex).map_err(|e| Error::new(format!("invalid regex: {e}")))
10}
11
12pub(crate) fn regex_matcher(
13 regex: &Regex,
14) -> xds_api::pb::envoy::r#type::matcher::v3::RegexMatcher {
15 xds_api::pb::envoy::r#type::matcher::v3::RegexMatcher {
16 regex: regex.to_string(),
17 engine_type: None,
18 }
19}
20
21impl TryFrom<xds_api::pb::google::protobuf::Duration> for Duration {
22 type Error = Error;
23
24 fn try_from(
25 proto_duration: xds_api::pb::google::protobuf::Duration,
26 ) -> Result<Self, Self::Error> {
27 let duration: StdDuration = proto_duration
28 .try_into()
29 .map_err(|e| Error::new(format!("invalid duration: {e}")))?;
30
31 Ok(duration.into())
32 }
33}
34
35impl TryFrom<Duration> for xds_api::pb::google::protobuf::Duration {
36 type Error = std::num::TryFromIntError;
37
38 fn try_from(value: Duration) -> Result<Self, Self::Error> {
39 let seconds = value.as_secs().try_into()?;
40 let nanos = value.subsec_nanos().try_into()?;
41 Ok(xds_api::pb::google::protobuf::Duration { seconds, nanos })
42 }
43}