diff --git a/iceoryx2-cli/iox2-node/src/cli.rs b/iceoryx2-cli/iox2-node/src/cli.rs index 34d090b37..f045941c3 100644 --- a/iceoryx2-cli/iox2-node/src/cli.rs +++ b/iceoryx2-cli/iox2-node/src/cli.rs @@ -10,16 +10,14 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT -use std::str::FromStr; - use clap::Args; use clap::Parser; use clap::Subcommand; -use clap::ValueEnum; +use iceoryx2_cli::filter::NodeIdentifier; +use iceoryx2_cli::filter::StateFilter; use iceoryx2_cli::help_template; use iceoryx2_cli::Format; -use iceoryx2_pal_posix::posix::pid_t; #[derive(Parser)] #[command( @@ -39,43 +37,6 @@ pub struct Cli { pub format: Format, } -#[derive(Clone, Debug)] -pub enum NodeIdentifier { - Name(String), - Id(String), - Pid(pid_t), -} - -fn is_valid_hex(s: &str) -> bool { - s.len() == 32 && s.chars().all(|c| c.is_ascii_hexdigit()) -} - -impl FromStr for NodeIdentifier { - type Err = String; - - fn from_str(s: &str) -> Result { - if let Ok(pid) = s.parse::() { - Ok(NodeIdentifier::Pid(pid)) - } else if is_valid_hex(s) { - Ok(NodeIdentifier::Id(s.to_string())) - } else { - Ok(NodeIdentifier::Name(s.to_string())) - } - } -} - -#[derive(Debug, Clone, ValueEnum)] -#[clap(rename_all = "PascalCase")] -#[derive(Default)] -pub enum StateFilter { - Alive, - Dead, - Inaccessible, - Undefined, - #[default] - All, -} - #[derive(Debug, Clone, Args)] pub struct OutputFilter { #[clap(short, long, value_enum, default_value_t = StateFilter::All)] diff --git a/iceoryx2-cli/iox2-node/src/commands.rs b/iceoryx2-cli/iox2-node/src/commands.rs index f3611ee06..19e17c575 100644 --- a/iceoryx2-cli/iox2-node/src/commands.rs +++ b/iceoryx2-cli/iox2-node/src/commands.rs @@ -12,13 +12,13 @@ use anyhow::{Context, Error, Result}; use iceoryx2::prelude::*; +use iceoryx2_cli::filter::Filter; +use iceoryx2_cli::filter::NodeIdentifier; use iceoryx2_cli::output::NodeDescription; use iceoryx2_cli::output::NodeDescriptor; use iceoryx2_cli::output::NodeList; -use iceoryx2_cli::Filter; use iceoryx2_cli::Format; -use crate::cli::NodeIdentifier; use crate::cli::OutputFilter; pub fn list(filter: OutputFilter, format: Format) -> Result<()> { diff --git a/iceoryx2-cli/iox2-node/src/filter.rs b/iceoryx2-cli/iox2-node/src/filter.rs index f76997e37..d69d68181 100644 --- a/iceoryx2-cli/iox2-node/src/filter.rs +++ b/iceoryx2-cli/iox2-node/src/filter.rs @@ -10,59 +10,10 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT -use crate::cli::NodeIdentifier; use crate::cli::OutputFilter; -use crate::cli::StateFilter; use iceoryx2::node::NodeState; -use iceoryx2::node::NodeView; use iceoryx2::service::ipc::Service; -use iceoryx2_cli::output::NodeIdString; -use iceoryx2_cli::Filter; - -impl Filter> for NodeIdentifier { - fn matches(&self, node: &NodeState) -> bool { - match self { - NodeIdentifier::Name(ref name) => match node { - NodeState::Alive(view) => view - .details() - .as_ref() - .map(|details| details.name().as_str() == name) - .unwrap_or(false), - NodeState::Dead(view) => view - .details() - .as_ref() - .map(|details| details.name().as_str() == name) - .unwrap_or(false), - NodeState::Inaccessible(_) | NodeState::Undefined(_) => false, - }, - NodeIdentifier::Id(ref id) => match node { - NodeState::Alive(view) => NodeIdString::from(view.id()) == **id, - NodeState::Dead(view) => NodeIdString::from(view.id()) == **id, - NodeState::Inaccessible(node_id) => NodeIdString::from(node_id) == **id, - NodeState::Undefined(node_id) => NodeIdString::from(node_id) == **id, - }, - NodeIdentifier::Pid(pid) => match node { - NodeState::Alive(view) => view.id().pid().value() == *pid, - NodeState::Dead(view) => view.id().pid().value() == *pid, - NodeState::Inaccessible(node_id) => node_id.pid().value() == *pid, - NodeState::Undefined(node_id) => node_id.pid().value() == *pid, - }, - } - } -} - -impl Filter> for StateFilter { - fn matches(&self, node: &NodeState) -> bool { - matches!( - (self, node), - (StateFilter::Alive, NodeState::Alive(_)) - | (StateFilter::Dead, NodeState::Dead(_)) - | (StateFilter::Inaccessible, NodeState::Inaccessible(_)) - | (StateFilter::Undefined, NodeState::Undefined(_)) - | (StateFilter::All, _) - ) - } -} +use iceoryx2_cli::filter::Filter; impl Filter> for OutputFilter { fn matches(&self, node: &NodeState) -> bool { diff --git a/iceoryx2-cli/iox2-service/src/cli.rs b/iceoryx2-cli/iox2-service/src/cli.rs index e812a73da..b1926906c 100644 --- a/iceoryx2-cli/iox2-service/src/cli.rs +++ b/iceoryx2-cli/iox2-service/src/cli.rs @@ -13,8 +13,8 @@ use clap::Args; use clap::Parser; use clap::Subcommand; -use clap::ValueEnum; +use iceoryx2_cli::filter::MessagingPatternFilter; use iceoryx2_cli::help_template; use iceoryx2_cli::Format; @@ -36,16 +36,6 @@ pub struct Cli { pub format: Format, } -#[derive(Debug, Clone, ValueEnum)] -#[clap(rename_all = "PascalCase")] -#[derive(Default)] -pub enum MessagingPatternFilter { - PublishSubscribe, - Event, - #[default] - All, -} - #[derive(Debug, Clone, Args)] pub struct OutputFilter { #[clap(short, long, value_enum, default_value_t = MessagingPatternFilter::All)] diff --git a/iceoryx2-cli/iox2-service/src/commands.rs b/iceoryx2-cli/iox2-service/src/commands.rs index 5c7be8eb9..90f9fae82 100644 --- a/iceoryx2-cli/iox2-service/src/commands.rs +++ b/iceoryx2-cli/iox2-service/src/commands.rs @@ -12,16 +12,15 @@ use anyhow::{Context, Error, Result}; use iceoryx2::prelude::*; +use iceoryx2_cli::filter::Filter; use iceoryx2_cli::output::ServiceDescription; use iceoryx2_cli::output::ServiceDescriptor; -use iceoryx2_cli::output::ServiceList; -use iceoryx2_cli::Filter; use iceoryx2_cli::Format; use crate::cli::OutputFilter; pub fn list(filter: OutputFilter, format: Format) -> Result<()> { - let mut services = ServiceList::new(); + let mut services = Vec::::new(); ipc::Service::list(Config::global_config(), |service| { if filter.matches(&service) { diff --git a/iceoryx2-cli/iox2-service/src/filter.rs b/iceoryx2-cli/iox2-service/src/filter.rs index e3550cdc7..935c74b4c 100644 --- a/iceoryx2-cli/iox2-service/src/filter.rs +++ b/iceoryx2-cli/iox2-service/src/filter.rs @@ -10,25 +10,10 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT -use crate::cli::MessagingPatternFilter; use crate::cli::OutputFilter; use iceoryx2::service::ipc::Service; -use iceoryx2::service::static_config::messaging_pattern::MessagingPattern; use iceoryx2::service::ServiceDetails; -use iceoryx2_cli::Filter; - -impl Filter> for MessagingPatternFilter { - fn matches(&self, service: &ServiceDetails) -> bool { - matches!( - (self, &service.static_details.messaging_pattern()), - ( - MessagingPatternFilter::PublishSubscribe, - MessagingPattern::PublishSubscribe(_) - ) | (MessagingPatternFilter::Event, MessagingPattern::Event(_)) - | (MessagingPatternFilter::All, _) - ) - } -} +use iceoryx2_cli::filter::Filter; impl Filter> for OutputFilter { fn matches(&self, service: &ServiceDetails) -> bool { diff --git a/iceoryx2-cli/lib/src/filter.rs b/iceoryx2-cli/lib/src/filter.rs index e5bab4b91..935bd02ca 100644 --- a/iceoryx2-cli/lib/src/filter.rs +++ b/iceoryx2-cli/lib/src/filter.rs @@ -10,8 +10,124 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT +use crate::output::NodeIdString; +use clap::ValueEnum; +use iceoryx2::node::NodeState; +use iceoryx2::node::NodeView; +use iceoryx2::service::ipc::Service; +use iceoryx2::service::static_config::messaging_pattern::MessagingPattern; +use iceoryx2::service::ServiceDetails; +use iceoryx2_pal_posix::posix::pid_t; use std::fmt::Debug; +use std::str::FromStr; pub trait Filter: Debug { fn matches(&self, item: &T) -> bool; } + +#[derive(Clone, Debug)] +pub enum NodeIdentifier { + Name(String), + Id(String), + Pid(pid_t), +} + +impl NodeIdentifier { + fn is_valid_id(s: &str) -> bool { + s.len() == 32 && s.chars().all(|c| c.is_ascii_hexdigit()) + } +} + +impl FromStr for NodeIdentifier { + type Err = String; + + fn from_str(s: &str) -> Result { + if let Ok(pid) = s.parse::() { + Ok(NodeIdentifier::Pid(pid)) + } else if Self::is_valid_id(s) { + Ok(NodeIdentifier::Id(s.to_string())) + } else { + Ok(NodeIdentifier::Name(s.to_string())) + } + } +} + +#[derive(Debug, Clone, ValueEnum)] +#[clap(rename_all = "PascalCase")] +#[derive(Default)] +pub enum StateFilter { + Alive, + Dead, + Inaccessible, + Undefined, + #[default] + All, +} + +impl Filter> for NodeIdentifier { + fn matches(&self, node: &NodeState) -> bool { + match self { + NodeIdentifier::Name(ref name) => match node { + NodeState::Alive(view) => view + .details() + .as_ref() + .map(|details| details.name().as_str() == name) + .unwrap_or(false), + NodeState::Dead(view) => view + .details() + .as_ref() + .map(|details| details.name().as_str() == name) + .unwrap_or(false), + NodeState::Inaccessible(_) | NodeState::Undefined(_) => false, + }, + NodeIdentifier::Id(ref id) => match node { + NodeState::Alive(view) => NodeIdString::from(view.id()) == **id, + NodeState::Dead(view) => NodeIdString::from(view.id()) == **id, + NodeState::Inaccessible(node_id) => NodeIdString::from(node_id) == **id, + NodeState::Undefined(node_id) => NodeIdString::from(node_id) == **id, + }, + NodeIdentifier::Pid(pid) => match node { + NodeState::Alive(view) => view.id().pid().value() == *pid, + NodeState::Dead(view) => view.id().pid().value() == *pid, + NodeState::Inaccessible(node_id) => node_id.pid().value() == *pid, + NodeState::Undefined(node_id) => node_id.pid().value() == *pid, + }, + } + } +} + +impl Filter> for StateFilter { + fn matches(&self, node: &NodeState) -> bool { + matches!( + (self, node), + (StateFilter::Alive, NodeState::Alive(_)) + | (StateFilter::Dead, NodeState::Dead(_)) + | (StateFilter::Inaccessible, NodeState::Inaccessible(_)) + | (StateFilter::Undefined, NodeState::Undefined(_)) + | (StateFilter::All, _) + ) + } +} + +#[derive(Debug, Clone, ValueEnum)] +#[clap(rename_all = "PascalCase")] +#[derive(Default)] +pub enum MessagingPatternFilter { + PublishSubscribe, + Event, + #[default] + All, +} + +impl Filter> for MessagingPatternFilter { + fn matches(&self, service: &ServiceDetails) -> bool { + matches!( + (self, &service.static_details.messaging_pattern()), + ( + MessagingPatternFilter::PublishSubscribe, + MessagingPattern::PublishSubscribe(_) + ) | (MessagingPatternFilter::Event, MessagingPattern::Event(_)) + | (MessagingPatternFilter::All, _) + ) + } +} diff --git a/iceoryx2-cli/lib/src/lib.rs b/iceoryx2-cli/lib/src/lib.rs index 2051faef5..354292e8a 100644 --- a/iceoryx2-cli/lib/src/lib.rs +++ b/iceoryx2-cli/lib/src/lib.rs @@ -11,11 +11,10 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT mod cli; -mod filter; mod format; +pub mod filter; pub mod output; pub use cli::help_template; -pub use filter::Filter; pub use format::Format; diff --git a/iceoryx2-cli/lib/src/output.rs b/iceoryx2-cli/lib/src/output.rs index c84da2741..0b0932165 100644 --- a/iceoryx2-cli/lib/src/output.rs +++ b/iceoryx2-cli/lib/src/output.rs @@ -73,8 +73,6 @@ where } } -pub type ServiceList = Vec; - #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)] pub struct NodeIdString(String); @@ -226,7 +224,6 @@ pub struct NodeList { pub details: Vec, } -// @todo Remove impl From<&IceoryxServiceDynamicDetails> for NodeList where T: IceoryxService,