endhost_api_discovery_models/proto/
convert.rs1use crate::{
18 EndhostApiGroup, EndhostApiInfo,
19 proto::endhost::discovery::v1::{RpcEndhostApiGroup, RpcEndhostApiInfo},
20};
21
22#[derive(Debug, thiserror::Error, PartialEq, Eq, Clone)]
26pub enum EndhostApiFromRpcError {
27 #[error("address field was empty")]
29 MissingAddress,
30 #[error("invalid address: {0}")]
32 InvalidAddress(#[from] url::ParseError),
33}
34
35impl TryFrom<RpcEndhostApiInfo> for EndhostApiInfo {
36 type Error = EndhostApiFromRpcError;
37
38 fn try_from(value: RpcEndhostApiInfo) -> Result<Self, Self::Error> {
39 if value.address.is_empty() {
40 return Err(EndhostApiFromRpcError::MissingAddress);
41 }
42
43 let address = value.address.parse()?;
44
45 Ok(EndhostApiInfo { address })
46 }
47}
48
49impl From<EndhostApiInfo> for RpcEndhostApiInfo {
50 fn from(value: EndhostApiInfo) -> Self {
51 RpcEndhostApiInfo {
52 address: value.address.to_string(),
53 }
54 }
55}
56
57impl TryFrom<RpcEndhostApiGroup> for EndhostApiGroup {
60 type Error = EndhostApiFromRpcError;
61
62 fn try_from(value: RpcEndhostApiGroup) -> Result<Self, Self::Error> {
63 let mut apis = Vec::with_capacity(value.apis.len());
64 for rpc_api in value.apis {
65 apis.push(rpc_api.try_into()?);
66 }
67
68 Ok(EndhostApiGroup { apis })
69 }
70}
71
72impl From<EndhostApiGroup> for RpcEndhostApiGroup {
73 fn from(value: EndhostApiGroup) -> Self {
74 RpcEndhostApiGroup {
75 apis: value
76 .apis
77 .into_iter()
78 .map(RpcEndhostApiInfo::from)
79 .collect(),
80 }
81 }
82}