iceoryx2_cli/
filter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// 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, _)
        )
    }
}