Skip to content

Commit feb5fdb

Browse files
authored
Merge pull request #68 from itowlson/moar-examples-moar-moar-moar
Round out a few more rustdoc examples
2 parents c62b587 + 856aafc commit feb5fdb

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

crates/macro/src/lib.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,27 @@ use quote::quote;
33

44
const WIT_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/wit");
55

6-
/// Generates the entrypoint to a Spin Redis component written in Rust.
6+
/// The entrypoint to a Spin Redis component.
7+
///
8+
/// The component runs in response to messages on a Redis queue.
9+
///
10+
/// # Examples
11+
///
12+
/// A handler that logs the content of each message it receives.
13+
///
14+
/// ```ignore
15+
/// # use anyhow::Result;
16+
/// # use bytes::Bytes;
17+
/// # use spin_sdk::redis_component;
18+
/// # use std::str::from_utf8;
19+
/// #[redis_component]
20+
/// fn on_message(message: Bytes) -> Result<()> {
21+
/// println!("{}", from_utf8(&message)?);
22+
/// Ok(())
23+
/// }
24+
/// ```
25+
///
26+
/// See <https://spinframework.dev/redis-trigger> for more information.
727
#[proc_macro_attribute]
828
pub fn redis_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
929
let func = syn::parse_macro_input!(item as syn::ItemFn);
@@ -35,7 +55,10 @@ pub fn redis_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
3555
.into()
3656
}
3757

38-
/// The entrypoint to a WASI HTTP component written in Rust.
58+
/// The entrypoint to an HTTP component.
59+
///
60+
/// The component runs in response to inbound HTTP requests that match the component's
61+
/// trigger.
3962
///
4063
/// Functions annotated with this attribute can be of two forms:
4164
/// * Request/Response
@@ -85,6 +108,8 @@ pub fn redis_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
85108
/// // Your logic goes here
86109
/// }
87110
/// ```
111+
///
112+
/// See <https://spinframework.dev/http-trigger> for more information.
88113
#[proc_macro_attribute]
89114
pub fn http_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
90115
let func = syn::parse_macro_input!(item as syn::ItemFn);

src/http.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
//! Sending and receiving HTTP requests.
2+
//!
3+
//! This module includes:
4+
//!
5+
//! * Common HTTP types used in sending and receiving.
6+
//! * The [`send`] function for making HTTP requests.
7+
//! * The [`Router`] type for routing received HTTP requests.
8+
//! * The [`conversions`] and [`responses`] modules to make common cases easier.
9+
//!
10+
//! For writing a component which handles HTTP requests, see the
11+
//! [`http_component`](super::http_component) macro.
12+
113
/// Traits for converting between the various types
214
pub mod conversions;
315

@@ -1019,6 +1031,7 @@ impl ResponseOutparam {
10191031
/// # Ok(())
10201032
/// # }
10211033
/// ```
1034+
#[doc(alias = "fetch")]
10221035
pub async fn send<I, O>(request: I) -> Result<O, SendError>
10231036
where
10241037
I: TryIntoOutgoingRequest,

src/http/router.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,32 @@ fn trailing_suffix(req: &Request) -> Option<&str> {
591591
.and_then(|path_info| path_info.as_str())
592592
}
593593

594-
/// A macro to help with constructing a Router from a stream of tokens.
594+
/// A macro to help with constructing a [`Router`] from method-pattern-handler triples.
595+
///
596+
/// # Examples
597+
///
598+
/// ```no_run
599+
/// # use spin_sdk::http_router;
600+
/// # use spin_sdk::http::{IntoResponse, Params, Request, Response, Router};
601+
/// fn handle_route(req: Request) -> Response {
602+
/// let router = http_router! {
603+
/// GET "/user" => list_users,
604+
/// POST "/user" => create_user,
605+
/// GET "/user/:id" => get_user,
606+
/// PUT "/user/:id" => update_user,
607+
/// DELETE "/user/:id" => delete_user,
608+
/// _ "/user" => method_not_allowed,
609+
/// _ "/user/:id" => method_not_allowed
610+
/// };
611+
/// router.handle(req)
612+
/// }
613+
/// # fn list_users(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
614+
/// # fn create_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
615+
/// # fn get_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
616+
/// # fn update_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
617+
/// # fn delete_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
618+
/// # fn method_not_allowed(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
619+
/// ```
595620
#[macro_export]
596621
macro_rules! http_router {
597622
($($method:tt $path:literal => $h:expr),*) => {

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub mod sqlite;
1414
/// Large Language Model (Serverless AI) APIs
1515
pub mod llm;
1616

17-
/// Exports the procedural macros for writing handlers for Spin components.
1817
pub use spin_macro::*;
1918

2019
#[doc(hidden)]
@@ -54,7 +53,6 @@ extern "C" fn __spin_sdk_language() {}
5453
#[export_name = concat!("spin-sdk-commit-", env!("SDK_COMMIT"))]
5554
extern "C" fn __spin_sdk_hash() {}
5655

57-
/// Helpers for building Spin `wasi-http` components.
5856
pub mod http;
5957

6058
#[allow(missing_docs)]

0 commit comments

Comments
 (0)