Skip to content

Commit 67b2d3d

Browse files
Docs improvements (#24)
1 parent a40af63 commit 67b2d3d

File tree

4 files changed

+142
-6
lines changed

4 files changed

+142
-6
lines changed

macros/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use proc_macro::TokenStream;
2222
use quote::ToTokens;
2323
use syn::parse_macro_input;
2424

25-
/// Entry-point macro to define a Restate service.
2625
#[proc_macro_attribute]
2726
pub fn service(_: TokenStream, input: TokenStream) -> TokenStream {
2827
let svc = parse_macro_input!(input as Service);
@@ -32,7 +31,6 @@ pub fn service(_: TokenStream, input: TokenStream) -> TokenStream {
3231
.into()
3332
}
3433

35-
/// Entry-point macro to define a Restate object.
3634
#[proc_macro_attribute]
3735
pub fn object(_: TokenStream, input: TokenStream) -> TokenStream {
3836
let svc = parse_macro_input!(input as Object);
@@ -42,7 +40,6 @@ pub fn object(_: TokenStream, input: TokenStream) -> TokenStream {
4240
.into()
4341
}
4442

45-
/// Entry-point macro to define a Restate workflow.
4643
#[proc_macro_attribute]
4744
pub fn workflow(_: TokenStream, input: TokenStream) -> TokenStream {
4845
let svc = parse_macro_input!(input as Workflow);

src/context/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ where
2525
}
2626
}
2727

28-
/// Future created using [`super::ContextSideEffects::run`].
28+
/// Future created using [`ContextSideEffects::run`](super::ContextSideEffects::run).
2929
pub trait RunFuture<O>: Future<Output = O> {
3030
/// Provide a custom retry policy for this `run` operation.
3131
///

src/endpoint/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ impl Builder {
194194

195195
/// Add a [`Service`] to this endpoint.
196196
///
197-
/// When using the service/object/workflow macros, you need to pass the result of the `serve` method.
197+
/// When using the [`service`](macro@crate::service), [`object`](macro@crate::object) or [`workflow`](macro@crate::workflow) macros,
198+
/// you need to pass the result of the `serve` method.
198199
pub fn bind<
199200
S: Service<Future = BoxFuture<'static, Result<(), Error>>>
200201
+ Discoverable

src/lib.rs

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,145 @@ pub mod http_server;
4848
pub mod hyper;
4949
pub mod serde;
5050

51-
pub use restate_sdk_macros::{object, service, workflow};
51+
/// Entry-point macro to define a Restate [Service](https://docs.restate.dev/concepts/services#services-1).
52+
///
53+
/// ```rust,no_run
54+
/// use restate_sdk::prelude::*;
55+
///
56+
/// #[restate_sdk::service]
57+
/// trait Greeter {
58+
/// async fn greet(name: String) -> Result<String, HandlerError>;
59+
/// }
60+
/// ```
61+
///
62+
/// This macro accepts a `trait` as input, and generates as output:
63+
///
64+
/// * A trait with the same name, that you should implement on your own concrete type (e.g. `struct`), e.g.:
65+
///
66+
/// ```rust,no_run
67+
/// # use restate_sdk::prelude::*;
68+
/// # #[restate_sdk::service]
69+
/// # trait Greeter {
70+
/// # async fn greet(name: String) -> Result<String, HandlerError>;
71+
/// # }
72+
/// struct GreeterImpl;
73+
/// impl Greeter for GreeterImpl {
74+
/// async fn greet(&self, _: Context<'_>, name: String) -> Result<String, HandlerError> {
75+
/// Ok(format!("Greetings {name}"))
76+
/// }
77+
/// }
78+
/// ```
79+
///
80+
/// This trait will additionally contain, for each handler, the appropriate [`Context`](crate::prelude::Context), to interact with Restate.
81+
///
82+
/// * An implementation of the [`Service`](crate::service::Service) trait, to bind the service in the [`Endpoint`](crate::prelude::Endpoint) and expose it:
83+
///
84+
/// ```rust,no_run
85+
/// # use restate_sdk::prelude::*;
86+
/// # #[restate_sdk::service]
87+
/// # trait Greeter {
88+
/// # async fn greet(name: String) -> HandlerResult<String>;
89+
/// # }
90+
/// # struct GreeterImpl;
91+
/// # impl Greeter for GreeterImpl {
92+
/// # async fn greet(&self, _: Context<'_>, name: String) -> HandlerResult<String> {
93+
/// # Ok(format!("Greetings {name}"))
94+
/// # }
95+
/// # }
96+
/// let endpoint = Endpoint::builder()
97+
/// // .serve() returns the implementation of Service used by the SDK
98+
/// // to bind your struct to the endpoint
99+
/// .bind(GreeterImpl.serve())
100+
/// .build();
101+
/// ```
102+
///
103+
/// * A client implementation to call this service from another service, object or workflow, e.g.:
104+
///
105+
/// ```rust,no_run
106+
/// # use restate_sdk::prelude::*;
107+
/// # #[restate_sdk::service]
108+
/// # trait Greeter {
109+
/// # async fn greet(name: String) -> HandlerResult<String>;
110+
/// # }
111+
/// # async fn example(ctx: Context<'_>) -> Result<(), TerminalError> {
112+
/// let result = ctx
113+
/// .service_client::<GreeterClient>()
114+
/// .greet("My greetings".to_string())
115+
/// .call()
116+
/// .await?;
117+
/// # Ok(())
118+
/// # }
119+
/// ```
120+
///
121+
/// Methods of this trait can accept either no parameter, or one parameter implementing [`Deserialize`](crate::serde::Deserialize).
122+
/// The return value MUST always be a `Result`. Down the hood, the error type is always converted to [`HandlerError`](crate::prelude::HandlerError) for the SDK to distinguish between terminal and retryable errors. For more details, check the [`HandlerError`](crate::prelude::HandlerError) doc.
123+
///
124+
/// When invoking the service through Restate, the method name should be used as handler name, that is:
125+
///
126+
/// ```rust,no_run
127+
/// use restate_sdk::prelude::*;
128+
///
129+
/// #[restate_sdk::service]
130+
/// trait Greeter {
131+
/// async fn my_greet(name: String) -> Result<String, HandlerError>;
132+
/// }
133+
/// ```
134+
///
135+
/// The `Greeter/my_greet` handler be invoked sending a request to `http://<RESTATE_ENDPOINT>/Greeter/my_greet`.
136+
/// You can override the names used by Restate during registration using the `name` attribute:
137+
///
138+
/// ```rust,no_run
139+
/// use restate_sdk::prelude::*;
140+
///
141+
/// #[restate_sdk::service]
142+
/// #[name = "greeter"]
143+
/// trait Greeter {
144+
/// // You can invoke this handler with `http://<RESTATE_ENDPOINT>/greeter/myGreet`
145+
/// #[name = "myGreet"]
146+
/// async fn my_greet(name: String) -> Result<String, HandlerError>;
147+
/// }
148+
/// ```
149+
pub use restate_sdk_macros::service;
150+
151+
/// Entry-point macro to define a Restate [Virtual object](https://docs.restate.dev/concepts/services#virtual-objects).
152+
///
153+
/// For more details, check the [`service` macro](macro@crate::service) documentation.
154+
///
155+
/// ## Shared handlers
156+
///
157+
/// To define a shared handler, simply annotate the handler with the `#[shared]` annotation:
158+
///
159+
/// ```rust,no_run
160+
/// use restate_sdk::prelude::*;
161+
///
162+
/// #[restate_sdk::object]
163+
/// trait Counter {
164+
/// async fn add(val: u64) -> Result<u64, TerminalError>;
165+
/// #[shared]
166+
/// async fn get() -> Result<u64, TerminalError>;
167+
/// }
168+
/// ```
169+
pub use restate_sdk_macros::object;
170+
171+
/// Entry-point macro to define a Restate [Workflow](https://docs.restate.dev/concepts/services#workflows).
172+
///
173+
/// For more details, check the [`service` macro](macro@crate::service) documentation.
174+
///
175+
/// ## Shared handlers
176+
///
177+
/// To define a shared handler, simply annotate the handler with the `#[shared]` annotation:
178+
///
179+
/// ```rust,no_run
180+
/// use restate_sdk::prelude::*;
181+
///
182+
/// #[restate_sdk::workflow]
183+
/// trait Billing {
184+
/// async fn run() -> Result<u64, TerminalError>;
185+
/// #[shared]
186+
/// async fn get_status() -> Result<String, TerminalError>;
187+
/// }
188+
/// ```
189+
pub use restate_sdk_macros::workflow;
52190

53191
/// Prelude contains all the useful imports you need to get started with Restate.
54192
pub mod prelude {

0 commit comments

Comments
 (0)