| 1 |
use crate::{Path as PathName, Interface as IfaceName, Member as MemberName, Signature, Message}; |
| 2 |
use std::borrow::Cow; |
| 3 |
use std::collections::BTreeMap; |
| 4 |
use std::any::Any; |
| 5 |
use std::mem; |
| 6 |
use crate::arg::{Arg, Append, Get, ArgBuilder, TypeMismatchError, IterAppend}; |
| 7 |
use std::marker::PhantomData; |
| 8 |
use super::MethodErr; |
| 9 |
use super::handlers::{Handlers, DebugMethod, DebugProp, Par, ParInfo, Mut, MutCtx}; |
| 10 |
use super::crossroads::{Crossroads, PathData}; |
| 11 |
|
| 12 |
fn build_argvec<A: ArgBuilder>(a: A::strs) -> Vec<Argument<'static>> { |
| 13 |
let mut v = vec!(); |
| 14 |
A::strs_sig(a, |name, sig| { |
| 15 |
v.push(Argument { name: name.into(), sig }) |
| 16 |
}); |
| 17 |
v |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
#[derive(Default, Debug, Clone)] |
| 22 |
struct Annotations(Option<BTreeMap<String, String>>); |
| 23 |
|
| 24 |
#[derive(Debug, Clone)] |
| 25 |
pub struct Argument<'a> { |
| 26 |
name: Cow<'a, str>, |
| 27 |
sig: Signature<'a>, |
| 28 |
} |
| 29 |
|
| 30 |
#[derive(Debug)] |
| 31 |
pub struct IfaceInfo<'a, H: Handlers> { |
| 32 |
pub (crate) name: IfaceName<'a>, |
| 33 |
pub (crate) methods: Vec<MethodInfo<'a, H>>, |
| 34 |
pub (crate) props: Vec<PropInfo<'a, H>>, |
| 35 |
pub (crate) signals: Vec<SignalInfo<'a>>, |
| 36 |
} |
| 37 |
|
| 38 |
#[derive(Debug)] |
| 39 |
pub struct MethodInfo<'a, H: Handlers> { |
| 40 |
name: MemberName<'a>, |
| 41 |
handler: DebugMethod<H>, |
| 42 |
i_args: Vec<Argument<'a>>, |
| 43 |
o_args: Vec<Argument<'a>>, |
| 44 |
anns: Annotations, |
| 45 |
} |
| 46 |
|
| 47 |
impl<'a, H: Handlers> MethodInfo<'a, H> { |
| 48 |
pub fn name(&self) -> &MemberName<'a> { &self.name } |
| 49 |
pub fn handler(&self) -> &H::Method { &self.handler.0 } |
| 50 |
pub fn handler_mut(&mut self) -> &mut H::Method { &mut self.handler.0 } |
| 51 |
} |
| 52 |
|
| 53 |
#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Debug)] |
| 54 |
/// Enumerates the different signaling behaviors a Property can have |
| 55 |
/// to being changed. |
| 56 |
pub enum EmitsChangedSignal { |
| 57 |
/// The Property emits a signal that includes the new value. |
| 58 |
True, |
| 59 |
/// The Property emits a signal that does not include the new value. |
| 60 |
Invalidates, |
| 61 |
/// The Property cannot be changed. |
| 62 |
Const, |
| 63 |
/// The Property does not emit a signal when changed. |
| 64 |
False, |
| 65 |
} |
| 66 |
|
| 67 |
#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Debug)] |
| 68 |
/// The possible access characteristics a Property can have. |
| 69 |
pub enum Access { |
| 70 |
/// The Property can only be read (Get). |
| 71 |
Read, |
| 72 |
/// The Property can be read or written. |
| 73 |
ReadWrite, |
| 74 |
/// The Property can only be written (Set). |
| 75 |
Write, |
| 76 |
} |
| 77 |
|
| 78 |
#[derive(Debug)] |
| 79 |
pub struct PropInfo<'a, H: Handlers> { |
| 80 |
pub (crate) name: MemberName<'a>, |
| 81 |
pub (crate) handlers: DebugProp<H>, |
| 82 |
anns: Annotations, |
| 83 |
sig: Signature<'a>, |
| 84 |
emits: EmitsChangedSignal, |
| 85 |
auto_emit: bool, |
| 86 |
rw: Access, |
| 87 |
} |
| 88 |
|
| 89 |
#[derive(Debug)] |
| 90 |
pub struct SignalInfo<'a> { |
| 91 |
name: MemberName<'a>, |
| 92 |
args: Vec<Argument<'a>>, |
| 93 |
anns: Annotations, |
| 94 |
} |
| 95 |
|
| 96 |
#[derive(Debug)] |
| 97 |
pub struct IfaceInfoBuilder<'a, I: 'static, H: Handlers> { |
| 98 |
cr: Option<&'a mut Crossroads<H>>, |
| 99 |
info: IfaceInfo<'static, H>, |
| 100 |
_dummy: PhantomData<*const I>, |
| 101 |
} |
| 102 |
|
| 103 |
impl<'a, I, H: Handlers> IfaceInfoBuilder<'a, I, H> { |
| 104 |
pub fn new(cr: Option<&'a mut Crossroads<H>>, name: IfaceName<'static>) -> Self { |
| 105 |
IfaceInfoBuilder { cr, _dummy: PhantomData, info: IfaceInfo::new_empty(name) } |
| 106 |
} |
| 107 |
|
| 108 |
pub fn signal<A: ArgBuilder, N: Into<MemberName<'static>>>(mut self, name: N, args: A::strs) -> Self { |
| 109 |
let s = SignalInfo { name: name.into(), args: build_argvec::<A>(args), anns: Default::default() }; |
| 110 |
self.info.signals.push(s); |
| 111 |
self |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
impl<'a, I: 'static, H: Handlers> Drop for IfaceInfoBuilder<'a, I, H> { |
| 116 |
fn drop(&mut self) { |
| 117 |
if let Some(ref mut cr) = self.cr { |
| 118 |
let info = IfaceInfo::new_empty(self.info.name.clone()); // workaround for not being able to consume self.info |
| 119 |
cr.register_custom::<I>(mem::replace(&mut self.info, info)); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
impl<'a, I: 'static> IfaceInfoBuilder<'a, I, Par> { |
| 125 |
pub fn method<IA: ArgBuilder, OA: ArgBuilder, N, F>(mut self, name: N, in_args: IA::strs, out_args: OA::strs, f: F) -> Self |
| 126 |
where N: Into<MemberName<'static>>, F: Fn(&I, &ParInfo, IA) -> Result<OA, MethodErr> + Send + Sync + 'static { |
| 127 |
let f: <Par as Handlers>::Method = Box::new(move |data, info| { |
| 128 |
let iface: &I = data.downcast_ref().unwrap(); |
| 129 |
let r = IA::read(info.msg()).map_err(From::from); |
| 130 |
let r = r.and_then(|ia| f(iface, info, ia)); |
| 131 |
match r { |
| 132 |
Err(e) => Some(e.to_message(info.msg())), |
| 133 |
Ok(r) => { |
| 134 |
let mut m = info.msg().method_return(); |
| 135 |
OA::append(r, &mut m); |
| 136 |
Some(m) |
| 137 |
}, |
| 138 |
} |
| 139 |
}); |
| 140 |
|
| 141 |
let m = MethodInfo { name: name.into(), handler: DebugMethod(f), |
| 142 |
i_args: build_argvec::<IA>(in_args), o_args: build_argvec::<OA>(out_args), anns: Default::default() }; |
| 143 |
self.info.methods.push(m); |
| 144 |
self |
| 145 |
} |
| 146 |
|
| 147 |
pub fn prop_rw<T, N, G, S>(mut self, name: N, getf: G, setf: S) -> Self |
| 148 |
where T: Arg + Append + for<'z> Get<'z> + Send + Sync + 'static, |
| 149 |
N: Into<MemberName<'static>>, |
| 150 |
G: Fn(&I, &ParInfo) -> Result<T, MethodErr> + Send + Sync + 'static, |
| 151 |
S: Fn(&I, &ParInfo, T) -> Result<(), MethodErr> + Send + Sync + 'static |
| 152 |
{ |
| 153 |
let p = PropInfo::new(name.into(), T::signature(), Some(Par::typed_getprop(getf)), Some(Par::typed_setprop(setf))); |
| 154 |
self.info.props.push(p); |
| 155 |
self |
| 156 |
} |
| 157 |
|
| 158 |
pub fn prop_ro<T, N, G>(mut self, name: N, getf: G) -> Self |
| 159 |
where T: Arg + Append + Send + Sync + 'static, |
| 160 |
N: Into<MemberName<'static>>, |
| 161 |
G: Fn(&I, &ParInfo) -> Result<T, MethodErr> + Send + Sync + 'static, |
| 162 |
{ |
| 163 |
let p = PropInfo::new(name.into(), T::signature(), Some(Par::typed_getprop(getf)), None); |
| 164 |
self.info.props.push(p); |
| 165 |
self |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
impl<'a, I: 'static> IfaceInfoBuilder<'a, I, Mut> { |
| 171 |
pub fn method_iface<IA: ArgBuilder, OA: ArgBuilder, N, F>(mut self, name: N, in_args: IA::strs, out_args: OA::strs, f: F) -> Self |
| 172 |
where N: Into<MemberName<'static>>, F: FnMut(&mut I, &MutCtx, IA) -> Result<OA, MethodErr> + Send + Sync + 'static { |
| 173 |
let m = MethodInfo { name: name.into(), handler: DebugMethod(Mut::typed_method_iface(f)), |
| 174 |
i_args: build_argvec::<IA>(in_args), o_args: build_argvec::<OA>(out_args), anns: Default::default() }; |
| 175 |
self.info.methods.push(m); |
| 176 |
self |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
impl<H: Handlers> MethodInfo<'_, H> { |
| 181 |
pub fn new(name: MemberName<'static>, f: H::Method) -> Self { |
| 182 |
MethodInfo { name: name, handler: DebugMethod(f), |
| 183 |
i_args: Default::default(), o_args: Default::default(), anns: Default::default() } |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
impl<H: Handlers> PropInfo<'_, H> { |
| 188 |
pub fn new(name: MemberName<'static>, sig: Signature<'static>, get: Option<H::GetProp>, |
| 189 |
set: Option<H::SetProp>) -> Self { |
| 190 |
let a = match (&get, &set) { |
| 191 |
(Some(_), Some(_)) => Access::ReadWrite, |
| 192 |
(Some(_), None) => Access::Read, |
| 193 |
(None, Some(_)) => Access::Write, |
| 194 |
_ => unimplemented!(), |
| 195 |
}; |
| 196 |
PropInfo { name, handlers: DebugProp(get, set), sig, auto_emit: true, rw: a, |
| 197 |
emits: EmitsChangedSignal::True, anns: Default::default() } |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
impl<'a, H: Handlers> IfaceInfo<'a, H> { |
| 202 |
pub fn new_empty(name: IfaceName<'static>) -> Self { |
| 203 |
IfaceInfo { name, methods: vec!(), props: vec!(), signals: vec!() } |
| 204 |
} |
| 205 |
|
| 206 |
pub fn new<N, M, P, S>(name: N, methods: M, properties: P, signals: S) -> Self where |
| 207 |
N: Into<IfaceName<'a>>, |
| 208 |
M: IntoIterator<Item=MethodInfo<'a, H>>, |
| 209 |
P: IntoIterator<Item=PropInfo<'a, H>>, |
| 210 |
S: IntoIterator<Item=SignalInfo<'a>> |
| 211 |
{ |
| 212 |
IfaceInfo { |
| 213 |
name: name.into(), |
| 214 |
methods: methods.into_iter().collect(), |
| 215 |
props: properties.into_iter().collect(), |
| 216 |
signals: signals.into_iter().collect() |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|