use std::collections::{BTreeMap, BTreeSet};
use serde::Serialize;
use crate::{MethodInfo_, NoUpdates, method::method_info_by_typeid};
#[derive(Serialize, Debug, Clone)]
struct MethodDescription {
method_type: String,
output_type: String,
update_type: Option<String>,
applies_to_object_types: BTreeSet<String>,
}
#[derive(Serialize, Debug, Clone)]
pub struct RpcDispatchInformation {
methods: BTreeMap<String, MethodDescription>,
delegations: BTreeMap<String, BTreeSet<String>>,
}
impl super::DispatchTable {
pub fn dispatch_information(&self) -> RpcDispatchInformation {
let mut methods = BTreeMap::new();
for invoker_ent in self.map.values() {
let Some(method_info) = method_info_by_typeid(invoker_ent.invoker.method_type()) else {
continue; };
let rpc_method_name = method_info.method_name.to_owned();
let (object_type_name, method_type_name) =
invoker_ent.invoker.object_and_method_type_names();
let description = methods
.entry(rpc_method_name)
.or_insert_with(|| MethodDescription::new(method_type_name, method_info));
description.push_object_type(object_type_name);
}
let mut delegations = BTreeMap::new();
for note in inventory::iter::<DelegationNote>() {
let set = delegations
.entry((note.from_type_name)().to_string())
.or_insert_with(BTreeSet::new);
set.insert((note.to_type_name)().to_string());
}
RpcDispatchInformation {
methods,
delegations,
}
}
}
impl MethodDescription {
fn new(method_type_name: &str, info: &MethodInfo_) -> Self {
let method_type_name = method_type_name.to_owned();
let output_type_name = (info.output_name)().to_owned();
let update_type_name = {
let name = (info.update_name)();
if name == std::any::type_name::<NoUpdates>() {
None
} else {
Some(name.to_owned())
}
};
MethodDescription {
method_type: method_type_name,
output_type: output_type_name,
update_type: update_type_name,
applies_to_object_types: Default::default(),
}
}
fn push_object_type(&mut self, object_type_name: &str) {
self.applies_to_object_types
.insert(object_type_name.to_owned());
}
}
#[derive(Debug)]
#[doc(hidden)]
#[allow(clippy::exhaustive_structs)]
pub struct DelegationNote {
pub from_type_name: fn() -> &'static str,
pub to_type_name: fn() -> &'static str,
}
inventory::collect!(DelegationNote);
#[macro_export]
#[doc(hidden)]
macro_rules! register_delegation_note {
{ $from_type:ty, $to_type:ty } => {
$crate::inventory::submit!{$crate::DelegationNote {
from_type_name: std::any::type_name::<$from_type>,
to_type_name: std::any::type_name::<$to_type>,
}}
}
}