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
impl Router
Sourcepub fn new() -> Self
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.
Sourcepub fn register(
&mut self,
method: impl Into<String>,
handler: impl MethodHandler + 'static,
) -> &mut Self
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.
Sourcepub async fn dispatch(&self, req: Request, ctx: &ConnectionContext) -> Response
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).
Sourcepub async fn dispatch_json(
&self,
raw: &[u8],
ctx: &ConnectionContext,
) -> Response
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§
Auto Trait Implementations§
impl !RefUnwindSafe for Router
impl !UnwindSafe for Router
impl Freeze for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl UnsafeUnpin for Router
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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