use crate::{
EndhostApiGroup, EndhostApiInfo,
proto::endhost::discovery::v1::{RpcEndhostApiGroup, RpcEndhostApiInfo},
};
#[derive(Debug, thiserror::Error, PartialEq, Eq, Clone)]
pub enum EndhostApiFromRpcError {
#[error("address field was empty")]
MissingAddress,
#[error("invalid address: {0}")]
InvalidAddress(#[from] url::ParseError),
}
impl TryFrom<RpcEndhostApiInfo> for EndhostApiInfo {
type Error = EndhostApiFromRpcError;
fn try_from(value: RpcEndhostApiInfo) -> Result<Self, Self::Error> {
if value.address.is_empty() {
return Err(EndhostApiFromRpcError::MissingAddress);
}
let address = value.address.parse()?;
Ok(EndhostApiInfo { address })
}
}
impl From<EndhostApiInfo> for RpcEndhostApiInfo {
fn from(value: EndhostApiInfo) -> Self {
RpcEndhostApiInfo {
address: value.address.to_string(),
}
}
}
impl TryFrom<RpcEndhostApiGroup> for EndhostApiGroup {
type Error = EndhostApiFromRpcError;
fn try_from(value: RpcEndhostApiGroup) -> Result<Self, Self::Error> {
let mut apis = Vec::with_capacity(value.apis.len());
for rpc_api in value.apis {
apis.push(rpc_api.try_into()?);
}
Ok(EndhostApiGroup { apis })
}
}
impl From<EndhostApiGroup> for RpcEndhostApiGroup {
fn from(value: EndhostApiGroup) -> Self {
RpcEndhostApiGroup {
apis: value
.apis
.into_iter()
.map(RpcEndhostApiInfo::from)
.collect(),
}
}
}