Skip to content

Commit

Permalink
reuse zenoh-examples common arguments parsing in zenoh-ext examples (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 authored Dec 20, 2024
1 parent 9640d22 commit 0549678
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 64 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ zenoh-link-commons = { version = "1.0.0-dev", path = "io/zenoh-link-commons" }
zenoh = { version = "1.0.0-dev", path = "zenoh", default-features = false }
zenoh-runtime = { version = "1.0.0-dev", path = "commons/zenoh-runtime" }
zenoh-task = { version = "1.0.0-dev", path = "commons/zenoh-task" }
zenoh-examples = { version = "1.0.0-dev", path = "examples" }

[profile.dev]
debug = true
Expand Down
1 change: 1 addition & 0 deletions zenoh-ext/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ futures = { workspace = true }
zenoh = { workspace = true, features = ["unstable", "internal_config"], default-features = false }
clap = { workspace = true, features = ["derive"] }
zenoh-ext = { workspace = true, features = ["unstable"] }
zenoh-examples = { workspace = true }

[dev-dependencies]
zenoh-config = { workspace = true }
Expand Down
65 changes: 1 addition & 64 deletions zenoh-ext/examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,4 @@
//! See the code in ../examples/
//! Check ../README.md for usage.
//!
use zenoh::{config::WhatAmI, Config};

#[derive(clap::ValueEnum, Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Wai {
Peer,
Client,
Router,
}
impl core::fmt::Display for Wai {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
core::fmt::Debug::fmt(&self, f)
}
}
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
pub struct CommonArgs {
#[arg(short, long)]
/// A configuration file.
config: Option<String>,
#[arg(short, long)]
/// The Zenoh session mode [default: peer].
mode: Option<Wai>,
#[arg(short = 'e', long)]
/// Endpoints to connect to.
connect: Vec<String>,
#[arg(short, long)]
/// Endpoints to listen on.
listen: Vec<String>,
}

impl From<CommonArgs> for Config {
fn from(value: CommonArgs) -> Self {
(&value).into()
}
}
impl From<&CommonArgs> for Config {
fn from(value: &CommonArgs) -> Self {
let mut config = match &value.config {
Some(path) => Config::from_file(path).unwrap(),
None => Config::default(),
};
match value.mode {
Some(Wai::Peer) => config.set_mode(Some(WhatAmI::Peer)),
Some(Wai::Client) => config.set_mode(Some(WhatAmI::Client)),
Some(Wai::Router) => config.set_mode(Some(WhatAmI::Router)),
None => Ok(None),
}
.unwrap();
if !value.connect.is_empty() {
config
.connect
.endpoints
.set(value.connect.iter().map(|v| v.parse().unwrap()).collect())
.unwrap();
}
if !value.listen.is_empty() {
config
.listen
.endpoints
.set(value.listen.iter().map(|v| v.parse().unwrap()).collect())
.unwrap();
}
config
}
}
pub use zenoh_examples::CommonArgs;

0 comments on commit 0549678

Please sign in to comment.