Skip to content

Commit

Permalink
[eclipse-iceoryx#98] Move common filters to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Sep 21, 2024
1 parent 34cfea6 commit 0579cf6
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 128 deletions.
43 changes: 2 additions & 41 deletions iceoryx2-cli/iox2-node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<Self, Self::Err> {
if let Ok(pid) = s.parse::<pid_t>() {
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)]
Expand Down
4 changes: 2 additions & 2 deletions iceoryx2-cli/iox2-node/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down
51 changes: 1 addition & 50 deletions iceoryx2-cli/iox2-node/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeState<Service>> for NodeIdentifier {
fn matches(&self, node: &NodeState<Service>) -> 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<NodeState<Service>> for StateFilter {
fn matches(&self, node: &NodeState<Service>) -> 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<NodeState<Service>> for OutputFilter {
fn matches(&self, node: &NodeState<Service>) -> bool {
Expand Down
12 changes: 1 addition & 11 deletions iceoryx2-cli/iox2-service/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)]
Expand Down
5 changes: 2 additions & 3 deletions iceoryx2-cli/iox2-service/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ServiceDescriptor>::new();

ipc::Service::list(Config::global_config(), |service| {
if filter.matches(&service) {
Expand Down
17 changes: 1 addition & 16 deletions iceoryx2-cli/iox2-service/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServiceDetails<Service>> for MessagingPatternFilter {
fn matches(&self, service: &ServiceDetails<Service>) -> 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<ServiceDetails<Service>> for OutputFilter {
fn matches(&self, service: &ServiceDetails<Service>) -> bool {
Expand Down
116 changes: 116 additions & 0 deletions iceoryx2-cli/lib/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>: 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<Self, Self::Err> {
if let Ok(pid) = s.parse::<pid_t>() {
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<NodeState<Service>> for NodeIdentifier {
fn matches(&self, node: &NodeState<Service>) -> 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<NodeState<Service>> for StateFilter {
fn matches(&self, node: &NodeState<Service>) -> 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<ServiceDetails<Service>> for MessagingPatternFilter {
fn matches(&self, service: &ServiceDetails<Service>) -> bool {
matches!(
(self, &service.static_details.messaging_pattern()),
(
MessagingPatternFilter::PublishSubscribe,
MessagingPattern::PublishSubscribe(_)
) | (MessagingPatternFilter::Event, MessagingPattern::Event(_))
| (MessagingPatternFilter::All, _)
)
}
}
3 changes: 1 addition & 2 deletions iceoryx2-cli/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 0 additions & 3 deletions iceoryx2-cli/lib/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ where
}
}

pub type ServiceList = Vec<ServiceDescriptor>;

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
pub struct NodeIdString(String);

Expand Down Expand Up @@ -226,7 +224,6 @@ pub struct NodeList {
pub details: Vec<NodeDescriptor>,
}

// @todo Remove
impl<T> From<&IceoryxServiceDynamicDetails<T>> for NodeList
where
T: IceoryxService,
Expand Down

0 comments on commit 0579cf6

Please sign in to comment.