From 2d5164ac778cbd85cf48ce9d08f138f2d2f0494d Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 19 Dec 2024 16:30:29 +0100 Subject: [PATCH] reuse zenoh-examples common arguments parsing in zenoh-ext examples --- Cargo.lock | 1 + Cargo.toml | 1 + zenoh-ext/examples/Cargo.toml | 1 + zenoh-ext/examples/src/lib.rs | 65 +---------------------------------- 4 files changed, 4 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c2fa5212e..69148b230b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5247,6 +5247,7 @@ dependencies = [ "tokio", "zenoh", "zenoh-config", + "zenoh-examples", "zenoh-ext", ] diff --git a/Cargo.toml b/Cargo.toml index 8ad505ec67..f2b96a40be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/zenoh-ext/examples/Cargo.toml b/zenoh-ext/examples/Cargo.toml index fc4cb22dc7..ec70c6f503 100644 --- a/zenoh-ext/examples/Cargo.toml +++ b/zenoh-ext/examples/Cargo.toml @@ -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 } diff --git a/zenoh-ext/examples/src/lib.rs b/zenoh-ext/examples/src/lib.rs index 04d1223022..0c176a2181 100644 --- a/zenoh-ext/examples/src/lib.rs +++ b/zenoh-ext/examples/src/lib.rs @@ -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, - #[arg(short, long)] - /// The Zenoh session mode [default: peer]. - mode: Option, - #[arg(short = 'e', long)] - /// Endpoints to connect to. - connect: Vec, - #[arg(short, long)] - /// Endpoints to listen on. - listen: Vec, -} - -impl From 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;