Debian Codegraph @

third_party/rust/coremidi/src/object.rs

raw
1 #![allow(non_upper_case_globals)]
2
3 use core_foundation_sys::base::OSStatus;
4
5 use coremidi_sys::{
6 kMIDIObjectType_Destination, kMIDIObjectType_Device, kMIDIObjectType_Entity,
7 kMIDIObjectType_ExternalDestination, kMIDIObjectType_ExternalDevice,
8 kMIDIObjectType_ExternalEntity, kMIDIObjectType_ExternalSource, kMIDIObjectType_Other,
9 kMIDIObjectType_Source, MIDIObjectRef, SInt32,
10 };
11
12 use std::fmt;
13
14 use crate::properties::{
15 BooleanProperty, IntegerProperty, Properties, PropertyGetter, PropertySetter, StringProperty,
16 };
17
18 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
19 pub enum ObjectType {
20 Other,
21 Device,
22 Entity,
23 Source,
24 Destination,
25 ExternalDevice,
26 ExternalEntity,
27 ExternalSource,
28 ExternalDestination,
29 }
30
31 impl ObjectType {
32 pub fn from(value: i32) -> Result<ObjectType, i32> {
33 match value {
34 kMIDIObjectType_Other => Ok(ObjectType::Other),
35 kMIDIObjectType_Device => Ok(ObjectType::Device),
36 kMIDIObjectType_Entity => Ok(ObjectType::Entity),
37 kMIDIObjectType_Source => Ok(ObjectType::Source),
38 kMIDIObjectType_Destination => Ok(ObjectType::Destination),
39 kMIDIObjectType_ExternalDevice => Ok(ObjectType::ExternalDevice),
40 kMIDIObjectType_ExternalEntity => Ok(ObjectType::ExternalEntity),
41 kMIDIObjectType_ExternalSource => Ok(ObjectType::ExternalSource),
42 kMIDIObjectType_ExternalDestination => Ok(ObjectType::ExternalDestination),
43 unknown => Err(unknown),
44 }
45 }
46 }
47
48 /// A [MIDI Object](https://developer.apple.com/reference/coremidi/midiobjectref).
49 ///
50 /// The base class of many CoreMIDI objects.
51 ///
52 #[derive(PartialEq)]
53 pub struct Object(pub(crate) MIDIObjectRef);
54
55 impl Object {
56 /// Get the name for the object.
57 ///
58 pub fn name(&self) -> Option<String> {
59 Properties::name().value_from(self).ok()
60 }
61
62 /// Get the unique id for the object.
63 ///
64 pub fn unique_id(&self) -> Option<u32> {
65 Properties::unique_id()
66 .value_from(self)
67 .ok()
68 .map(|v: SInt32| v as u32)
69 }
70
71 /// Get the display name for the object.
72 ///
73 pub fn display_name(&self) -> Option<String> {
74 Properties::display_name().value_from(self).ok()
75 }
76
77 /// Sets an object's string-type property.
78 ///
79 pub fn set_property_string(&self, name: &str, value: &str) -> Result<(), OSStatus> {
80 StringProperty::new(name).set_value(self, value)
81 }
82
83 /// Gets an object's string-type property.
84 ///
85 pub fn get_property_string(&self, name: &str) -> Result<String, OSStatus> {
86 StringProperty::new(name).value_from(self)
87 }
88
89 /// Sets an object's integer-type property.
90 ///
91 pub fn set_property_integer(&self, name: &str, value: i32) -> Result<(), OSStatus> {
92 IntegerProperty::new(name).set_value(self, value)
93 }
94
95 /// Gets an object's integer-type property.
96 ///
97 pub fn get_property_integer(&self, name: &str) -> Result<i32, OSStatus> {
98 IntegerProperty::new(name).value_from(self)
99 }
100
101 /// Sets an object's boolean-type property.
102 ///
103 /// CoreMIDI treats booleans as integers (0/1) but this API uses native bool types
104 ///
105 pub fn set_property_boolean(&self, name: &str, value: bool) -> Result<(), OSStatus> {
106 BooleanProperty::new(name).set_value(self, value)
107 }
108
109 /// Gets an object's boolean-type property.
110 ///
111 /// CoreMIDI treats booleans as integers (0/1) but this API uses native bool types
112 ///
113 pub fn get_property_boolean(&self, name: &str) -> Result<bool, OSStatus> {
114 BooleanProperty::new(name).value_from(self)
115 }
116 }
117
118 impl fmt::Debug for Object {
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120 write!(f, "Object({:x})", self.0 as usize)
121 }
122 }
123
124 #[cfg(test)]
125 mod tests {
126 use crate::object::ObjectType;
127
128 use coremidi_sys::{
129 kMIDIObjectType_Destination, kMIDIObjectType_Device, kMIDIObjectType_Entity,
130 kMIDIObjectType_ExternalDestination, kMIDIObjectType_ExternalDevice,
131 kMIDIObjectType_ExternalEntity, kMIDIObjectType_ExternalSource, kMIDIObjectType_Other,
132 kMIDIObjectType_Source,
133 };
134
135 #[test]
136 fn objecttype_from() {
137 assert_eq!(
138 ObjectType::from(kMIDIObjectType_Other),
139 Ok(ObjectType::Other)
140 );
141 assert_eq!(
142 ObjectType::from(kMIDIObjectType_Device),
143 Ok(ObjectType::Device)
144 );
145 assert_eq!(
146 ObjectType::from(kMIDIObjectType_Entity),
147 Ok(ObjectType::Entity)
148 );
149 assert_eq!(
150 ObjectType::from(kMIDIObjectType_Source),
151 Ok(ObjectType::Source)
152 );
153 assert_eq!(
154 ObjectType::from(kMIDIObjectType_Destination),
155 Ok(ObjectType::Destination)
156 );
157 assert_eq!(
158 ObjectType::from(kMIDIObjectType_ExternalDevice),
159 Ok(ObjectType::ExternalDevice)
160 );
161 assert_eq!(
162 ObjectType::from(kMIDIObjectType_ExternalEntity),
163 Ok(ObjectType::ExternalEntity)
164 );
165 assert_eq!(
166 ObjectType::from(kMIDIObjectType_ExternalSource),
167 Ok(ObjectType::ExternalSource)
168 );
169 assert_eq!(
170 ObjectType::from(kMIDIObjectType_ExternalDestination),
171 Ok(ObjectType::ExternalDestination)
172 );
173 }
174
175 #[test]
176 fn objecttype_from_error() {
177 assert_eq!(ObjectType::from(0xffff_i32), Err(0xffff));
178 }
179 }