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