endhost-api-models 0.6.0

Connect RPC client library for reqwest based clients
Documentation
// Copyright 2025 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Endhost API models library.

use std::borrow::Cow;

use sciparse::{identifier::isd_asn::IsdAsn, segment::SegmentsPage};

use crate::underlays::Underlays;

pub mod underlays;

/// Underlay discovery trait.
pub trait UnderlayDiscovery: Send + Sync {
    /// List the underlays available to reach the given ISD-AS.
    fn list_underlays(&self, isd_as: IsdAsn) -> Underlays;
}

/// Path segment error.
#[derive(Debug, thiserror::Error)]
pub enum SegmentsError {
    /// Invalid argument.
    #[error("invalid argument: {0}")]
    InvalidArgument(Cow<'static, str>),
    /// Internal error.
    #[error("internal error: {0}")]
    InternalError(Cow<'static, str>),
}

/// Segments discovery trait.
#[async_trait::async_trait]
pub trait SegmentsDiscovery: Send + Sync {
    /// List path segments between the given source and destination ISD-ASes.
    async fn list_segments(
        &self,
        src: IsdAsn,
        dst: IsdAsn,
        page_size: i32,
        page_token: String,
    ) -> Result<SegmentsPage, SegmentsError>;
}

/// Allow sharing a single [SegmentsDiscovery] instance behind an [`Arc`](std::sync::Arc), e.g.
/// between the control plane router and other consumers.
#[async_trait::async_trait]
impl<T: SegmentsDiscovery + ?Sized> SegmentsDiscovery for std::sync::Arc<T> {
    async fn list_segments(
        &self,
        src: IsdAsn,
        dst: IsdAsn,
        page_size: i32,
        page_token: String,
    ) -> Result<SegmentsPage, SegmentsError> {
        (**self)
            .list_segments(src, dst, page_size, page_token)
            .await
    }
}