component_id_index/
fidl_convert.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
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
// Copyright 2020 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::{
    Index, InstanceId, InstanceIdError, PersistedIndex, PersistedIndexEntry, ValidationError,
};
use fidl_fuchsia_component_internal as fcomponent_internal;
use moniker::{Moniker, MonikerError};
use thiserror::Error;

#[derive(Error, Clone, Debug, PartialEq)]
pub enum FidlConversionError {
    #[error("Missing instances")]
    MissingInstances,
    #[error("Missing instance_id")]
    MissingInstanceId,
    #[error("Missing moniker")]
    MissingMoniker,
    #[error("Invalid instance ID")]
    InstanceIdError(#[from] InstanceIdError),
    #[error("Invalid moniker")]
    MonikerError(#[from] MonikerError),
    #[error("invalid index")]
    ValidationError(#[from] ValidationError),
}

impl TryFrom<fcomponent_internal::ComponentIdIndex> for PersistedIndex {
    type Error = FidlConversionError;

    fn try_from(index: fcomponent_internal::ComponentIdIndex) -> Result<Self, Self::Error> {
        let mut instances: Vec<PersistedIndexEntry> = vec![];
        for entry in index.instances.ok_or_else(|| FidlConversionError::MissingInstances)? {
            instances.push(PersistedIndexEntry {
                instance_id: entry
                    .instance_id
                    .ok_or_else(|| FidlConversionError::MissingInstanceId)?
                    .parse::<InstanceId>()?,
                moniker: Moniker::parse_str(
                    &entry.moniker.ok_or_else(|| FidlConversionError::MissingMoniker)?,
                )?,
            });
        }
        Ok(PersistedIndex { instances })
    }
}

impl TryFrom<fcomponent_internal::ComponentIdIndex> for Index {
    type Error = FidlConversionError;

    fn try_from(index: fcomponent_internal::ComponentIdIndex) -> Result<Self, Self::Error> {
        let persisted_index: PersistedIndex = index.try_into()?;
        persisted_index.try_into().map_err(FidlConversionError::ValidationError)
    }
}

impl From<PersistedIndex> for fcomponent_internal::ComponentIdIndex {
    fn from(index: PersistedIndex) -> Self {
        fcomponent_internal::ComponentIdIndex {
            instances: Some(
                index
                    .instances
                    .into_iter()
                    .map(|entry| fcomponent_internal::InstanceIdEntry {
                        instance_id: Some(entry.instance_id.to_string()),
                        moniker: Some(entry.moniker.to_string()),
                        ..Default::default()
                    })
                    .collect(),
            ),
            ..Default::default()
        }
    }
}

impl From<Index> for fcomponent_internal::ComponentIdIndex {
    fn from(index: Index) -> Self {
        let persisted_index: PersistedIndex = index.into();
        persisted_index.into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn fidl_native_translation() {
        let fidl_index = fcomponent_internal::ComponentIdIndex {
            instances: Some(vec![fcomponent_internal::InstanceIdEntry {
                instance_id: Some(
                    "8c90d44863ff67586cf6961081feba4f760decab8bbbee376a3bfbc77b351280".to_string(),
                ),
                moniker: Some("a/b/c".to_string()),
                ..Default::default()
            }]),
            ..Default::default()
        };

        let native_index = PersistedIndex {
            instances: vec![PersistedIndexEntry {
                instance_id: "8c90d44863ff67586cf6961081feba4f760decab8bbbee376a3bfbc77b351280"
                    .parse::<InstanceId>()
                    .unwrap(),
                moniker: Moniker::parse_str("a/b/c").unwrap(),
            }],
        };

        assert_eq!(native_index, PersistedIndex::try_from(fidl_index.clone()).unwrap());
        assert_eq!(fidl_index, fcomponent_internal::ComponentIdIndex::from(native_index));
    }

    #[test]
    fn missing_instances() {
        assert_eq!(
            Some(FidlConversionError::MissingInstances),
            PersistedIndex::try_from(fcomponent_internal::ComponentIdIndex {
                ..Default::default()
            })
            .err()
        );
    }

    #[test]
    fn missing_instance_id() {
        assert_eq!(
            Some(FidlConversionError::MissingInstanceId),
            PersistedIndex::try_from(fcomponent_internal::ComponentIdIndex {
                instances: Some(vec![fcomponent_internal::InstanceIdEntry {
                    instance_id: None,
                    moniker: Some("a/b/c".to_string()),
                    ..Default::default()
                }]),
                ..Default::default()
            })
            .err()
        );
    }

    #[test]
    fn missing_moniker() {
        assert_eq!(
            Some(FidlConversionError::MissingMoniker),
            PersistedIndex::try_from(fcomponent_internal::ComponentIdIndex {
                instances: Some(vec![fcomponent_internal::InstanceIdEntry {
                    instance_id: Some(
                        "8c90d44863ff67586cf6961081feba4f760decab8bbbee376a3bfbc77b351280"
                            .to_string()
                    ),
                    moniker: None,
                    ..Default::default()
                }]),
                ..Default::default()
            })
            .err()
        );
    }
}