Skip to main content

inspect_proto/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! A generic protobuf service for inspect.
5
6#![expect(missing_docs)]
7#![forbid(unsafe_code)]
8#![expect(clippy::allow_attributes)]
9
10// Crates used by generated code. Reference them explicitly to ensure that
11// automated tools do not remove them.
12
13use mesh_rpc as _;
14use prost as _;
15
16include!(concat!(env!("OUT_DIR"), "/inspect.rs"));
17
18/// Equivalent to [`InspectResponse`], but using [`inspect::Node`].
19/// These have equivalent encodings.
20#[derive(Debug, Clone, mesh::MeshPayload)]
21pub struct InspectResponse2 {
22    #[mesh(1)]
23    pub result: inspect::Node,
24}
25
26/// Equivalent to [`InspectResponse`], but using [`inspect::Value`].
27/// These have equivalent encodings.
28#[derive(Debug, Clone, mesh::MeshPayload)]
29pub struct UpdateResponse2 {
30    #[mesh(1)]
31    pub new_value: inspect::Value,
32}
33
34#[cfg(test)]
35mod tests {
36    use crate::InspectResponse;
37    use crate::InspectResponse2;
38    use inspect::Entry;
39    use inspect::Error;
40    use inspect::Node;
41    use inspect::SensitivityLevel;
42    use inspect::Value;
43    use inspect::ValueKind;
44    use mesh::Message;
45
46    #[test]
47    fn test() {
48        let response2 = InspectResponse2 {
49            result: Node::Dir(vec![
50                Entry {
51                    name: "a".to_string(),
52                    node: Node::Unevaluated,
53                    sensitivity: SensitivityLevel::Unspecified,
54                },
55                Entry {
56                    name: "b".to_string(),
57                    node: Node::Failed(Error::Update("foo".into())),
58                    sensitivity: SensitivityLevel::Safe,
59                },
60                Entry {
61                    name: "c".to_string(),
62                    node: Node::Value(Value::new(ValueKind::Signed(-1))),
63                    sensitivity: SensitivityLevel::Sensitive,
64                },
65                Entry {
66                    name: "d".to_string(),
67                    node: Node::Value(Value::new(ValueKind::Unsigned(2))),
68                    sensitivity: SensitivityLevel::Safe,
69                },
70                Entry {
71                    name: "e".to_string(),
72                    node: Node::Value(Value::new(ValueKind::Bool(true))),
73                    sensitivity: SensitivityLevel::Sensitive,
74                },
75                Entry {
76                    name: "f".to_string(),
77                    node: Node::Value(Value::new(ValueKind::String("foo".to_string()))),
78                    sensitivity: SensitivityLevel::Unspecified,
79                },
80            ]),
81        };
82
83        let response = Message::new(response2.clone())
84            .parse::<InspectResponse>()
85            .unwrap();
86
87        assert_eq!(
88            Message::new(response)
89                .parse::<InspectResponse2>()
90                .unwrap()
91                .result,
92            response2.result
93        );
94    }
95}