Skip to main content

nominal_streaming/
types.rs

1use std::collections::BTreeMap;
2use std::time::Duration;
3
4use conjure_object::BearerToken;
5use nominal_api::api::rids::WorkspaceRid;
6use nominal_api::tonic::google::protobuf::Timestamp;
7use nominal_api::tonic::io::nominal::scout::api::proto::points::PointsType;
8use nominal_api::tonic::io::nominal::scout::api::proto::DoublePoint;
9use nominal_api::tonic::io::nominal::scout::api::proto::DoublePoints;
10use nominal_api::tonic::io::nominal::scout::api::proto::IntegerPoint;
11use nominal_api::tonic::io::nominal::scout::api::proto::IntegerPoints;
12use nominal_api::tonic::io::nominal::scout::api::proto::StringPoint;
13use nominal_api::tonic::io::nominal::scout::api::proto::StringPoints;
14use nominal_api::tonic::io::nominal::scout::api::proto::StructPoint;
15use nominal_api::tonic::io::nominal::scout::api::proto::StructPoints;
16use nominal_api::tonic::io::nominal::scout::api::proto::Uint64Point;
17use nominal_api::tonic::io::nominal::scout::api::proto::Uint64Points;
18
19const NANOS_PER_SECOND: i64 = 1_000_000_000;
20
21/// A descriptor for a channel.
22///
23/// Note that this is used internally to compare channels.
24#[derive(Clone, Debug, Eq, Hash, PartialEq, Ord, PartialOrd)]
25pub struct ChannelDescriptor {
26    /// The name of the channel.
27    pub name: String,
28    /// The tags associated with the channel, if any.
29    pub tags: Option<BTreeMap<String, String>>,
30}
31
32impl ChannelDescriptor {
33    /// Creates a new channel descriptor from the given `name`.
34    ///
35    /// If you would like to include tags, see also [`Self::with_tags`].
36    pub fn new(name: impl Into<String>) -> Self {
37        Self {
38            name: name.into(),
39            tags: None,
40        }
41    }
42
43    /// Creates a new channel descriptor from the given `name` and `tags`.
44    pub fn with_tags(
45        name: impl Into<String>,
46        tags: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
47    ) -> Self {
48        Self {
49            name: name.into(),
50            tags: Some(
51                tags.into_iter()
52                    .map(|(key, value)| (key.into(), value.into()))
53                    .collect(),
54            ),
55        }
56    }
57}
58
59pub trait AuthProvider: Clone + Send + Sync {
60    fn token(&self) -> Option<BearerToken>;
61
62    fn workspace_rid(&self) -> Option<WorkspaceRid> {
63        None
64    }
65}
66
67pub trait IntoPoints {
68    fn into_points(self) -> PointsType;
69}
70
71impl IntoPoints for PointsType {
72    fn into_points(self) -> PointsType {
73        self
74    }
75}
76
77impl IntoPoints for Vec<DoublePoint> {
78    fn into_points(self) -> PointsType {
79        PointsType::DoublePoints(DoublePoints { points: self })
80    }
81}
82
83impl IntoPoints for Vec<StringPoint> {
84    fn into_points(self) -> PointsType {
85        PointsType::StringPoints(StringPoints { points: self })
86    }
87}
88
89impl IntoPoints for Vec<IntegerPoint> {
90    fn into_points(self) -> PointsType {
91        PointsType::IntegerPoints(IntegerPoints { points: self })
92    }
93}
94
95impl IntoPoints for Vec<StructPoint> {
96    fn into_points(self) -> PointsType {
97        PointsType::StructPoints(StructPoints { points: self })
98    }
99}
100
101impl IntoPoints for Vec<Uint64Point> {
102    fn into_points(self) -> PointsType {
103        PointsType::Uint64Points(Uint64Points { points: self })
104    }
105}
106
107pub trait IntoTimestamp {
108    fn into_timestamp(self) -> Timestamp;
109}
110
111impl IntoTimestamp for Duration {
112    fn into_timestamp(self) -> Timestamp {
113        Timestamp {
114            seconds: self.as_secs() as i64,
115            nanos: self.subsec_nanos() as i32,
116        }
117    }
118}
119
120impl<T: chrono::TimeZone> IntoTimestamp for chrono::DateTime<T> {
121    fn into_timestamp(self) -> Timestamp {
122        Timestamp {
123            seconds: self.timestamp(),
124            nanos: self.timestamp_subsec_nanos() as i32,
125        }
126    }
127}
128
129impl IntoTimestamp for i64 {
130    fn into_timestamp(self) -> Timestamp {
131        Timestamp {
132            seconds: (self / NANOS_PER_SECOND),
133            nanos: (self % NANOS_PER_SECOND) as i32,
134        }
135    }
136}