metio/streams/
mod.rs

1/*
2 * Copyright 2024 Bagaluten GmbH <contact@bagaluten.email>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::client::error::Error;
18use crate::client::Metio;
19use crate::types::Event;
20
21/// A stream is a Metio Stream. It should not be confused with
22/// streams in that are used elsewhere in Rust. This is not something
23/// you can call `next` on or iterate over.
24#[derive(Debug, Clone)]
25pub struct Stream {
26    name: String,
27    client: Metio,
28}
29
30impl Stream {
31    pub fn new(client: Metio, name: String) -> Self {
32        Self { name, client }
33    }
34
35    // Get the name of the stream that this object is connected to.
36    pub fn get_name(&self) -> &str {
37        &self.name
38    }
39
40    /// Push a vector of events to the stream.
41    /// Every element will be pushed as a single message to the stream.
42    #[tracing::instrument(skip(events), level = "debug")]
43    pub async fn publish<I>(&self, events: I) -> Result<(), Error>
44    where
45        I: IntoIterator<Item = Event>,
46    {
47        let subject = self.name.clone();
48        self.client.publish(subject, events).await
49    }
50}