Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reuse zenoh-examples common arguments parsing in zenoh-ext examples #1679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading