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::{EndhostApiInfo, proto::endhost::discovery::v1::RpcEndhostApiInfo};
18
19/// Errors that can occur when trying to convert RpcEndhostApiInfo to EndhostApiInfo.
20#[derive(Debug, thiserror::Error, PartialEq, Eq, Clone)]
21pub enum EndhostApiFromRpcError {
22 /// The address field was empty.
23 #[error("address field was empty")]
24 MissingAddress,
25 /// The address field contained an invalid URL.
26 #[error("invalid address: {0}")]
27 InvalidAddress(#[from] url::ParseError),
28}
29
30impl TryFrom<RpcEndhostApiInfo> for EndhostApiInfo {
31 type Error = EndhostApiFromRpcError;
32
33 fn try_from(value: RpcEndhostApiInfo) -> Result<Self, Self::Error> {
34 if value.address.is_empty() {
35 return Err(EndhostApiFromRpcError::MissingAddress);
36 }
37
38 let address = value.address.parse()?;
39
40 Ok(EndhostApiInfo { address })
41 }
42}
43
44impl From<EndhostApiInfo> for RpcEndhostApiInfo {
45 fn from(value: EndhostApiInfo) -> Self {
46 RpcEndhostApiInfo {
47 address: value.address.to_string(),
48 }
49 }
50}