Skip to content

Commit 579e8ee

Browse files
committed
docs(sacp): add guidance on when to use MatchMessage vs MatchMessageFrom
- Add module-level 'When to use which' section - Recommend MatchMessageFrom over JrMessageHandler implementations - Clarify MatchMessage is for unwrapped messages or inside callbacks
1 parent bd123b2 commit 579e8ee

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/sacp/src/session.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ where
135135
#[non_exhaustive]
136136
pub enum SessionMessage {
137137
/// Periodic updates with new content, tool requests, etc.
138+
/// Use [`MatchMessage`] to match on the message type.
138139
SessionMessage(MessageCx),
139140

140141
/// When a prompt completes, the stop reason.

src/sacp/src/util/typed.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
//! When handling [`UntypedMessage`]s, you can use [`MatchMessage`] for simple parsing
44
//! or [`MatchMessageFrom`] when you need role-aware endpoint transforms (e.g., unwrapping
55
//! proxy envelopes).
6+
//!
7+
//! # When to use which
8+
//!
9+
//! - **[`MatchMessageFrom`]**: Preferred over implementing [`JrMessageHandler`] directly.
10+
//! Use this in connection handlers when you need to match on message types with
11+
//! proper endpoint-aware transforms (e.g., unwrapping `SuccessorMessage` envelopes).
12+
//!
13+
//! - **[`MatchMessage`]**: Use this when you already have an unwrapped message and
14+
//! just need to parse it, such as inside a [`MatchMessageFrom`] callback or when
15+
//! processing messages that don't need endpoint transforms.
16+
//!
17+
//! [`JrMessageHandler`]: crate::JrMessageHandler
618
719
// Types re-exported from crate root
820
use jsonrpcmsg::Params;
@@ -16,9 +28,12 @@ use crate::{
1628

1729
/// Role-agnostic helper for pattern-matching on untyped JSON-RPC messages.
1830
///
19-
/// Use this when you just need to parse messages as specific types without
20-
/// any endpoint-aware transforms. For role-aware matching that handles
21-
/// proxy envelope unwrapping, use [`MatchMessageFrom`] instead.
31+
/// Use this when you already have an unwrapped message and just need to parse it,
32+
/// such as inside a [`MatchMessageFrom`] callback or when processing messages
33+
/// that don't need endpoint transforms.
34+
///
35+
/// For connection handlers where you need proper endpoint-aware transforms,
36+
/// use [`MatchMessageFrom`] instead.
2237
///
2338
/// # Example
2439
///
@@ -249,13 +264,19 @@ impl MatchMessage {
249264

250265
/// Role-aware helper for pattern-matching on untyped JSON-RPC requests.
251266
///
267+
/// **Prefer this over implementing [`JrMessageHandler`] directly.** This provides
268+
/// a more ergonomic API for matching on message types in connection handlers.
269+
///
252270
/// Use this when you need endpoint-aware transforms (e.g., unwrapping proxy envelopes)
253-
/// before parsing messages. For simple parsing without role awareness, use [`MatchMessage`].
271+
/// before parsing messages. For simple parsing without role awareness (e.g., inside
272+
/// a callback), use [`MatchMessage`] instead.
254273
///
255274
/// This wraps [`MatchMessage`] and applies endpoint-specific message transformations
256275
/// via `remote_style().handle_incoming_message()` before delegating to `MatchMessage`
257276
/// for the actual parsing.
258277
///
278+
/// [`JrMessageHandler`]: crate::JrMessageHandler
279+
///
259280
/// # Example
260281
///
261282
/// ```

src/yopo/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use sacp::schema::{
88
RequestPermissionOutcome, RequestPermissionRequest, RequestPermissionResponse,
99
SessionNotification, TextContent, VERSION as PROTOCOL_VERSION,
1010
};
11-
use sacp::util::MatchMessageFrom;
11+
use sacp::util::{MatchMessage, MatchMessageFrom};
1212
use sacp::{Component, Handled, MessageCx, UntypedMessage};
1313
use std::path::PathBuf;
1414

@@ -120,7 +120,7 @@ pub async fn prompt_with_callback(
120120
let update = session.read_update().await?;
121121
match update {
122122
sacp::SessionMessage::SessionMessage(message) => {
123-
MatchMessageFrom::new(message, &cx)
123+
MatchMessage::new(message)
124124
.if_notification(async |notification: SessionNotification| {
125125
tracing::debug!(
126126
?notification,

0 commit comments

Comments
 (0)