File size: 9,681 Bytes
b98ffbb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
use once_cell::sync::OnceCell;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{
borrow::Borrow,
collections::{BTreeMap, BTreeSet},
convert::Infallible,
fmt,
str::FromStr,
time::Duration,
};
#[derive(
Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
)]
pub struct NodeId(String);
impl FromStr for NodeId {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_owned()))
}
}
impl From<String> for NodeId {
fn from(id: String) -> Self {
Self(id)
}
}
impl std::fmt::Display for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl AsRef<str> for NodeId {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(
Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
)]
pub struct OperatorId(String);
impl FromStr for OperatorId {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_owned()))
}
}
impl From<String> for OperatorId {
fn from(id: String) -> Self {
Self(id)
}
}
impl std::fmt::Display for OperatorId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl AsRef<str> for OperatorId {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(
Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
)]
pub struct DataId(String);
impl From<DataId> for String {
fn from(id: DataId) -> Self {
id.0
}
}
impl From<String> for DataId {
fn from(id: String) -> Self {
Self(id)
}
}
impl std::fmt::Display for DataId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl std::ops::Deref for DataId {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<String> for DataId {
fn as_ref(&self) -> &String {
&self.0
}
}
impl AsRef<str> for DataId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<String> for DataId {
fn borrow(&self) -> &String {
&self.0
}
}
impl Borrow<str> for DataId {
fn borrow(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
pub enum InputMapping {
Timer { interval: Duration },
User(UserInputMapping),
}
impl InputMapping {
pub fn source(&self) -> &NodeId {
static DORA_NODE_ID: OnceCell<NodeId> = OnceCell::new();
match self {
InputMapping::User(mapping) => &mapping.source,
InputMapping::Timer { .. } => DORA_NODE_ID.get_or_init(|| NodeId("dora".to_string())),
}
}
}
impl fmt::Display for InputMapping {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InputMapping::Timer { interval } => {
let duration = format_duration(*interval);
write!(f, "dora/timer/{duration}")
}
InputMapping::User(mapping) => {
write!(f, "{}/{}", mapping.source, mapping.output)
}
}
}
}
impl Serialize for InputMapping {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(self)
}
}
impl<'de> Deserialize<'de> for InputMapping {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let string = String::deserialize(deserializer)?;
let (source, output) = string
.split_once('/')
.ok_or_else(|| serde::de::Error::custom("input must start with `<source>/`"))?;
let deserialized = match source {
"dora" => match output.split_once('/') {
Some(("timer", output)) => {
let (unit, value) = output.split_once('/').ok_or_else(|| {
serde::de::Error::custom(
"timer input must specify unit and value (e.g. `secs/5` or `millis/100`)",
)
})?;
let interval = match unit {
"secs" => {
let value = value.parse().map_err(|_| {
serde::de::Error::custom(format!(
"secs must be an integer (got `{value}`)"
))
})?;
Duration::from_secs(value)
}
"millis" => {
let value = value.parse().map_err(|_| {
serde::de::Error::custom(format!(
"millis must be an integer (got `{value}`)"
))
})?;
Duration::from_millis(value)
}
other => {
return Err(serde::de::Error::custom(format!(
"timer unit must be either secs or millis (got `{other}`"
)))
}
};
Self::Timer { interval }
}
Some((other, _)) => {
return Err(serde::de::Error::custom(format!(
"unknown dora input `{other}`"
)))
}
None => return Err(serde::de::Error::custom("dora input has invalid format")),
},
_ => Self::User(UserInputMapping {
source: source.to_owned().into(),
output: output.to_owned().into(),
}),
};
Ok(deserialized)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
pub struct UserInputMapping {
pub source: NodeId,
pub output: DataId,
}
pub struct FormattedDuration(pub Duration);
impl fmt::Display for FormattedDuration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.subsec_millis() == 0 {
write!(f, "secs/{}", self.0.as_secs())
} else {
write!(f, "millis/{}", self.0.as_millis())
}
}
}
pub fn format_duration(interval: Duration) -> FormattedDuration {
FormattedDuration(interval)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct NodeRunConfig {
/// Inputs for the nodes as a map from input ID to `node_id/output_id`.
///
/// e.g.
///
/// inputs:
///
/// example_input: example_node/example_output1
///
#[serde(default)]
pub inputs: BTreeMap<DataId, Input>,
/// List of output IDs.
///
/// e.g.
///
/// outputs:
///
/// - output_1
///
/// - output_2
#[serde(default)]
pub outputs: BTreeSet<DataId>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, from = "InputDef", into = "InputDef")]
pub struct Input {
pub mapping: InputMapping,
pub queue_size: Option<usize>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum InputDef {
MappingOnly(InputMapping),
WithOptions {
source: InputMapping,
queue_size: Option<usize>,
},
}
impl From<Input> for InputDef {
fn from(input: Input) -> Self {
match input {
Input {
mapping,
queue_size: None,
} => Self::MappingOnly(mapping),
Input {
mapping,
queue_size,
} => Self::WithOptions {
source: mapping,
queue_size,
},
}
}
}
impl From<InputDef> for Input {
fn from(value: InputDef) -> Self {
match value {
InputDef::MappingOnly(mapping) => Self {
mapping,
queue_size: None,
},
InputDef::WithOptions { source, queue_size } => Self {
mapping: source,
queue_size,
},
}
}
}
#[derive(Debug, Default, Serialize, Deserialize, JsonSchema, Clone)]
#[serde(deny_unknown_fields, rename_all = "lowercase")]
pub struct CommunicationConfig {
// see https://github.com/dtolnay/serde-yaml/issues/298
#[serde(
default,
with = "serde_yaml::with::singleton_map",
rename = "_unstable_local"
)]
#[schemars(with = "String")]
pub local: LocalCommunicationConfig,
#[serde(
default,
with = "serde_yaml::with::singleton_map",
rename = "_unstable_remote"
)]
#[schemars(with = "String")]
pub remote: RemoteCommunicationConfig,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum LocalCommunicationConfig {
Tcp,
Shmem,
}
impl Default for LocalCommunicationConfig {
fn default() -> Self {
Self::Tcp
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(deny_unknown_fields, rename_all = "lowercase")]
pub enum RemoteCommunicationConfig {
Tcp,
// TODO:a
// Zenoh {
// config: Option<serde_yaml::Value>,
// prefix: String,
// },
}
impl Default for RemoteCommunicationConfig {
fn default() -> Self {
Self::Tcp
}
}
|