Skip to main content

endhost_api_discovery_models/proto/
convert.rs

1// Copyright 2026 Anapaya Systems
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//   http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Conversion implementations between protobuf and native types.
16
17use crate::{
18    EndhostApiGroup, EndhostApiInfo,
19    proto::endhost::discovery::v1::{RpcEndhostApiGroup, RpcEndhostApiInfo},
20};
21
22// EndhostApiInfo
23
24/// Errors that can occur when trying to convert RpcEndhostApiInfo to EndhostApiInfo.
25#[derive(Debug, thiserror::Error, PartialEq, Eq, Clone)]
26pub enum EndhostApiFromRpcError {
27    /// The address field was empty.
28    #[error("address field was empty")]
29    MissingAddress,
30    /// The address field contained an invalid URL.
31    #[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
57// EndhostApiGroup
58
59impl 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}