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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::{
collections::HashSet,
error::Error,
io::ErrorKind,
sync::Arc,
};
use tokio::sync::Mutex;
use crate::sensor::Sensor;
pub type ErrResult<T> = Result<T, Box<dyn Error>>;
fn mk_err<E>(kind: ErrorKind, err: E) -> Box<dyn Error>
where E: Into<Box<dyn Error + Send + Sync>>
{
Box::new(std::io::Error::new(kind, err))
}
pub fn err_not_found<E>(msg: E) -> Box<dyn Error>
where E: Into<Box<dyn Error + Send + Sync>>
{
mk_err(ErrorKind::NotFound, msg)
}
pub fn err_invalid_data<E>(msg: E) -> Box<dyn Error>
where E: Into<Box<dyn Error + Send + Sync>>
{
mk_err(ErrorKind::InvalidData, msg)
}
pub fn err_unsupported<E>(msg: E) -> Box<dyn Error>
where E: Into<Box<dyn Error + Send + Sync>>
{
mk_err(ErrorKind::Unsupported, msg)
}
pub fn err_other<E>(msg: E) -> Box<dyn Error>
where E: Into<Box<dyn Error + Send + Sync>>
{
mk_err(ErrorKind::Other, msg)
}
#[derive(Debug)]
pub enum FilterSet<T> {
All,
Only(HashSet<T>),
}
impl<T: Eq + std::hash::Hash> FilterSet<T> {
pub fn contains(&self, x: &T) -> bool {
match self {
Self::All => true,
Self::Only(set) => set.contains(x),
}
}
}
impl<T> From<Option<HashSet<T>>> for FilterSet<T> {
fn from(set: Option<HashSet<T>>) -> Self {
match set {
Some(set) => Self::Only(set),
None => Self::All,
}
}
}
pub type PropChgMsgFn = dyn Fn(&dbus::Path<'_>, &dyn dbus::arg::RefArg)
-> Option<dbus::Message> + Send + Sync;
pub struct SensorIntf<T> {
pub token: dbus_crossroads::IfaceToken<Arc<Mutex<Sensor>>>,
pub msgfns: T,
}
impl<T> SensorIntf<T> {
pub fn build<F, I>(cr: &mut dbus_crossroads::Crossroads, intf: I, mkprops: F) -> Self
where F: FnOnce(&mut dbus_crossroads::IfaceBuilder<Arc<Mutex<Sensor>>>) -> T,
I: Into<dbus::strings::Interface<'static>>
{
let mut msgfns: Option<T> = None;
let ctor = |b: &mut dbus_crossroads::IfaceBuilder<Arc<Mutex<Sensor>>>| {
msgfns = Some(mkprops(b))
};
let token = cr.register(intf, ctor);
Self {
token,
msgfns: msgfns.expect("no msgfns set?"),
}
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct InventoryPath(pub dbus::Path<'static>);
#[derive(Debug, Clone)]
pub struct SensorPath(pub dbus::Path<'static>);