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