Skip to main content

scion_stack/path/
types.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
15//! Path Manager specific types.
16
17use std::time::SystemTime;
18
19use scion_proto::path::{DataPlanePathFingerprint, Path};
20
21use crate::path::manager::reliability::ReliabilityScore;
22
23/// Entry in the path set cache.
24#[derive(Debug)]
25pub struct PathManagerPath {
26    /// The underlying SCION path.
27    pub path: Path,
28    /// The fingerprint of the path.
29    pub fingerprint: DataPlanePathFingerprint,
30    /// The reliability score of the path.
31    pub reliability: ReliabilityScore,
32}
33
34impl PathManagerPath {
35    /// Wrap a scion path with metadata
36    pub fn new(path: Path) -> Self {
37        let fingerprint = path.data_plane_fingerprint();
38
39        Self {
40            path,
41            fingerprint,
42            reliability: ReliabilityScore::new_with_time(SystemTime::now()),
43        }
44    }
45
46    /// Get the underlying scion path
47    pub fn scion_path(&self) -> &Path {
48        &self.path
49    }
50}
51
52impl From<&Path> for PathManagerPath {
53    fn from(path: &Path) -> Self {
54        Self::new(path.clone())
55    }
56}
57
58/// A Score represents a floating point score for path ranking.
59///
60/// Higher scores indicate more preferred paths.
61/// Lower scores indicate less preferred paths.
62///
63/// Scores are clamped between -1.0 and 1.0.
64#[derive(Debug, Clone, Copy, PartialEq)]
65pub struct Score(f32);
66
67impl Eq for Score {}
68
69impl Ord for Score {
70    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
71        self.0
72            .partial_cmp(&other.0)
73            .unwrap_or(std::cmp::Ordering::Equal)
74    }
75}
76impl PartialOrd for Score {
77    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
78        Some(self.cmp(other))
79    }
80}
81
82impl Score {
83    /// Creates a new Score, clamping the value between -1.0 and 1.0.
84    pub fn new_clamped(value: f32) -> Self {
85        let value = match value.is_nan() {
86            true => 0.0,
87            false => value,
88        };
89        Score(value.clamp(-1.0, 1.0))
90    }
91
92    /// Returns the inner floating point value of the score.
93    pub fn value(&self) -> f32 {
94        self.0
95    }
96}