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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2022 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::instanced_child_name::InstancedChildName,
    core::cmp::{self, Ordering},
    moniker::{ChildName, ChildNameBase, Moniker, MonikerBase, MonikerError},
    std::{fmt, hash::Hash},
};

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

/// An instanced moniker describes the identity of a component instance in terms of its path
/// relative to the root of the component instance tree.
///
/// A root moniker is a moniker with an empty path.
///
/// Instanced monikers are only used internally within the component manager.  Externally,
/// components are referenced by encoded moniker so as to minimize the amount of
/// information which is disclosed about the overall structure of the component instance tree.
///
/// Display notation: ".", "name1:1", "name1:1/name2:2", ...
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Eq, PartialEq, Clone, Hash, Default)]
pub struct InstancedMoniker {
    path: Vec<InstancedChildName>,
}

impl InstancedMoniker {
    /// Create a new InstancedMoniker with zero-value InstanceIds for all path parts
    /// in `moniker`.
    pub fn from_moniker_with_zero_value_instance_ids(moniker: &Moniker) -> InstancedMoniker {
        let path: Vec<InstancedChildName> =
            moniker.path().iter().map(|p| InstancedChildName::from_child_moniker(p, 0)).collect();
        InstancedMoniker::new(path)
    }

    /// Convert an InstancedMoniker into an allocated Moniker without InstanceIds
    pub fn without_instance_ids(&self) -> Moniker {
        let path: Vec<ChildName> = self.path().iter().map(|p| p.without_instance_id()).collect();
        Moniker::new(path)
    }

    /// Transforms an `InstancedMoniker` into a representation where all dynamic children
    /// have `0` value instance ids.
    pub fn with_zero_value_instance_ids(&self) -> InstancedMoniker {
        let path = self
            .path()
            .iter()
            .map(|c| {
                InstancedChildName::try_new(
                    c.name().as_str(),
                    c.collection().map(|c| c.as_str()),
                    0,
                )
                .expect("down path moniker is guaranteed to be valid")
            })
            .collect();
        InstancedMoniker::new(path)
    }
}

impl MonikerBase for InstancedMoniker {
    type Part = InstancedChildName;

    fn new(path: Vec<Self::Part>) -> Self {
        Self { path }
    }

    fn path(&self) -> &Vec<Self::Part> {
        &self.path
    }

    fn path_mut(&mut self) -> &mut Vec<Self::Part> {
        &mut self.path
    }
}

impl TryFrom<&str> for InstancedMoniker {
    type Error = MonikerError;

    fn try_from(input: &str) -> Result<Self, MonikerError> {
        Self::parse_str(input)
    }
}

impl TryFrom<Vec<&str>> for InstancedMoniker {
    type Error = MonikerError;

    fn try_from(rep: Vec<&str>) -> Result<Self, MonikerError> {
        Self::parse(&rep)
    }
}

impl cmp::Ord for InstancedMoniker {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.compare(other)
    }
}

impl PartialOrd for InstancedMoniker {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl fmt::Display for InstancedMoniker {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.format(f)
    }
}

