Skip to main content

fdf_component/node/
properties.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fidl_fuchsia_driver_framework::NodePropertyValue;
6
7/// A newtype wrapper that can be used to construct [`NodePropertyValue`]-compatible values
8/// for [`crate::NodeBuilder::add_property`], and to read the values of the properties reported
9/// by [`crate::DriverContext::node_properties`].
10#[derive(Debug, Clone, PartialEq)]
11pub struct PropertyValue(pub(crate) NodePropertyValue);
12
13/// A property of one of the driver's parent nodes, as reported by
14/// [`crate::DriverContext::node_properties`].
15#[derive(Debug, Clone, PartialEq)]
16pub struct ParentProperty<'a> {
17    /// The name of the parent node the property belongs to. A driver bound to a single node sees
18    /// its properties under the name `default`; a composite driver sees one group per parent,
19    /// plus a `default` group that repeats the primary parent's properties.
20    pub parent: &'a str,
21    /// The property key.
22    pub key: &'a str,
23    /// The property value.
24    pub value: PropertyValue,
25}
26
27impl From<String> for PropertyValue {
28    fn from(value: String) -> Self {
29        Self(NodePropertyValue::StringValue(value))
30    }
31}
32
33impl From<&str> for PropertyValue {
34    fn from(value: &str) -> Self {
35        Self(NodePropertyValue::StringValue(value.to_owned()))
36    }
37}
38
39impl From<u32> for PropertyValue {
40    fn from(value: u32) -> Self {
41        Self(NodePropertyValue::IntValue(value))
42    }
43}
44
45impl From<bool> for PropertyValue {
46    fn from(value: bool) -> Self {
47        Self(NodePropertyValue::BoolValue(value))
48    }
49}
50
51impl From<&fidl_next_fuchsia_driver_framework::NodePropertyValue> for PropertyValue {
52    fn from(value: &fidl_next_fuchsia_driver_framework::NodePropertyValue) -> Self {
53        use fidl_next_fuchsia_driver_framework::NodePropertyValue as Next;
54        Self(match value {
55            Next::IntValue(v) => NodePropertyValue::IntValue(*v),
56            Next::StringValue(v) => NodePropertyValue::StringValue(v.clone()),
57            Next::BoolValue(v) => NodePropertyValue::BoolValue(*v),
58            Next::EnumValue(v) => NodePropertyValue::EnumValue(v.clone()),
59            Next::UnknownOrdinal_(unknown_ordinal) => {
60                NodePropertyValue::__SourceBreaking { unknown_ordinal: *unknown_ordinal }
61            }
62        })
63    }
64}
65
66impl PropertyValue {
67    /// Creates a value from something that is string-like
68    pub fn from_string(value: impl Into<String>) -> Self {
69        String::into(value.into())
70    }
71
72    /// Creates a value from something that is integer-like
73    pub fn from_int(value: impl Into<u32>) -> Self {
74        u32::into(value.into())
75    }
76
77    /// Creates a value from something that is bool-like
78    pub fn from_bool(value: impl Into<bool>) -> Self {
79        bool::into(value.into())
80    }
81
82    /// Returns the value if it is an integer property.
83    pub fn as_int(&self) -> Option<u32> {
84        match self.0 {
85            NodePropertyValue::IntValue(value) => Some(value),
86            _ => None,
87        }
88    }
89
90    /// Returns the value if it is a string property.
91    pub fn as_str(&self) -> Option<&str> {
92        match &self.0 {
93            NodePropertyValue::StringValue(value) => Some(value),
94            _ => None,
95        }
96    }
97
98    /// Returns the value if it is a boolean property.
99    pub fn as_bool(&self) -> Option<bool> {
100        match self.0 {
101            NodePropertyValue::BoolValue(value) => Some(value),
102            _ => None,
103        }
104    }
105
106    /// Returns the value if it is an enum property.
107    pub fn as_enum(&self) -> Option<&str> {
108        match &self.0 {
109            NodePropertyValue::EnumValue(value) => Some(value),
110            _ => None,
111        }
112    }
113}