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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use {
    crate::RegistrationDecl,
    cm_rust::{
        CapabilityDecl, ExposeDecl, ExposeDeclCommon, OfferDecl, OfferDeclCommon, SourceName,
        UseDecl, UseDeclCommon,
    },
    cm_types::Name,
    moniker::Moniker,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Describes a single step taken by the capability routing algorithm.
#[cfg_attr(
    feature = "serde",
    derive(Deserialize, Serialize),
    serde(rename_all = "snake_case", tag = "action")
)]
#[derive(Clone, Debug, PartialEq)]
pub enum RouteSegment {
    /// The capability was used by a component instance in its manifest.
    UseBy { moniker: Moniker, capability: UseDecl },

    /// The capability was offered by a component instance in its manifest.
    OfferBy { moniker: Moniker, capability: OfferDecl },

    /// The capability was exposed by a component instance in its manifest.
    ExposeBy { moniker: Moniker, capability: ExposeDecl },

    /// The capability was declared by a component instance in its manifest.
    DeclareBy { moniker: Moniker, capability: CapabilityDecl },

    /// The capability was registered in a component instance's environment in its manifest.
    RegisterBy { moniker: Moniker, capability: RegistrationDecl },

    /// This is a framework capability served by component manager.
    ProvideFromFramework { capability: Name },

    /// This is a builtin capability served by component manager.
    ProvideAsBuiltin { capability: CapabilityDecl },

    /// This is a capability available in component manager's namespace.
    ProvideFromNamespace { capability: CapabilityDecl },
}

impl std::fmt::Display for RouteSegment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UseBy { moniker, capability } => {
                write!(
                    f,
                    "`{}` used `{}` from {}",
                    moniker,
                    capability.source_name(),
                    capability.source()
                )
            }
            Self::OfferBy { moniker, capability } => {
                write!(
                    f,
                    "`{}` offered `{}` from {} to {}",
                    moniker,
                    capability.source_name(),
                    capability.source(),
                    capability.target()
                )
            }
            Self::ExposeBy { moniker, capability } => {
                write!(
                    f,
                    "`{}` exposed `{}` from {} to {}",
                    moniker,
                    capability.source_name(),
                    capability.source(),
                    capability.target()
                )
            }
            Self::DeclareBy { moniker, capability } => {
                write!(f, "`{}` declared capability `{}`", moniker, capability.name())
            }
            Self::RegisterBy { moniker, capability } => {
                write!(f, "`{}` registered capability {:?}", moniker, capability)
            }
            Self::ProvideAsBuiltin { capability } => {
                write!(f, "`{}` is a built-in capability", capability.name())
            }
            Self::ProvideFromFramework { capability } => {
                write!(f, "`{}` is a framework capability", capability)
            }
            Self::ProvideFromNamespace { capability } => {
                write!(f, "`{}` exists in component manager's namespace", capability.name())
            }
        }
    }
}

impl RouteSegment {
    /// Get the moniker of the component instance where this segment occurred, if any.
    pub fn moniker(&self) -> Option<Moniker> {
        match self {
            Self::UseBy { moniker, .. }
            | Self::DeclareBy { moniker, .. }
            | Self::ExposeBy { moniker, .. }
            | Self::OfferBy { moniker, .. }
            | Self::RegisterBy { moniker, .. } => Some(moniker.clone()),
            _ => None,
        }
    }
}

#[derive(Clone, Debug)]
pub struct RouteMapper {
    route: Vec<RouteSegment>,
}

impl RouteMapper {
    pub fn new() -> Self {
        Self { route: vec![] }
    }

    pub fn get_route(self) -> Vec<RouteSegment> {
        self.route
    }
}

impl DebugRouteMapper for RouteMapper {
    fn add_use(&mut self, moniker: Moniker, use_decl: &UseDecl) {
        self.route.push(RouteSegment::UseBy { moniker: moniker, capability: use_decl.clone() })
    }

    fn add_offer(&mut self, moniker: Moniker, offer_decl: &OfferDecl) {
        self.route.push(RouteSegment::OfferBy { moniker: moniker, capability: offer_decl.clone() })
    }

    fn add_expose(&mut self, moniker: Moniker, expose_decl: ExposeDecl) {
        self.route.push(RouteSegment::ExposeBy { moniker: moniker, capability: expose_decl })
    }

    fn add_registration(&mut self, moniker: Moniker, registration_decl: &RegistrationDecl) {
        self.route.push(RouteSegment::RegisterBy {
            moniker: moniker,
            capability: registration_decl.clone(),
        })
    }

    fn add_component_capability(&mut self, moniker: Moniker, capability_decl: &CapabilityDecl) {
        self.route
            .push(RouteSegment::DeclareBy { moniker: moniker, capability: capability_decl.clone() })
    }

    fn add_framework_capability(&mut self, capability_name: Name) {
        self.route.push(RouteSegment::ProvideFromFramework { capability: capability_name })
    }

    fn add_builtin_capability(&mut self, capability_decl: &CapabilityDecl) {
        self.route.push(RouteSegment::ProvideAsBuiltin { capability: capability_decl.clone() })
    }

    fn add_namespace_capability(&mut self, capability_decl: &CapabilityDecl) {
        self.route.push(RouteSegment::ProvideFromNamespace { capability: capability_decl.clone() })
    }
}

#[derive(Clone, Debug)]
pub struct NoopRouteMapper;

impl DebugRouteMapper for NoopRouteMapper {}

/// Provides methods to record and retrieve a summary of a capability route.
pub trait DebugRouteMapper: Send + Sync {
    #[allow(unused_variables)]
    fn add_use(&mut self, moniker: Moniker, use_decl: &UseDecl) {}

    #[allow(unused_variables)]
    fn add_offer(&mut self, moniker: Moniker, offer_decl: &OfferDecl) {}

    #[allow(unused_variables)]
    fn add_expose(&mut self, moniker: Moniker, expose_decl: ExposeDecl) {}

    #[allow(unused_variables)]
    fn add_registration(&mut self, moniker: Moniker, registration_decl: &RegistrationDecl) {}

    #[allow(unused_variables)]
    fn add_component_capability(&mut self, moniker: Moniker, capability_decl: &CapabilityDecl) {}

    #[allow(unused_variables)]
    fn add_framework_capability(&mut self, capability_name: Name) {}

    #[allow(unused_variables)]
    fn add_builtin_capability(&mut self, capability_decl: &CapabilityDecl) {}

    #[allow(unused_variables)]
    fn add_namespace_capability(&mut self, capability_decl: &CapabilityDecl) {}
}