impl fmt::Debug for InstancedMoniker {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.format(f)
    }
}
#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use {super::*, cm_types::Name};

    #[test]
    fn from_moniker() {
        let m = Moniker::from_str("foo/bar").unwrap();
        let instanced = InstancedMoniker::from_moniker_with_zero_value_instance_ids(&m);
        assert_eq!(instanced.to_string(), "foo:0/bar:0");
    }

    #[test]
    fn instanced_monikers() {
        let root = InstancedMoniker::root();
        assert_eq!(true, root.is_root());
        assert_eq!(".", format!("{}", root));
        assert_eq!(root, InstancedMoniker::try_from(vec![]).unwrap());

        let m = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", Some("coll"), 2).unwrap(),
        ]);
        assert_eq!(false, m.is_root());
        assert_eq!("a:1/coll:b:2", format!("{}", m));
        assert_eq!(m, InstancedMoniker::try_from(vec!["a:1", "coll:b:2"]).unwrap());
        assert_eq!(m.leaf().map(|m| m.collection()).flatten(), Some(&Name::new("coll").unwrap()));
        assert_eq!(m.leaf().map(|m| m.name().as_str()), Some("b"));
        assert_eq!(m.leaf().map(|m| m.instance()), Some(2));
        assert_eq!(m.leaf(), Some(&InstancedChildName::try_from("coll:b:2").unwrap()));
    }

    #[test]
    fn instanced_moniker_parent() {
        let root = InstancedMoniker::root();
        assert_eq!(true, root.is_root());
        assert_eq!(None, root.parent());

        let m = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", None, 2).unwrap(),
        ]);
        assert_eq!("a:1/b:2", format!("{}", m));
        assert_eq!("a:1", format!("{}", m.parent().unwrap()));
        assert_eq!(".", format!("{}", m.parent().unwrap().parent().unwrap()));
        assert_eq!(None, m.parent().unwrap().parent().unwrap().parent());
        assert_eq!(m.leaf(), Some(&InstancedChildName::try_from("b:2").unwrap()));
    }

    #[test]
    fn instanced_moniker_compare() {
        let a = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", None, 2).unwrap(),
            InstancedChildName::try_new("c", None, 3).unwrap(),
        ]);
        let a2 = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", None, 3).unwrap(),
            InstancedChildName::try_new("c", None, 3).unwrap(),
        ]);
        let b = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", None, 2).unwrap(),
            InstancedChildName::try_new("b", None, 3).unwrap(),
        ]);
        let c = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", None, 2).unwrap(),
            InstancedChildName::try_new("c", None, 3).unwrap(),
            InstancedChildName::try_new("d", None, 4).unwrap(),
        ]);
        let d = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", None, 2).unwrap(),
            InstancedChildName::try_new("c", None, 3).unwrap(),
        ]);

        assert_eq!(Ordering::Less, a.cmp(&a2));
        assert_eq!(Ordering::Greater, a2.cmp(&a));
        assert_eq!(Ordering::Greater, a.cmp(&b));
        assert_eq!(Ordering::Less, b.cmp(&a));
        assert_eq!(Ordering::Less, a.cmp(&c));
        assert_eq!(Ordering::Greater, c.cmp(&a));
        assert_eq!(Ordering::Equal, a.cmp(&d));
        assert_eq!(Ordering::Equal, d.cmp(&a));
        assert_eq!(Ordering::Less, b.cmp(&c));
        assert_eq!(Ordering::Greater, c.cmp(&b));
        assert_eq!(Ordering::Less, b.cmp(&d));
        assert_eq!(Ordering::Greater, d.cmp(&b));
        assert_eq!(Ordering::Greater, c.cmp(&d));
        assert_eq!(Ordering::Less, d.cmp(&c));
    }

    #[test]
    fn instanced_monikers_has_prefix() {
        let root = InstancedMoniker::root();
        let a = InstancedMoniker::new(vec![InstancedChildName::try_new("a", None, 1).unwrap()]);
        let ab = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", None, 2).unwrap(),
        ]);
        let abc = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", None, 2).unwrap(),
            InstancedChildName::try_new("c", None, 3).unwrap(),
        ]);
        let abd = InstancedMoniker::new(vec![
            InstancedChildName::try_new("a", None, 1).unwrap(),
            InstancedChildName::try_new("b", None, 2).unwrap(),
            InstancedChildName::try_new("d", None, 3).unwrap(),
        ]);

        assert!(root.has_prefix(&root));
        assert!(a.has_prefix(&root));
        assert!(ab.has_prefix(&root));
        assert!(abc.has_prefix(&root));
        assert!(abd.has_prefix(&root));

        assert!(!root.has_prefix(&a));
        assert!(a.has_prefix(&a));
        assert!(ab.has_prefix(&a));
        assert!(abc.has_prefix(&a));
        assert!(abd.has_prefix(&a));

        assert!(!root.has_prefix(&ab));
        assert!(!a.has_prefix(&ab));
        assert!(ab.has_prefix(&ab));
        assert!(abc.has_prefix(&ab));
        assert!(abd.has_prefix(&ab));

        assert!(!root.has_prefix(&abc));
        assert!(abc.has_prefix(&abc));
        assert!(!a.has_prefix(&abc));
        assert!(!ab.has_prefix(&abc));
        assert!(!abd.has_prefix(&abc));

        assert!(!abd.has_prefix(&abc));
        assert!(abd.has_prefix(&abd));
        assert!(!a.has_prefix(&abd));
        assert!(!ab.has_prefix(&abd));
        assert!(!abc.has_prefix(&abd));
    }

    #[test]
    fn instanced_moniker_parse_str() -> Result<(), MonikerError> {
        let under_test = |s| InstancedMoniker::parse_str(s);

        assert_eq!(under_test(".")?, InstancedMoniker::root());

        let a = InstancedChildName::try_new("a", None, 0).unwrap();
        let bb = InstancedChildName::try_new("b", Some("b"), 0).unwrap();

        assert_eq!(under_test("a:0")?, InstancedMoniker::new(vec![a.clone()]));
        assert_eq!(under_test("a:0/b:b:0")?, InstancedMoniker::new(vec![a.clone(), bb.clone()]));
        assert_eq!(
            under_test("a:0/b:b:0/a:0/b:b:0")?,
            InstancedMoniker::new(vec![a.clone(), bb.clone(), a.clone(), bb.clone()])
        );

        assert!(under_test("").is_err(), "cannot be empty");
        assert!(under_test("a:0/").is_err(), "path segments cannot be empty");
        assert!(under_test("a:0//b:0").is_err(), "path segments cannot be empty");
        assert!(under_test("a:a").is_err(), "must contain instance id");

        Ok(())
    }
}