Skip to content

Commit 3a8154d

Browse files
committed
Rustdoc examples for Redis
Signed-off-by: itowlson <[email protected]>
1 parent a97c3b5 commit 3a8154d

File tree

3 files changed

+117
-35
lines changed

3 files changed

+117
-35
lines changed

src/lib.rs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,41 +60,8 @@ pub mod http;
6060
#[allow(missing_docs)]
6161
pub mod mqtt;
6262

63-
/// Redis storage and messaging.
6463
#[allow(missing_docs)]
65-
pub mod redis {
66-
use std::hash::{Hash, Hasher};
67-
68-
pub use super::wit::v2::redis::{Connection, Error, Payload, RedisParameter, RedisResult};
69-
70-
impl PartialEq for RedisResult {
71-
fn eq(&self, other: &Self) -> bool {
72-
use RedisResult::*;
73-
match (self, other) {
74-
(Nil, Nil) => true,
75-
(Status(a), Status(b)) => a == b,
76-
(Int64(a), Int64(b)) => a == b,
77-
(Binary(a), Binary(b)) => a == b,
78-
_ => false,
79-
}
80-
}
81-
}
82-
83-
impl Eq for RedisResult {}
84-
85-
impl Hash for RedisResult {
86-
fn hash<H: Hasher>(&self, state: &mut H) {
87-
use RedisResult::*;
88-
89-
match self {
90-
Nil => (),
91-
Status(s) => s.hash(state),
92-
Int64(v) => v.hash(state),
93-
Binary(v) => v.hash(state),
94-
}
95-
}
96-
}
97-
}
64+
pub mod redis;
9865

9966
pub mod pg;
10067

src/redis.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//! Redis storage and message publishing.
2+
//!
3+
//! To receive Redis messages, use the Redis trigger.
4+
//!
5+
//! # Examples
6+
//!
7+
//! Get a value from the Redis database.
8+
//!
9+
//! ```no_run
10+
//! use spin_sdk::redis::Connection;
11+
//!
12+
//! # fn main() -> anyhow::Result<()> {
13+
//! let conn = Connection::open("redis://127.0.0.1:6379")?;
14+
//! let payload = conn.get("archimedes-data")?;
15+
//! if let Some(data) = payload {
16+
//! println!("{}", String::from_utf8_lossy(&data));
17+
//! }
18+
//! # Ok(())
19+
//! # }
20+
//! ```
21+
//!
22+
//! See the [`Connection`] type for further examples.
23+
24+
use std::hash::{Hash, Hasher};
25+
26+
/// An open connection to a Redis server.
27+
///
28+
/// # Examples
29+
///
30+
/// Get a value from the Redis database.
31+
///
32+
/// ```no_run
33+
/// use spin_sdk::redis::Connection;
34+
///
35+
/// # fn main() -> anyhow::Result<()> {
36+
/// let conn = Connection::open("redis://127.0.0.1:6379")?;
37+
/// let payload = conn.get("archimedes-data")?;
38+
/// if let Some(data) = payload {
39+
/// println!("{}", String::from_utf8_lossy(&data));
40+
/// }
41+
/// # Ok(())
42+
/// # }
43+
/// ```
44+
///
45+
/// Set a value in the Redis database.
46+
///
47+
/// ```no_run
48+
/// use spin_sdk::redis::Connection;
49+
///
50+
/// # fn main() -> anyhow::Result<()> {
51+
/// let conn = Connection::open("redis://127.0.0.1:6379")?;
52+
/// let payload = "Eureka!".to_owned().into_bytes();
53+
/// conn.set("archimedes-data", &payload)?;
54+
/// # Ok(())
55+
/// # }
56+
/// ```
57+
///
58+
/// Delete a value from the Redis database.
59+
///
60+
/// ```no_run
61+
/// use spin_sdk::redis::Connection;
62+
///
63+
/// # fn main() -> anyhow::Result<()> {
64+
/// let conn = Connection::open("redis://127.0.0.1:6379")?;
65+
/// conn.del(&["archimedes-data".to_owned()])?;
66+
/// # Ok(())
67+
/// # }
68+
/// ```
69+
///
70+
/// Publish a message to a Redis channel.
71+
///
72+
/// ```no_run
73+
/// use spin_sdk::redis::Connection;
74+
///
75+
/// # fn ensure_pet_picture(_: &[u8]) -> anyhow::Result<()> { Ok(()) }
76+
/// # fn use_redis(request: spin_sdk::http::Request) -> anyhow::Result<()> {
77+
/// let conn = Connection::open("redis://127.0.0.1:6379")?;
78+
///
79+
/// let payload = request.body().to_vec();
80+
/// ensure_pet_picture(&payload)?;
81+
///
82+
/// conn.publish("pet-pictures", &payload)?;
83+
/// # Ok(())
84+
/// # }
85+
/// ```
86+
pub use super::wit::v2::redis::Connection;
87+
88+
pub use super::wit::v2::redis::{Error, Payload, RedisParameter, RedisResult};
89+
90+
impl PartialEq for RedisResult {
91+
fn eq(&self, other: &Self) -> bool {
92+
use RedisResult::*;
93+
match (self, other) {
94+
(Nil, Nil) => true,
95+
(Status(a), Status(b)) => a == b,
96+
(Int64(a), Int64(b)) => a == b,
97+
(Binary(a), Binary(b)) => a == b,
98+
_ => false,
99+
}
100+
}
101+
}
102+
103+
impl Eq for RedisResult {}
104+
105+
impl Hash for RedisResult {
106+
fn hash<H: Hasher>(&self, state: &mut H) {
107+
use RedisResult::*;
108+
109+
match self {
110+
Nil => (),
111+
Status(s) => s.hash(state),
112+
Int64(v) => v.hash(state),
113+
Binary(v) => v.hash(state),
114+
}
115+
}
116+
}

src/sqlite.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ pub use sqlite::QueryResult;
178178
/// # Ok(())
179179
/// # }
180180
/// ```
181-
182181
#[doc(inline)]
183182
pub use sqlite::RowResult;
184183

0 commit comments

Comments
 (0)