Skip to content

Commit 85919d5

Browse files
committed
docs: add derive macro examples to JrRequest, JrNotification, JrResponsePayload traits
1 parent a608458 commit 85919d5

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/sacp/src/jsonrpc.rs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl<H: JrMessageHandler> JrConnectionBuilder<H> {
580580
///
581581
/// ```ignore
582582
/// # use sacp::role::UntypedRole;
583-
/// # use sacp::{JrConnectionBuilder};
583+
/// # use sacp::{JrConnectionBuilder};
584584
/// # use sacp::schema::{PromptRequest, PromptResponse, SessionNotification};
585585
/// # fn example(connection: JrConnectionBuilder<impl sacp::JrMessageHandler<Role = UntypedRole>>) {
586586
/// connection.on_receive_request(async |request: PromptRequest, request_cx, cx| {
@@ -990,7 +990,7 @@ impl<H: JrMessageHandler> JrConnection<H> {
990990
///
991991
/// ```no_run
992992
/// # use sacp::role::UntypedRole;
993-
/// # use sacp::{JrConnectionBuilder};
993+
/// # use sacp::{JrConnectionBuilder};
994994
/// # use sacp::ByteStreams;
995995
/// # use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};
996996
/// # use sacp_test::*;
@@ -1031,7 +1031,7 @@ impl<H: JrMessageHandler> JrConnection<H> {
10311031
///
10321032
/// ```no_run
10331033
/// # use sacp::role::UntypedRole;
1034-
/// # use sacp::{JrConnectionBuilder};
1034+
/// # use sacp::{JrConnectionBuilder};
10351035
/// # use sacp::ByteStreams;
10361036
/// # use sacp::schema::InitializeRequest;
10371037
/// # use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};
@@ -1333,7 +1333,7 @@ impl<Role: JrRole> JrConnectionCx<Role> {
13331333
///
13341334
/// ```
13351335
/// # use sacp::role::UntypedRole;
1336-
/// # use sacp::{JrConnectionBuilder, JrConnectionCx};
1336+
/// # use sacp::{JrConnectionBuilder, JrConnectionCx};
13371337
/// # use sacp_test::*;
13381338
/// # async fn example(cx: JrConnectionCx<UntypedRole>) -> Result<(), sacp::Error> {
13391339
/// // Set up a backend connection
@@ -1798,6 +1798,12 @@ impl<T: JrResponsePayload> JrRequestCx<T> {
17981798
}
17991799

18001800
/// Common bounds for any JSON-RPC message.
1801+
///
1802+
/// # Derive Macro
1803+
///
1804+
/// For simple message types, you can use the `JrRequest` or `JrNotification` derive macros
1805+
/// which will implement both `JrMessage` and the respective trait. See [`JrRequest`] and
1806+
/// [`JrNotification`] for examples.
18011807
pub trait JrMessage: 'static + Debug + Sized + Send + Clone {
18021808
/// The method name for the message.
18031809
fn method(&self) -> &str;
@@ -1815,6 +1821,20 @@ pub trait JrMessage: 'static + Debug + Sized + Send + Clone {
18151821
}
18161822

18171823
/// Defines the "payload" of a successful response to a JSON-RPC request.
1824+
///
1825+
/// # Derive Macro
1826+
///
1827+
/// Use `#[derive(JrResponsePayload)]` to automatically implement this trait:
1828+
///
1829+
/// ```ignore
1830+
/// use sacp::JrResponsePayload;
1831+
/// use serde::{Serialize, Deserialize};
1832+
///
1833+
/// #[derive(Debug, Serialize, Deserialize, JrResponsePayload)]
1834+
/// struct HelloResponse {
1835+
/// greeting: String,
1836+
/// }
1837+
/// ```
18181838
pub trait JrResponsePayload: 'static + Debug + Sized + Send {
18191839
/// Convert this message into a JSON value.
18201840
fn into_json(self, method: &str) -> Result<serde_json::Value, crate::Error>;
@@ -1834,9 +1854,44 @@ impl JrResponsePayload for serde_json::Value {
18341854
}
18351855

18361856
/// A struct that represents a notification (JSON-RPC message that does not expect a response).
1857+
///
1858+
/// # Derive Macro
1859+
///
1860+
/// Use `#[derive(JrNotification)]` to automatically implement both `JrMessage` and `JrNotification`:
1861+
///
1862+
/// ```ignore
1863+
/// use sacp::JrNotification;
1864+
/// use serde::{Serialize, Deserialize};
1865+
///
1866+
/// #[derive(Debug, Clone, Serialize, Deserialize, JrNotification)]
1867+
/// #[notification(method = "_ping")]
1868+
/// struct PingNotification {
1869+
/// timestamp: u64,
1870+
/// }
1871+
/// ```
18371872
pub trait JrNotification: JrMessage {}
18381873

18391874
/// A struct that represents a request (JSON-RPC message expecting a response).
1875+
///
1876+
/// # Derive Macro
1877+
///
1878+
/// Use `#[derive(JrRequest)]` to automatically implement both `JrMessage` and `JrRequest`:
1879+
///
1880+
/// ```ignore
1881+
/// use sacp::{JrRequest, JrResponsePayload};
1882+
/// use serde::{Serialize, Deserialize};
1883+
///
1884+
/// #[derive(Debug, Clone, Serialize, Deserialize, JrRequest)]
1885+
/// #[request(method = "_hello", response = HelloResponse)]
1886+
/// struct HelloRequest {
1887+
/// name: String,
1888+
/// }
1889+
///
1890+
/// #[derive(Debug, Serialize, Deserialize, JrResponsePayload)]
1891+
/// struct HelloResponse {
1892+
/// greeting: String,
1893+
/// }
1894+
/// ```
18401895
pub trait JrRequest: JrMessage {
18411896
/// The type of data expected in response.
18421897
type Response: JrResponsePayload;
@@ -2145,7 +2200,7 @@ impl<T: JrResponsePayload> JrResponse<T> {
21452200
///
21462201
/// ```
21472202
/// # use sacp::role::UntypedRole;
2148-
/// # use sacp::{JrConnectionBuilder, JrConnectionCx};
2203+
/// # use sacp::{JrConnectionBuilder, JrConnectionCx};
21492204
/// # use sacp_test::*;
21502205
/// # async fn example(cx: JrConnectionCx<UntypedRole>) -> Result<(), sacp::Error> {
21512206
/// // Set up backend connection

src/sacp/src/schema/proxy_protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! These types are intended to become part of the ACP protocol specification.
44
5-
use crate::{JrMessage, JrNotification, JrRequest, JrResponsePayload, UntypedMessage};
5+
use crate::{JrMessage, JrNotification, JrRequest, UntypedMessage};
66
use agent_client_protocol_schema::InitializeResponse;
77
use serde::{Deserialize, Serialize};
88

0 commit comments

Comments
 (0)