zenoh_ext_examples/
lib.rs

1//! Examples on using Zenoh.
2//! See the code in ../examples/
3//! Check ../README.md for usage.
4//!
5use zenoh::config::Config;
6
7#[derive(clap::ValueEnum, Clone, Copy, PartialEq, Eq, Hash, Debug)]
8pub enum Wai {
9    Peer,
10    Client,
11    Router,
12}
13impl core::fmt::Display for Wai {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        core::fmt::Debug::fmt(&self, f)
16    }
17}
18#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
19pub struct CommonArgs {
20    #[arg(short, long)]
21    /// A configuration file.
22    config: Option<String>,
23    #[arg(short, long)]
24    /// The Zenoh session mode [default: peer].
25    mode: Option<Wai>,
26    #[arg(short = 'e', long)]
27    /// Endpoints to connect to.
28    connect: Vec<String>,
29    #[arg(short, long)]
30    /// Endpoints to listen on.
31    listen: Vec<String>,
32}
33
34impl From<CommonArgs> for Config {
35    fn from(value: CommonArgs) -> Self {
36        (&value).into()
37    }
38}
39impl From<&CommonArgs> for Config {
40    fn from(value: &CommonArgs) -> Self {
41        let mut config = match &value.config {
42            Some(path) => Config::from_file(path).unwrap(),
43            None => Config::default(),
44        };
45        match value.mode {
46            Some(Wai::Peer) => config.set_mode(Some(zenoh::scouting::WhatAmI::Peer)),
47            Some(Wai::Client) => config.set_mode(Some(zenoh::scouting::WhatAmI::Client)),
48            Some(Wai::Router) => config.set_mode(Some(zenoh::scouting::WhatAmI::Router)),
49            None => Ok(None),
50        }
51        .unwrap();
52        if !value.connect.is_empty() {
53            config.connect.endpoints = value.connect.iter().map(|v| v.parse().unwrap()).collect();
54        }
55        if !value.listen.is_empty() {
56            config.listen.endpoints = value.listen.iter().map(|v| v.parse().unwrap()).collect();
57        }
58        config
59    }
60}