Skip to content

Commit 0313dc8

Browse files
committed
feat: Support extended Things
1 parent 284bea9 commit 0313dc8

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ futures-util = "0.3.21"
1818
tracing = "0.1.35"
1919

2020
[dev-dependencies]
21+
serde = { version = "1.0.171", features = ["derive"] }
2122
tokio = { version = "1.19.2", features = ["macros", "rt-multi-thread"] }
2223
tracing-subscriber = "0.3.11"
2324
wot-serve = "0.3.1"

examples/list.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
use std::future::ready;
22

33
use futures_util::StreamExt;
4+
use serde::{Deserialize, Serialize};
45
use tracing::{info, warn};
56
use wot_discovery::Discoverer;
7+
use wot_td::extend::ExtendableThing;
8+
9+
#[derive(Debug, Clone, Serialize, Deserialize)]
10+
struct A {}
11+
12+
impl ExtendableThing for A {
13+
type InteractionAffordance = ();
14+
type PropertyAffordance = ();
15+
type ActionAffordance = ();
16+
type EventAffordance = ();
17+
type Form = ();
18+
type ExpectedResponse = ();
19+
type DataSchema = ();
20+
type ObjectSchema = ();
21+
type ArraySchema = ();
22+
}
623

724
#[tokio::main]
825
async fn main() -> Result<(), Box<dyn std::error::Error>> {
926
tracing_subscriber::fmt().init();
1027

11-
let d = Discoverer::new()?;
28+
let d = Discoverer::new()?.ext::<A>();
1229

1330
d.stream()?
1431
.for_each(|thing| {

src/lib.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
//!
77
//! - [x] [mDNS-SD (HTTP)](https://www.w3.org/TR/wot-discovery/#introduction-dns-sd-sec)
88
9+
use std::marker::PhantomData;
10+
911
use futures_core::Stream;
1012
use futures_util::StreamExt;
1113
use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo};
1214
use tracing::debug;
1315

14-
use wot_td::thing::Thing;
16+
use wot_td::{
17+
extend::{Extend, ExtendablePiece, ExtendableThing},
18+
hlist::Nil,
19+
thing::Thing,
20+
};
1521

1622
/// The error type for Discovery operation
1723
#[derive(thiserror::Error, Debug)]
@@ -31,12 +37,15 @@ pub type Result<T> = std::result::Result<T, Error>;
3137
const WELL_KNOWN: &str = "/.well-known/wot";
3238

3339
/// Discover [Web Of Things](https://www.w3.org/WoT/) via a supported Introduction Mechanism.
34-
pub struct Discoverer {
40+
pub struct Discoverer<Other: ExtendableThing + ExtendablePiece = Nil> {
3541
mdns: ServiceDaemon,
3642
service_type: String,
43+
_other: PhantomData<Other>,
3744
}
3845

39-
async fn get_thing(info: ServiceInfo) -> Result<Thing> {
46+
async fn get_thing<Other: ExtendableThing + ExtendablePiece>(
47+
info: ServiceInfo,
48+
) -> Result<Thing<Other>> {
4049
let host = info.get_addresses().iter().next().ok_or(Error::NoAddress)?;
4150
let port = info.get_port();
4251
let props = info.get_properties();
@@ -60,11 +69,36 @@ impl Discoverer {
6069
pub fn new() -> Result<Self> {
6170
let mdns = ServiceDaemon::new()?;
6271
let service_type = "_wot._tcp.local.".to_owned();
63-
Ok(Self { mdns, service_type })
72+
Ok(Self {
73+
mdns,
74+
service_type,
75+
_other: PhantomData,
76+
})
77+
}
78+
}
79+
80+
impl<Other: ExtendableThing + ExtendablePiece> Discoverer<Other> {
81+
/// Extend the [Discoverer] with a [ExtendableThing]
82+
pub fn ext<T>(self) -> Discoverer<Other::Target>
83+
where
84+
Other: Extend<T>,
85+
Other::Target: ExtendableThing + ExtendablePiece,
86+
{
87+
let Discoverer {
88+
mdns,
89+
service_type,
90+
_other,
91+
} = self;
92+
93+
Discoverer {
94+
mdns,
95+
service_type,
96+
_other: PhantomData,
97+
}
6498
}
6599

66100
/// Returns an Stream of discovered things
67-
pub fn stream(&self) -> Result<impl Stream<Item = Result<Thing>>> {
101+
pub fn stream(&self) -> Result<impl Stream<Item = Result<Thing<Other>>>> {
68102
let receiver = self.mdns.browse(&self.service_type)?;
69103

70104
let s = receiver.into_stream().filter_map(|v| async move {

tests/mdns.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use std::time::Duration;
22

33
use futures_util::StreamExt;
44

5+
use serde::{Deserialize, Serialize};
56
use wot_discovery::Discoverer;
67
use wot_serve::servient::*;
78

89
use tokio::task;
10+
use wot_td::extend::ExtendableThing;
911

1012
async fn run_servient() {
1113
let servient = Servient::builder("TestThing")
@@ -22,6 +24,21 @@ async fn run_servient() {
2224
.await;
2325
}
2426

27+
#[derive(Debug, Clone, Serialize, Deserialize)]
28+
struct A {}
29+
30+
impl ExtendableThing for A {
31+
type InteractionAffordance = ();
32+
type PropertyAffordance = ();
33+
type ActionAffordance = ();
34+
type EventAffordance = ();
35+
type Form = ();
36+
type ExpectedResponse = ();
37+
type DataSchema = ();
38+
type ObjectSchema = ();
39+
type ArraySchema = ();
40+
}
41+
2542
#[tokio::test(flavor = "multi_thread")]
2643
async fn discoverer() -> Result<(), Box<dyn std::error::Error>> {
2744
let local = task::LocalSet::new();
@@ -43,6 +60,18 @@ async fn discoverer() -> Result<(), Box<dyn std::error::Error>> {
4360

4461
assert_eq!("TestThing", t.title);
4562
});
63+
64+
task::spawn_local(async {
65+
let d = Discoverer::new().unwrap().ext::<A>();
66+
67+
let t = std::pin::pin!(d.stream().unwrap())
68+
.next()
69+
.await
70+
.unwrap()
71+
.unwrap();
72+
73+
assert_eq!("TestThing", t.title);
74+
});
4675
})
4776
.await;
4877

0 commit comments

Comments
 (0)