fdf_component/node/
properties.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
// Copyright 2024 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 fidl_fuchsia_driver_framework::{NodePropertyKey, NodePropertyValue};

/// A newtype wrapper that can be used to construct [`NodePropertyKey`]-compatible values
/// for [`crate::NodeBuilder::add_property`]
pub struct PropertyKey(pub(crate) NodePropertyKey);

impl From<String> for PropertyKey {
    fn from(value: String) -> Self {
        Self(NodePropertyKey::StringValue(value))
    }
}

impl From<&str> for PropertyKey {
    fn from(value: &str) -> Self {
        Self(NodePropertyKey::StringValue(value.to_owned()))
    }
}

impl From<u32> for PropertyKey {
    fn from(value: u32) -> Self {
        Self(NodePropertyKey::IntValue(value))
    }
}

impl PropertyKey {
    /// Creates a key from something that is string-like
    pub fn from_string(value: impl Into<String>) -> Self {
        String::into(value.into())
    }

    /// Creates a key from something that is integer-like
    pub fn from_int(value: impl Into<u32>) -> Self {
        u32::into(value.into())
    }
}

/// A newtype wrapper that can be used to construct [`NodePropertyValue`]-compatible values
/// for [`crate::NodeBuilder::add_property`]
pub struct PropertyValue(pub(crate) NodePropertyValue);

impl From<String> for PropertyValue {
    fn from(value: String) -> Self {
        Self(NodePropertyValue::StringValue(value))
    }
}

impl From<&str> for PropertyValue {
    fn from(value: &str) -> Self {
        Self(NodePropertyValue::StringValue(value.to_owned()))
    }
}

impl From<u32> for PropertyValue {
    fn from(value: u32) -> Self {
        Self(NodePropertyValue::IntValue(value))
    }
}

impl From<bool> for PropertyValue {
    fn from(value: bool) -> Self {
        Self(NodePropertyValue::BoolValue(value))
    }
}

impl PropertyValue {
    /// Creates a value from something that is string-like
    pub fn from_string(value: impl Into<String>) -> Self {
        String::into(value.into())
    }

    /// Creates a value from something that is integer-like
    pub fn from_int(value: impl Into<u32>) -> Self {
        u32::into(value.into())
    }

    /// Creates a value from something that is bool-like
    pub fn from_bool(value: impl Into<bool>) -> Self {
        bool::into(value.into())
    }
}