nominal_streaming/
types.rs

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