Skip to main content

endhost_api_models/
lib.rs

1// Copyright 2025 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//! Endhost API models library.
15
16use std::borrow::Cow;
17
18use sciparse::{identifier::isd_asn::IsdAsn, segment::SegmentsPage};
19
20use crate::underlays::Underlays;
21
22pub mod underlays;
23
24/// Underlay discovery trait.
25pub trait UnderlayDiscovery: Send + Sync {
26    /// List the underlays available to reach the given ISD-AS.
27    fn list_underlays(&self, isd_as: IsdAsn) -> Underlays;
28}
29
30/// Path segment error.
31#[derive(Debug, thiserror::Error)]
32pub enum SegmentsError {
33    /// Invalid argument.
34    #[error("invalid argument: {0}")]
35    InvalidArgument(Cow<'static, str>),
36    /// Internal error.
37    #[error("internal error: {0}")]
38    InternalError(Cow<'static, str>),
39}
40
41/// Segments discovery trait.
42#[async_trait::async_trait]
43pub trait SegmentsDiscovery: Send + Sync {
44    /// List path segments between the given source and destination ISD-ASes.
45    async fn list_segments(
46        &self,
47        src: IsdAsn,
48        dst: IsdAsn,
49        page_size: i32,
50        page_token: String,
51    ) -> Result<SegmentsPage, SegmentsError>;
52}
53
54/// Allow sharing a single [SegmentsDiscovery] instance behind an [`Arc`](std::sync::Arc), e.g.
55/// between the control plane router and other consumers.
56#[async_trait::async_trait]
57impl<T: SegmentsDiscovery + ?Sized> SegmentsDiscovery for std::sync::Arc<T> {
58    async fn list_segments(
59        &self,
60        src: IsdAsn,
61        dst: IsdAsn,
62        page_size: i32,
63        page_token: String,
64    ) -> Result<SegmentsPage, SegmentsError> {
65        (**self)
66            .list_segments(src, dst, page_size, page_token)
67            .await
68    }
69}