fuchsia_inspect/writer/types/
string_property.rs1use crate::writer::{Inner, InnerPropertyType, InspectType, Property};
6use log::error;
7use std::borrow::Cow;
8
9#[derive(Debug, PartialEq, Eq, Default)]
16pub struct StringProperty {
17 inner: Inner<InnerPropertyType>,
18}
19
20impl<'t> Property<'t> for StringProperty {
21 type Type = &'t str;
22
23 fn set(&self, value: &'t str) {
24 if let Some(ref inner_ref) = self.inner.inner_ref() {
25 inner_ref
26 .state
27 .try_lock()
28 .and_then(|mut state| {
29 state.set_string_property(inner_ref.block_index, Cow::Borrowed(value))
30 })
31 .unwrap_or_else(|e| error!("Failed to set property. Error: {:?}", e));
32 }
33 }
34}
35
36impl InspectType for StringProperty {}
37
38crate::impl_inspect_type_internal!(StringProperty);
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43 use crate::writer::testing_utils::{get_state, GetBlockExt};
44 use crate::writer::Node;
45 use inspect_format::{BlockType, Buffer, PropertyFormat};
46
47 impl StringProperty {
48 fn load_data(&self) -> Option<String> {
49 let mut data_index = None;
50 self.get_block::<_, Buffer>(|b| data_index = Some(b.extent_index()));
51 self.inner.inner_ref().and_then(|inner_ref| {
52 inner_ref
53 .state
54 .try_lock()
55 .and_then(|state| state.load_string(data_index.unwrap()))
56 .ok()
57 })
58 }
59 }
60
61 #[fuchsia::test]
62 fn basic_string_property() {
63 let default = StringProperty::default();
65 default.set("test");
66
67 let state = get_state(4096);
68 let root = Node::new_root(state);
69 let node = root.create_child("node");
70 {
71 let property = node.create_string("property", "test");
72 property.get_block::<_, Buffer>(|property_block| {
73 assert_eq!(property_block.block_type(), Some(BlockType::BufferValue));
74 assert_eq!(property_block.total_length(), 0);
75 assert_eq!(property_block.format(), Some(PropertyFormat::StringReference));
76 });
77 assert_eq!(property.load_data().unwrap(), "test");
78 node.get_block::<_, inspect_format::Node>(|node_block| {
79 assert_eq!(node_block.child_count(), 1);
80 });
81
82 property.set("test-set");
83 property.get_block::<_, Buffer>(|property_block| {
84 assert_eq!(property_block.total_length(), 0);
85 });
86 assert_eq!(property.load_data().unwrap(), "test-set");
87 }
88 node.get_block::<_, inspect_format::Node>(|node_block| {
89 assert_eq!(node_block.child_count(), 0);
90 });
91 }
92
93 #[fuchsia::test]
94 fn string_property_interning() {
95 let default = StringProperty::default();
97 default.set("test");
98
99 let state = get_state(4096);
100 let root = Node::new_root(state);
101 let node = root.create_child("node");
102
103 let property1 = node.create_string("property", "test");
104 let mut name_idx_1 = None;
105 let mut payload_idx_1 = None;
106 property1.get_block::<_, Buffer>(|property_block| {
107 name_idx_1 = Some(property_block.name_index());
108 payload_idx_1 = Some(property_block.extent_index());
109 });
110
111 let property2 = node.create_string("test", "property");
112 let mut name_idx_2 = None;
113 let mut payload_idx_2 = None;
114 property2.get_block::<_, Buffer>(|property_block| {
115 name_idx_2 = Some(property_block.name_index());
116 payload_idx_2 = Some(property_block.extent_index());
117 });
118
119 assert_eq!(property1.load_data().unwrap(), "test");
120 assert_eq!(property2.load_data().unwrap(), "property");
121 assert_eq!(name_idx_1, payload_idx_2);
122 assert_eq!(name_idx_2, payload_idx_1);
123 }
124}