| 1 |
use std::{fmt, cell}; |
| 2 |
use std::any::Any; |
| 3 |
use crate::arg::ArgBuilder; |
| 4 |
use crate::{Path as PathName, Interface as IfaceName, Member as MemberName, Signature, Message, arg}; |
| 5 |
use super::crossroads::{Crossroads, PathData, MLookup}; |
| 6 |
use super::info::{MethodInfo, PropInfo}; |
| 7 |
use super::MethodErr; |
| 8 |
|
| 9 |
pub struct DebugMethod<H: Handlers>(pub H::Method); |
| 10 |
impl<H: Handlers> fmt::Debug for DebugMethod<H> { |
| 11 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "...") } |
| 12 |
} |
| 13 |
|
| 14 |
pub struct DebugProp<H: Handlers>(pub Option<H::GetProp>, pub Option<H::SetProp>); |
| 15 |
impl<H: Handlers> fmt::Debug for DebugProp<H> { |
| 16 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "...") } |
| 17 |
} |
| 18 |
|
| 19 |
pub trait Handlers { |
| 20 |
type Method; |
| 21 |
type GetProp; |
| 22 |
type SetProp; |
| 23 |
type Iface; |
| 24 |
} |
| 25 |
|
| 26 |
/// Parallel tree - Par |
| 27 |
#[derive(Debug, Clone, Copy, Default)] |
| 28 |
pub struct Par; |
| 29 |
|
| 30 |
impl Par { |
| 31 |
pub fn typed_getprop<I: 'static, T: arg::Arg + arg::Append, G>(getf: G) -> <Par as Handlers>::GetProp |
| 32 |
where G: Fn(&I, &ParInfo) -> Result<T, MethodErr> + Send + Sync + 'static { |
| 33 |
Box::new(move |data, ia, info| { |
| 34 |
let iface: &I = data.downcast_ref().unwrap(); |
| 35 |
let t = getf(iface, info)?; |
| 36 |
ia.append(t); |
| 37 |
Ok(()) |
| 38 |
}) |
| 39 |
} |
| 40 |
|
| 41 |
pub fn typed_setprop<I: 'static, T: arg::Arg + for <'z> arg::Get<'z>, S>(setf: S) -> <Par as Handlers>::SetProp |
| 42 |
where S: Fn(&I, &ParInfo, T) -> Result<(), MethodErr> + Send + Sync + 'static { |
| 43 |
Box::new(move |data, ii, info| { |
| 44 |
let iface: &I = data.downcast_ref().unwrap(); |
| 45 |
let t: T = ii.read()?; |
| 46 |
setf(iface, info, t) |
| 47 |
}) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[derive(Debug)] |
| 52 |
pub struct ParInfo<'a> { |
| 53 |
lookup: MLookup<'a, Par>, |
| 54 |
message: &'a Message, |
| 55 |
} |
| 56 |
|
| 57 |
impl<'a> ParInfo<'a> { |
| 58 |
pub fn msg(&self) -> &Message { self.message } |
| 59 |
pub (super) fn new(msg: &'a Message, lookup: MLookup<'a, Par>) -> Self { |
| 60 |
ParInfo { lookup, message: msg } |
| 61 |
} |
| 62 |
pub fn path_data(&self) -> &PathData<Par> { self.lookup.data } |
| 63 |
pub fn crossroads(&self) -> &Crossroads<Par> { self.lookup.cr } |
| 64 |
} |
| 65 |
|
| 66 |
impl Handlers for Par { |
| 67 |
type Method = Box<Fn(&(dyn Any + Send + Sync), &ParInfo) -> Option<Message> + Send + Sync + 'static>; |
| 68 |
type GetProp = Box<Fn(&(dyn Any + Send + Sync), &mut arg::IterAppend, &ParInfo) -> Result<(), MethodErr> + Send + Sync + 'static>; |
| 69 |
type SetProp = Box<Fn(&(dyn Any + Send + Sync), &mut arg::Iter, &ParInfo) -> Result<(), MethodErr> + Send + Sync + 'static>; |
| 70 |
type Iface = Box<dyn Any + 'static + Send + Sync>; |
| 71 |
} |
| 72 |
|
| 73 |
impl MethodInfo<'_, Par> { |
| 74 |
pub fn new_par<N, F, T>(name: N, f: F) -> Self where |
| 75 |
F: Fn(&T, &ParInfo) -> Result<Option<Message>, MethodErr> + Send + Sync + 'static, |
| 76 |
N: Into<MemberName<'static>>, |
| 77 |
T: Any + Send + Sync + 'static, |
| 78 |
{ |
| 79 |
Self::new(name.into(), Box::new(move |data, info| { |
| 80 |
let x = data.downcast_ref().unwrap(); |
| 81 |
f(x, info).unwrap_or_else(|e| { Some(e.to_message(info.message)) }) |
| 82 |
})) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/// Mutable, non-Send tree |
| 88 |
#[derive(Debug, Clone, Copy, Default)] |
| 89 |
pub struct Mut; |
| 90 |
|
| 91 |
#[derive(Debug)] |
| 92 |
pub struct MutCtx<'a> { |
| 93 |
message: &'a Message, |
| 94 |
send_extra: cell::RefCell<Vec<Message>>, |
| 95 |
} |
| 96 |
|
| 97 |
impl<'a> MutCtx<'a> { |
| 98 |
pub fn msg(&self) -> &Message { self.message } |
| 99 |
pub fn send(&self, msg: Message) { self.send_extra.borrow_mut().push(msg); } |
| 100 |
pub (super) fn new(msg: &'a Message) -> Self { MutCtx { message: msg, send_extra: Default::default() } } |
| 101 |
} |
| 102 |
|
| 103 |
impl Handlers for Mut { |
| 104 |
type Method = MutMethod; |
| 105 |
type GetProp = Box<FnMut(&mut (dyn Any), &mut arg::IterAppend, &MutCtx) -> Result<(), MethodErr> + 'static>; |
| 106 |
type SetProp = Box<FnMut(&mut (dyn Any), &mut arg::Iter, &MutCtx) -> Result<(), MethodErr> + 'static>; |
| 107 |
type Iface = Box<dyn Any>; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
pub struct MutMethod(pub (super) MutMethods); |
| 112 |
|
| 113 |
pub (super) enum MutMethods { |
| 114 |
MutIface(Box<FnMut(&mut (dyn Any), &MutCtx) -> Option<Message> + 'static>), |
| 115 |
// Info(Box<FnMut(&(dyn Any), &Message, &Path) -> Option<Message> + 'static>), |
| 116 |
// MutCr(fn(&mut Crossroads<Mut>, &Message) -> Vec<Message>), |
| 117 |
} |
| 118 |
|
| 119 |
impl Mut { |
| 120 |
pub fn typed_method_iface<IA: ArgBuilder, OA: ArgBuilder, I: 'static, F>(mut f: F) -> <Mut as Handlers>::Method |
| 121 |
where F: FnMut(&mut I, &MutCtx, IA) -> Result<OA, MethodErr> + 'static { |
| 122 |
MutMethod(MutMethods::MutIface(Box::new(move |data, info| { |
| 123 |
let iface: &mut I = data.downcast_mut().unwrap(); |
| 124 |
let ia = match IA::read(info.msg()) { |
| 125 |
Err(e) => return Some(MethodErr::from(e).to_message(info.msg())), |
| 126 |
Ok(ia) => ia, |
| 127 |
}; |
| 128 |
match f(iface, info, ia) { |
| 129 |
Err(e) => Some(e.to_message(info.msg())), |
| 130 |
Ok(r) => { |
| 131 |
let mut m = info.msg().method_return(); |
| 132 |
OA::append(r, &mut m); |
| 133 |
Some(m) |
| 134 |
}, |
| 135 |
} |
| 136 |
}))) |
| 137 |
} |
| 138 |
} |