Skip to content

Commit 1aae580

Browse files
committed
Add examples to rustdoc
Signed-off-by: itowlson <[email protected]>
1 parent 6ee0933 commit 1aae580

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ jobs:
3939
shell: bash
4040
run: cargo test --workspace
4141

42+
- name: Validate docs examples
43+
shell: bash
44+
run: cargo test --doc

src/http.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,59 @@ impl ResponseOutparam {
613613
}
614614

615615
/// Send an outgoing request
616+
///
617+
/// # Examples
618+
///
619+
/// Get the example.com home page:
620+
///
621+
/// ```no_run
622+
/// use spin_sdk::http::{Request, Response};
623+
///
624+
/// # #[tokio::main]
625+
/// # async fn main() -> anyhow::Result<()> {
626+
/// let request = Request::get("example.com").build();
627+
/// let response: Response = spin_sdk::http::send(request).await?;
628+
/// println!("{}", response.body().len());
629+
/// # Ok(())
630+
/// # }
631+
/// ```
632+
///
633+
/// Use the `http` crate Request type to send a data transfer value:
634+
///
635+
/// ```no_run
636+
/// use hyperium::Request;
637+
///
638+
/// #[derive(serde::Serialize)]
639+
/// struct User {
640+
/// name: String,
641+
/// }
642+
///
643+
/// impl spin_sdk::http::conversions::TryIntoBody for User {
644+
/// type Error = serde_json::Error;
645+
///
646+
/// fn try_into_body(self) -> Result<Vec<u8>, Self::Error> {
647+
/// serde_json::to_vec(&self)
648+
/// }
649+
/// }
650+
///
651+
/// # #[tokio::main]
652+
/// # async fn main() -> anyhow::Result<()> {
653+
/// let user = User {
654+
/// name: "Alice".to_owned(),
655+
/// };
656+
///
657+
/// let request = hyperium::Request::builder()
658+
/// .method("POST")
659+
/// .uri("https://example.com/users")
660+
/// .header("content-type", "application/json")
661+
/// .body(user)?;
662+
///
663+
/// let response: hyperium::Response<()> = spin_sdk::http::send(request).await?;
664+
///
665+
/// println!("{}", response.status().is_success());
666+
/// # Ok(())
667+
/// # }
668+
/// ```
616669
pub async fn send<I, O>(request: I) -> Result<O, SendError>
617670
where
618671
I: TryIntoOutgoingRequest,

src/key_value.rs

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,73 @@
33
//! This module provides a generic interface for key-value storage, which may be implemented by the host various
44
//! ways (e.g. via an in-memory table, a local file, or a remote database). Details such as consistency model and
55
//! durability will depend on the implementation and may vary from one to store to the next.
6+
//!
7+
//! # Examples
8+
//!
9+
//! Open the default store and set the 'message' key:
10+
//!
11+
//! ```no_run
12+
//! # fn main() -> anyhow::Result<()> {
13+
//! let store = spin_sdk::key_value::Store::open_default()?;
14+
//! store.set("message", "Hello world".as_bytes())?;
15+
//! # Ok(())
16+
//! # }
17+
//! ```
618
719
use super::wit::v2::key_value;
820

921
#[cfg(feature = "json")]
1022
use serde::{de::DeserializeOwned, Serialize};
1123

1224
#[doc(inline)]
13-
pub use key_value::{Error, Store};
25+
pub use key_value::Error;
26+
27+
/// An open key-value store.
28+
///
29+
/// # Examples
30+
///
31+
/// Open the default store and set the 'message' key:
32+
///
33+
/// ```no_run
34+
/// # fn main() -> anyhow::Result<()> {
35+
/// let store = spin_sdk::key_value::Store::open_default()?;
36+
/// store.set("message", "Hello world".as_bytes())?;
37+
/// # Ok(())
38+
/// # }
39+
/// ```
40+
///
41+
/// Open the default store and get the 'message' key:
42+
///
43+
/// ```no_run
44+
/// # fn main() -> anyhow::Result<()> {
45+
/// let store = spin_sdk::key_value::Store::open_default()?;
46+
/// let message = store.gettt("message")?;
47+
/// let response = message.unwrap_or_else(|| "not found".into());
48+
/// # Ok(())
49+
/// # }
50+
/// ```
51+
///
52+
/// Open a named store and list all the keys defined in it:
53+
///
54+
/// ```no_run
55+
/// # fn main() -> anyhow::Result<()> {
56+
/// let store = spin_sdk::key_value::Store::open("finance")?;
57+
/// let keys = store.get_keys()?;
58+
/// # Ok(())
59+
/// # }
60+
/// ```
61+
///
62+
/// Open the default store and delete the 'message' key:
63+
///
64+
/// ```no_run
65+
/// # fn main() -> anyhow::Result<()> {
66+
/// let store = spin_sdk::key_value::Store::open_default()?;
67+
/// store.delete("message")?;
68+
/// # Ok(())
69+
/// # }
70+
/// ```
71+
#[doc(inline)]
72+
pub use key_value::Store;
1473

1574
impl Store {
1675
/// Open the default store.
@@ -24,6 +83,31 @@ impl Store {
2483
impl Store {
2584
#[cfg(feature = "json")]
2685
/// Serialize the given data to JSON, then set it as the value for the specified `key`.
86+
///
87+
/// # Examples
88+
///
89+
/// Open the default store and save a customer information document against the customer ID:
90+
///
91+
/// ```no_run
92+
/// # use serde::{Deserialize, Serialize};
93+
/// #[derive(Deserialize, Serialize)]
94+
/// struct Customer {
95+
/// name: String,
96+
/// address: Vec<String>,
97+
/// }
98+
///
99+
/// # fn main() -> anyhow::Result<()> {
100+
/// let customer_id = "CR1234567";
101+
/// let customer = Customer {
102+
/// name: "Alice".to_owned(),
103+
/// address: vec!["Wonderland Way".to_owned()],
104+
/// };
105+
///
106+
/// let store = spin_sdk::key_value::Store::open_default()?;
107+
/// store.set_json(customer_id, &customer)?;
108+
/// # Ok(())
109+
/// # }
110+
/// ```
27111
pub fn set_json<T: Serialize>(
28112
&self,
29113
key: impl AsRef<str>,
@@ -34,6 +118,27 @@ impl Store {
34118

35119
#[cfg(feature = "json")]
36120
/// Deserialize an instance of type `T` from the value of `key`.
121+
///
122+
/// # Examples
123+
///
124+
/// Open the default store and retrieve a customer information document by customer ID:
125+
///
126+
/// ```no_run
127+
/// # use serde::{Deserialize, Serialize};
128+
/// #[derive(Deserialize, Serialize)]
129+
/// struct Customer {
130+
/// name: String,
131+
/// address: Vec<String>,
132+
/// }
133+
///
134+
/// # fn main() -> anyhow::Result<()> {
135+
/// let customer_id = "CR1234567";
136+
///
137+
/// let store = spin_sdk::key_value::Store::open_default()?;
138+
/// let customer = store.get_json::<Customer>(customer_id)?;
139+
/// # Ok(())
140+
/// # }
141+
/// ```
37142
pub fn get_json<T: DeserializeOwned>(
38143
&self,
39144
key: impl AsRef<str>,

0 commit comments

Comments
 (0)