summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_api/src/address.rs
blob: 3e215211e364bd3bffa106d3150bfbee351c9c18 (plain)
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
/// An address which could be either a target or source of messages
///
/// Nesting addresses allows to disambiguated between different kind of
/// sources and the way they have arrived here.
#[derive(Debug, Clone)]
pub struct Address {
    endpoint: EndpointKind,
    source: Option<Box<Address>>,
}

impl Address {
    pub fn new(endpoint: EndpointKind) -> Address {
        Self {
            endpoint,
            source: None,
        }
    }

    /// Get the original source of an `Address`
    pub fn origin(&self) -> &Address {
        if let Some(source) = self.source.as_ref() {
            source.origin()
        } else {
            self
        }
    }

    pub fn add_new_step(&self, endpoint: EndpointKind) -> Self {
        Self {
            endpoint,
            source: Some(Box::new(self.clone())),
        }
    }
}

/// What kind of endpoint is it
#[derive(Debug, Clone)]
pub enum EndpointKind {
    /// The `tedge` core
    Core,
    /// A specific plugin
    Plugin { id: String },
}