#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![warn(missing_docs)]
#![warn(noop_method_call)]
#![warn(unreachable_pub)]
#![warn(clippy::all)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::cargo_common_metadata)]
#![deny(clippy::cast_lossless)]
#![deny(clippy::checked_conversions)]
#![warn(clippy::cognitive_complexity)]
#![deny(clippy::debug_assert_with_mut_call)]
#![deny(clippy::exhaustive_enums)]
#![deny(clippy::exhaustive_structs)]
#![deny(clippy::expl_impl_clone_on_copy)]
#![deny(clippy::fallible_impl_from)]
#![deny(clippy::implicit_clone)]
#![deny(clippy::large_stack_arrays)]
#![warn(clippy::manual_ok_or)]
#![deny(clippy::missing_docs_in_private_items)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::needless_pass_by_value)]
#![warn(clippy::option_option)]
#![deny(clippy::print_stderr)]
#![deny(clippy::print_stdout)]
#![warn(clippy::rc_buffer)]
#![deny(clippy::ref_option_ref)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![warn(clippy::trait_duplication_in_bounds)]
#![deny(clippy::unchecked_time_subtraction)]
#![deny(clippy::unnecessary_wraps)]
#![warn(clippy::unseparated_literal_suffix)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::mod_module_files)]
#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] #![allow(clippy::needless_lifetimes)] #![allow(mismatched_lifetime_syntaxes)] #![allow(clippy::collapsible_if)] #![deny(clippy::unused_async)]
pub mod dispatch;
mod err;
mod method;
mod obj;
use std::{collections::HashSet, convert::Infallible, sync::Arc};
pub use dispatch::{DispatchTable, InvokeError, UpdateSink};
pub use err::{RpcError, RpcErrorKind};
pub use method::{
DeserMethod, DynMethod, Method, NoUpdates, RpcMethod, check_method_names, is_method_name,
iter_method_names,
};
pub use obj::{Object, ObjectArcExt, ObjectId};
#[cfg(feature = "describe-methods")]
pub use dispatch::description::RpcDispatchInformation;
#[cfg(feature = "describe-methods")]
#[doc(hidden)]
pub use dispatch::description::DelegationNote;
#[doc(hidden)]
pub use obj::cast::CastTable;
#[doc(hidden)]
pub use {
derive_deftly, dispatch::RpcResult, downcast_rs, erased_serde, futures, inventory,
method::MethodInfo_, paste, tor_async_utils, tor_error::internal, typetag,
};
pub mod templates {
pub use crate::method::derive_deftly_template_DynMethod;
pub use crate::obj::derive_deftly_template_Object;
}
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum LookupError {
#[error("No visible object with ID {0:?}")]
NoObject(ObjectId),
#[error("Unexpected type on object with ID {0:?}")]
WrongType(ObjectId),
}
impl From<LookupError> for RpcError {
fn from(err: LookupError) -> Self {
use LookupError as E;
use RpcErrorKind as EK;
let kind = match &err {
E::NoObject(_) => EK::ObjectNotFound,
E::WrongType(_) => EK::InvalidRequest,
};
RpcError::new(err.to_string(), kind)
}
}
pub trait Context: Send + Sync {
fn lookup_object(&self, id: &ObjectId) -> Result<Arc<dyn Object>, LookupError>;
fn register_owned(&self, object: Arc<dyn Object>) -> ObjectId;
fn release_owned(&self, object: &ObjectId) -> Result<(), LookupError>;
fn dispatch_table(&self) -> &Arc<std::sync::RwLock<DispatchTable>>;
}
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum SendUpdateError {
#[error("Unable to send on MPSC connection")]
ConnectionClosed,
}
impl tor_error::HasKind for SendUpdateError {
fn kind(&self) -> tor_error::ErrorKind {
tor_error::ErrorKind::Internal
}
}
impl From<Infallible> for SendUpdateError {
fn from(_: Infallible) -> Self {
unreachable!()
}
}
impl From<futures::channel::mpsc::SendError> for SendUpdateError {
fn from(_: futures::channel::mpsc::SendError) -> Self {
SendUpdateError::ConnectionClosed
}
}
pub trait ContextExt: Context {
fn lookup<T: Object>(&self, id: &ObjectId) -> Result<Arc<T>, LookupError> {
self.lookup_object(id)?
.downcast_arc()
.map_err(|_| LookupError::WrongType(id.clone()))
}
}
impl<T: Context> ContextExt for T {}
pub fn invoke_rpc_method(
ctx: Arc<dyn Context>,
obj_id: &ObjectId,
obj: Arc<dyn Object>,
method: Box<dyn DynMethod>,
sink: dispatch::BoxedUpdateSink,
) -> Result<dispatch::RpcResultFuture, InvokeError> {
match method.invoke_without_dispatch(Arc::clone(&ctx), obj_id) {
Err(InvokeError::NoDispatchBypass) => {
}
other => return other,
}
let (obj, invocable) = ctx
.dispatch_table()
.read()
.expect("poisoned lock")
.resolve_rpc_invoker(obj, method.as_ref())?;
invocable.invoke(obj, method, ctx, sink)
}
pub async fn invoke_special_method<M: Method>(
ctx: Arc<dyn Context>,
obj: Arc<dyn Object>,
method: Box<M>,
) -> Result<Box<M::Output>, InvokeError> {
let (obj, invocable) = ctx
.dispatch_table()
.read()
.expect("poisoned lock")
.resolve_special_invoker::<M>(obj)?;
invocable
.invoke_special(obj, method, ctx)?
.await
.downcast()
.map_err(|_| InvokeError::Bug(tor_error::internal!("Downcast to wrong type")))
}
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, Default)]
#[non_exhaustive]
pub struct Nil {}
pub const NIL: Nil = Nil {};
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, derive_more::From)]
pub struct SingleIdResponse {
id: ObjectId,
}
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
#[cfg_attr(test, derive(Eq, PartialEq))]
pub enum InvalidRpcIdentifier {
#[error("Identifier has no namespace separator")]
NoNamespace,
#[error("Identifier has unrecognized namespace")]
UnrecognizedNamespace,
#[error("Identifier name has unexpected format")]
BadIdName,
}
pub(crate) fn is_valid_rpc_identifier(
recognized_namespaces: Option<&HashSet<&str>>,
method: &str,
) -> Result<(), InvalidRpcIdentifier> {
fn name_ok(n: &str) -> bool {
let mut chars = n.chars();
let Some(first) = chars.next() else {
return false;
};
first.is_ascii_lowercase()
&& chars.all(|ch| ch.is_ascii_lowercase() || ch.is_ascii_digit() || ch == '_')
}
let (scope, name) = method
.split_once(':')
.ok_or(InvalidRpcIdentifier::NoNamespace)?;
if let Some(recognized_namespaces) = recognized_namespaces {
if !(scope.starts_with("x-") || recognized_namespaces.contains(scope)) {
return Err(InvalidRpcIdentifier::UnrecognizedNamespace);
}
}
if !name_ok(name) {
return Err(InvalidRpcIdentifier::BadIdName);
}
Ok(())
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_time_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
use futures::SinkExt as _;
use futures_await_test::async_test;
use super::*;
use crate::dispatch::test::{Ctx, GetKids, Swan};
#[async_test]
async fn invoke() {
let ctx = Arc::new(Ctx::from(DispatchTable::from_inventory()));
let discard = || Box::pin(futures::sink::drain().sink_err_into());
let r = invoke_rpc_method(
ctx.clone(),
&ObjectId::from("Odile"),
Arc::new(Swan),
Box::new(GetKids),
discard(),
)
.unwrap()
.await
.unwrap();
assert_eq!(serde_json::to_string(&r).unwrap(), r#"{"v":"cygnets"}"#);
let r = invoke_special_method(ctx, Arc::new(Swan), Box::new(GetKids))
.await
.unwrap()
.unwrap();
assert_eq!(r.v, "cygnets");
}
#[test]
fn valid_method_names() {
let namespaces: HashSet<_> = ["arti", "wombat"].into_iter().collect();
for name in [
"arti:clone",
"arti:clone7",
"arti:clone_now",
"wombat:knish",
"x-foo:bar",
] {
assert!(is_valid_rpc_identifier(Some(&namespaces), name).is_ok());
}
}
#[test]
fn invalid_method_names() {
let namespaces: HashSet<_> = ["arti", "wombat"].into_iter().collect();
use InvalidRpcIdentifier as E;
for (name, expect_err) in [
("arti-foo:clone", E::UnrecognizedNamespace),
("fred", E::NoNamespace),
("arti:", E::BadIdName),
("arti:7clone", E::BadIdName),
("arti:CLONE", E::BadIdName),
("arti:clone-now", E::BadIdName),
] {
assert_eq!(
is_valid_rpc_identifier(Some(&namespaces), name),
Err(expect_err)
);
}
}
}