zenoh_flow/model/descriptor/node/
source.rs

1//
2// Copyright (c) 2021 - 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15use crate::prelude::PortId;
16use crate::types::{Configuration, NodeId};
17use crate::zferror;
18use crate::zfresult::{ErrorKind, ZFResult as Result};
19use serde::{Deserialize, Serialize};
20
21/// Describes a source.
22///
23/// Example:
24///
25///
26/// ```yaml
27/// id : PrintSink
28/// uri: file://./target/release/libcounter_source.so
29/// configuration:
30///   start: 10
31/// outputs: [Counter]
32/// ```
33///
34#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
35pub struct SourceDescriptor {
36    pub id: NodeId,
37    pub outputs: Vec<PortId>,
38    pub uri: Option<String>,
39    pub configuration: Option<Configuration>,
40}
41
42impl std::fmt::Display for SourceDescriptor {
43    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
44        write!(f, "{} - Kind: Source", self.id)
45    }
46}
47
48impl SourceDescriptor {
49    /// Creates a new `SourceDescriptor` from its YAML representation.
50    ///
51    ///  # Errors
52    /// A variant error is returned if deserialization fails.
53    pub fn from_yaml(data: &str) -> Result<Self> {
54        let dataflow_descriptor = serde_yaml::from_str::<SourceDescriptor>(data)
55            .map_err(|e| zferror!(ErrorKind::ParsingError, e))?;
56        Ok(dataflow_descriptor)
57    }
58
59    /// Creates a new `SourceDescriptor` from its JSON representation.
60    ///
61    ///  # Errors
62    /// A variant error is returned if deserialization fails.
63    pub fn from_json(data: &str) -> Result<Self> {
64        let dataflow_descriptor = serde_json::from_str::<SourceDescriptor>(data)
65            .map_err(|e| zferror!(ErrorKind::ParsingError, e))?;
66        Ok(dataflow_descriptor)
67    }
68
69    /// Returns the JSON representation of the `SourceDescriptor`.
70    ///
71    ///  # Errors
72    /// A variant error is returned if serialization fails.
73    pub fn to_json(&self) -> Result<String> {
74        serde_json::to_string(&self).map_err(|e| zferror!(ErrorKind::SerializationError, e).into())
75    }
76
77    /// Returns the YAML representation of the `SourceDescriptor`.
78    ///
79    ///  # Errors
80    /// A variant error is returned if serialization fails.
81    pub fn to_yaml(&self) -> Result<String> {
82        serde_yaml::to_string(&self).map_err(|e| zferror!(ErrorKind::SerializationError, e).into())
83    }
84}