Skip to content

Commit 2e9e193

Browse files
committed
Similarly for device
1 parent cf3fe4f commit 2e9e193

File tree

10 files changed

+313
-132
lines changed

10 files changed

+313
-132
lines changed

gateway-addon-rust-codegen/src/lib.rs

+98-13
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ use std::str::FromStr;
55
use syn::DeriveInput;
66

77
/// Use this on a struct to generate a built adapter around it, including useful impls.
8-
///
8+
///
99
/// # Examples
1010
/// ```
1111
/// # use gateway_addon_rust::prelude::*;
1212
/// # use async_trait::async_trait;
1313
/// #[adapter]
14-
/// struct ExampleAdapter { foo: i32 }
15-
///
14+
/// struct ExampleAdapter {
15+
/// foo: i32,
16+
/// }
17+
///
1618
/// #[async_trait]
1719
/// impl Adapter for BuiltExampleAdapter {
1820
/// async fn on_unload(&mut self) -> Result<(), String> {
@@ -26,13 +28,15 @@ use syn::DeriveInput;
2628
/// # use gateway_addon_rust::{prelude::*, adapter::AdapterHandleWrapper};
2729
/// # use std::ops::{Deref, DerefMut};
2830
/// # use async_trait::async_trait;
29-
/// struct ExampleAdapter { foo: i32 }
30-
///
31-
/// struct BuiltExampleAdapter{
31+
/// struct ExampleAdapter {
32+
/// foo: i32,
33+
/// }
34+
///
35+
/// struct BuiltExampleAdapter {
3236
/// data: ExampleAdapter,
33-
/// adapter_handle: AdapterHandle
37+
/// adapter_handle: AdapterHandle,
3438
/// }
35-
///
39+
///
3640
/// impl AdapterHandleWrapper for BuiltExampleAdapter {
3741
/// fn adapter_handle(&self) -> &AdapterHandle {
3842
/// &self.adapter_handle
@@ -41,27 +45,30 @@ use syn::DeriveInput;
4145
/// &mut self.adapter_handle
4246
/// }
4347
/// }
44-
///
48+
///
4549
/// impl BuildAdapter for ExampleAdapter {
4650
/// type BuiltAdapter = BuiltExampleAdapter;
4751
/// fn build(data: Self, adapter_handle: AdapterHandle) -> Self::BuiltAdapter {
48-
/// BuiltExampleAdapter { data, adapter_handle }
52+
/// BuiltExampleAdapter {
53+
/// data,
54+
/// adapter_handle,
55+
/// }
4956
/// }
5057
/// }
51-
///
58+
///
5259
/// impl Deref for BuiltExampleAdapter {
5360
/// type Target = ExampleAdapter;
5461
/// fn deref(&self) -> &Self::Target {
5562
/// &self.data
5663
/// }
5764
/// }
58-
///
65+
///
5966
/// impl DerefMut for BuiltExampleAdapter {
6067
/// fn deref_mut(&mut self) -> &mut Self::Target {
6168
/// &mut self.data
6269
/// }
6370
/// }
64-
///
71+
///
6572
/// #[async_trait]
6673
/// impl Adapter for BuiltExampleAdapter {
6774
/// // ...
@@ -72,6 +79,84 @@ pub fn adapter(_args: TokenStream, input: TokenStream) -> TokenStream {
7279
apply_macro(input, "adapter", "Adapter")
7380
}
7481

