Skip to main content

Router

Struct Router 

Source
pub struct Router { /* private fields */ }
Expand description

Method-name -> handler registry and JSON-RPC 2.0 dispatcher.

Why: the single seam between both transports and tcode’s business methods. See the module docs for the extensibility rationale. What: a thin HashMap<String, Arc<dyn MethodHandler>> plus dispatch. Test: router::tests::*.

Implementations§

Source§

impl Router

Source

pub fn new() -> Self

Construct an empty router.

Why: callers assemble a router by registering methods immediately after construction (see crate::serve::build_router). What: returns a Router with no registered methods. Test: dispatch_unknown_method_returns_method_not_found uses a router with exactly one registered method to prove unregistered names are still rejected.

Source

pub fn register( &mut self, method: impl Into<String>, handler: impl MethodHandler + 'static, ) -> &mut Self

Register a method handler under method.

Why: the sole way new JSON-RPC methods enter the dispatcher. Re-registering the same name replaces the previous handler (last write wins) rather than erroring, since duplicate registration is a programmer error caught by tests, not a runtime condition to guard. What: inserts handler into the registry; returns &mut Self so call sites can chain multiple .register(...) calls. Test: dispatch_registered_method_returns_handler_result.

Source

pub fn method_names(&self) -> Vec<&str>

Every registered method name, sorted.

Why (#6637): crate::serve::uds::JsonRpcFallback mounts this whole table behind one catch-all rather than re-registering each name, so nothing else could say what that catch-all actually covers. uds_tests::fallback_dispatches_every_jsonrpc_router_method reads the list from here and calls each name over a real socket, which is what turns “the fallback is mounted” into something a test can check. What: the keys of the handler map, sorted so a failure names the same method on every run. Test: uds_tests::fallback_dispatches_every_jsonrpc_router_method.

Source

pub async fn dispatch(&self, req: Request, ctx: &ConnectionContext) -> Response

Dispatch a parsed JSON-RPC request, returning the response to write back on the wire (or a suppressed response for notifications).

Why: the single entry point each transport calls per request. See the module docs for the full error-code contract. What: validates the envelope, looks up the handler, awaits it with ctx in hand, and maps the result onto a Response — suppressing the reply entirely when req.id was absent (a notification). Test: router::tests::* (one case per code path).

Source

pub async fn dispatch_json( &self, raw: &[u8], ctx: &ConnectionContext, ) -> Response

Parse raw as a single JSON-RPC request and dispatch it, mapping a JSON parse failure onto -32700 Parse error instead of propagating.

Why: both wire transports (STDIO lines, HTTP request bodies) receive raw bytes rather than an already-parsed Request; this is the one place “parse or -32700” is implemented, so the message/code can never drift between them. See the module docs for the full rationale. What: serde_json::from_slice::<Request>(raw) then Self::dispatch. On parse failure, returns Response::err(None, error_codes::PARSE_ERROR, ...) without ever invoking a handler — the request id is unrecoverable from malformed JSON, so the response carries a null id per the JSON-RPC 2.0 spec. Test: dispatch_json_valid_request_dispatches, dispatch_json_malformed_bytes_return_parse_error.

Trait Implementations§

Source§

impl Default for Router

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more