82+
/// Use this on a struct to generate a built device around it, including useful impls.
83+
///
84+
/// # Examples
85+
/// ```
86+
/// # use gateway_addon_rust::prelude::*;
87+
/// # use async_trait::async_trait;
88+
/// #[device]
89+
/// struct ExamplDevice {
90+
/// foo: i32,
91+
/// }
92+
///
93+
/// impl DeviceStructure for ExampleDevice {
94+
/// // ...
95+
/// # fn id(&self) -> String {
96+
/// # "example-device".to_owned()
97+
/// # }
98+
/// # fn description(&self) -> DeviceDescription {
99+
/// # DeviceDescription::default()
100+
/// # }
101+
/// }
102+
///
103+
/// #[async_trait]
104+
/// impl Device for BuiltExampleDevice {}
105+
/// ```
106+
/// will expand to
107+
/// ```
108+
/// # use gateway_addon_rust::{prelude::*, device::DeviceHandleWrapper};
109+
/// # use std::ops::{Deref, DerefMut};
110+
/// # use async_trait::async_trait;
111+
/// struct ExampleDevice { foo: i32 }
112+
///
113+
/// struct BuiltExampleDevice{
114+
/// data: ExampleDevice,,
115+
/// device_handle: DeviceHandle
116+
/// }
117+
///
118+
/// impl DeviceHandleWrapper for BuiltExampleDevice {
119+
/// fn device_handle(&self) -> &DeviceHandle {
120+
/// &self.device_handle
121+
/// }
122+
/// fn device_handle_mut(&mut self) -> &mut DeviceHandle {
123+
/// &mut self.device_handle
124+
/// }
125+
/// }
126+
///
127+
/// impl BuildDevice for ExampleDevice {
128+
/// type BuiltDevice = BuiltExampleDevice;
129+
/// fn build(data: Self, device_handle: DeviceHandle) -> Self::BuiltDevice {
130+
/// BuiltExampleDevice { data, device_handle }
131+
/// }
132+
/// }
133+
///
134+
/// impl Deref for BuiltExampleDevice {
135+
/// type Target = ExampleDevice;
136+
/// fn deref(&self) -> &Self::Target {
137+
/// &self.data
138+
/// }
139+
/// }
140+
///
141+
/// impl DerefMut for BuiltExampleDevice {
142+
/// fn deref_mut(&mut self) -> &mut Self::Target {
143+
/// &mut self.data
144+
/// }
145+
/// }
146+
///
147+
/// impl DeviceStructure for ExampleDevice {
148+
/// // ...
149+
/// # fn id(&self) -> String {
150+
/// # "example-device".to_owned()
151+
/// # }
152+
/// # fn description(&self) -> DeviceDescription {
153+
/// # DeviceDescription::default()
154+
/// # }
155+
/// }
156+
///
157+
/// #[async_trait]
158+
/// impl Device for BuiltExampleDevice {}
159+
/// ```
75160
#[proc_macro_attribute]
76161
pub fn device(_args: TokenStream, input: TokenStream) -> TokenStream {
77162
apply_macro(input, "device", "Device")

src/adapter/adapter_handle.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
55
*/
66

7-
use crate::{client::Client, error::WebthingsError, Adapter, Device, DeviceBuilder, DeviceHandle};
7+
use crate::{
8+
client::Client,
9+
device::{BuildDevice, DeviceStructure},
10+
error::WebthingsError,
11+
Adapter, Device, DeviceHandle,
12+
};
813
use std::{
914
collections::HashMap,
1015
sync::{Arc, Weak},
@@ -38,16 +43,12 @@ impl AdapterHandle {
3843
}
3944
}
4045

41-
/// Build and add a new device using the given [device builder][crate::DeviceBuilder].
42-
pub async fn add_device<D, B>(
46+
/// Build and add a new device using the given data struct.
47+
pub async fn add_device<D: BuildDevice + DeviceStructure>(
4348
&mut self,
44-
device_builder: B,
45-
) -> Result<Arc<Mutex<Box<dyn Device>>>, WebthingsError>
46-
where
47-
D: Device,
48-
B: DeviceBuilder<Device = D>,
49-
{
50-
let device_description = device_builder.full_description()?;
49+
device: D,
50+
) -> Result<Arc<Mutex<Box<dyn Device>>>, WebthingsError> {
51+
let device_description = device.full_description()?;
5152

5253
let message: Message = DeviceAddedNotificationMessageData {
5354
plugin_id: self.plugin_id.clone(),
@@ -65,16 +66,16 @@ impl AdapterHandle {
6566
self.weak.clone(),
6667
self.plugin_id.clone(),
6768
self.adapter_id.clone(),
68-
device_builder.id(),
69-
device_builder.description(),
69+
device.id(),
70+
device.description(),
7071
);
7172

72-
let properties = device_builder.properties();
73-
let actions = device_builder.actions();
74-
let events = device_builder.events();
73+
let properties = device.properties();
74+
let actions = device.actions();
75+
let events = device.events();
7576

7677
let device: Arc<Mutex<Box<dyn Device>>> =
77-
Arc::new(Mutex::new(Box::new(device_builder.build(device_handle))));
78+
Arc::new(Mutex::new(Box::new(D::build(device, device_handle))));
7879
let device_weak = Arc::downgrade(&device);
7980

8081
{
@@ -145,7 +146,9 @@ impl AdapterHandle {
145146
#[cfg(test)]
146147
pub(crate) mod tests {
147148
use crate::{
148-
client::Client, device::tests::MockDeviceBuilder, AdapterHandle, Device, DeviceBuilder,
149+
client::Client,
150+
device::{tests::MockDevice, DeviceStructure},
151+
AdapterHandle, Device,
149152
};
150153
use rstest::{fixture, rstest};
151154
use std::sync::Arc;
@@ -156,8 +159,8 @@ pub(crate) mod tests {
156159
adapter: &mut AdapterHandle,
157160
device_id: &str,
158161
) -> Arc<Mutex<Box<dyn Device>>> {
159-
let device_builder = MockDeviceBuilder::new(device_id.to_owned());
160-
let expected_description = device_builder.full_description().unwrap();
162+
let device = MockDevice::new(device_id.to_owned());
163+
let expected_description = device.full_description().unwrap();
161164

162165
let plugin_id = adapter.plugin_id.to_owned();
163166
let adapter_id = adapter.adapter_id.to_owned();
@@ -178,7 +181,7 @@ pub(crate) mod tests {
178181
.times(1)
179182
.returning(|_| Ok(()));
180183

181-
adapter.add_device(device_builder).await.unwrap()
184+
adapter.add_device(device).await.unwrap()
182185
}
183186

184187
const PLUGIN_ID: &str = "plugin_id";

src/adapter/adapter_trait.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use webthings_gateway_ipc_types::DeviceWithoutId;
1818
///
1919
/// # Examples
2020
/// ```no_run
21-
/// # use gateway_addon_rust::{prelude::*, plugin::connect, example::ExampleDeviceBuilder, error::WebthingsError, adapter::AdapterHandleWrapper};
21+
/// # use gateway_addon_rust::{prelude::*, plugin::connect, example::ExampleDevice, error::WebthingsError, adapter::AdapterHandleWrapper};
2222
/// # use webthings_gateway_ipc_types::DeviceWithoutId;
2323
/// # use async_trait::async_trait;
2424
/// # use as_any::Downcast;
@@ -41,7 +41,7 @@ use webthings_gateway_ipc_types::DeviceWithoutId;
4141
/// impl BuiltExampleAdapter {
4242
/// pub async fn init(&mut self) -> Result<(), WebthingsError> {
4343
/// self.adapter_handle_mut()
44-
/// .add_device(ExampleDeviceBuilder::new())
44+
/// .add_device(ExampleDevice::new())
4545
/// .await?;
4646
/// Ok(())
4747
/// }
@@ -107,7 +107,7 @@ pub trait Adapter: AdapterHandleWrapper + Send + Sync + AsAny + 'static {
107107
impl Downcast for dyn Adapter {}
108108

109109
/// A trait used to wrap an [adapter handle][AdapterHandle].
110-
///
110+
///
111111
/// When you use the [adapter][macro@crate::adapter] macro, this will be implemented automatically.
112112
///
113113
/// # Examples
@@ -178,7 +178,7 @@ pub trait AdapterHandleWrapper {
178178
pub trait BuildAdapter {
179179
/// Type of [Adapter] to build.
180180
type BuiltAdapter: Adapter;
181-
181+
182182
/// Build the [adapter][Adapter] from a data struct and an [adapter handle][AdapterHandle].
183183
fn build(data: Self, adapter_handle: AdapterHandle) -> Self::BuiltAdapter;
184184
}

src/api_handler/api_handler_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use async_trait::async_trait;
1515
/// # Examples
1616
/// ```no_run
1717
/// # use gateway_addon_rust::{
18-
/// # prelude::*, plugin::connect, example::ExampleDeviceBuilder,
18+
/// # prelude::*, plugin::connect,
1919
/// # api_handler::{ApiHandler, ApiRequest, ApiResponse}, error::WebthingsError
2020
/// # };
2121
/// # use async_trait::async_trait;

src/device/device_description.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use webthings_gateway_ipc_types::{
1212

1313
/// A struct which represents a WoT [device description][webthings_gateway_ipc_types::Device].
1414
///
15-
/// This is used by [DeviceBuilder][crate::DeviceBuilder].
15+
/// This is used by [DeviceStructure][crate::DeviceStructure].
1616
///
1717
/// Use the provided builder methods instead of directly writing to the struct fields.
1818
///

0 commit comments

Comments
 (0)