Skip to content

Commit 29adff9

Browse files
committed
Rustdoc examples for variables
Signed-off-by: itowlson <[email protected]>
1 parent f4f89b7 commit 29adff9

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ pub mod pg3;
105105

106106
pub mod mysql;
107107

108-
#[doc(inline)]
109-
/// Component configuration variables.
110-
pub use wit::v2::variables;
108+
pub mod variables;
111109

112110
#[doc(hidden)]
113111
pub use wit_bindgen;

src/variables.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! Component configuration variables.
2+
//!
3+
//! Component variables must be defined in the application
4+
//! manifest, in the `[component.<name>.variables]` section.
5+
//! Component variables typically use template syntax to
6+
//! derive values from application variables, which are
7+
//! the only variables that may be overridden directly (for
8+
//! example, on the Spin command line).
9+
//!
10+
//! # Examples
11+
//!
12+
//! Get the value of a component variable.
13+
//!
14+
//! ```no_run
15+
//! # fn main() -> anyhow::Result<()> {
16+
//! let region = spin_sdk::variables::get("region_id")?;
17+
//! let regional_url = format!("https://{region}.db.example.com");
18+
//! # Ok(())
19+
//! # }
20+
//! ```
21+
//!
22+
//! Fail gracefully if a variable is not set.
23+
//!
24+
//! ```no_run
25+
//! use spin_sdk::variables::Error;
26+
//!
27+
//! # fn main() -> anyhow::Result<()> {
28+
//! let favourite = match spin_sdk::variables::get("favourite") {
29+
//! Ok(value) => value,
30+
//! Err(Error::Undefined(_)) => "not playing favourites".to_owned(),
31+
//! Err(e) => anyhow::bail!(e),
32+
//! };
33+
//! # Ok(())
34+
//! # }
35+
//! ```
36+
37+
/// Get the value of a component variable.
38+
///
39+
/// # Examples
40+
///
41+
/// Get the value of a component variable.
42+
///
43+
/// ```no_run
44+
/// # fn main() -> anyhow::Result<()> {
45+
/// let region = spin_sdk::variables::get("region_id")?;
46+
/// let regional_url = format!("https://{region}.db.example.com");
47+
/// # Ok(())
48+
/// # }
49+
/// ```
50+
///
51+
/// Fail gracefully if a variable is not set.
52+
///
53+
/// ```no_run
54+
/// use spin_sdk::variables::Error;
55+
///
56+
/// # fn main() -> anyhow::Result<()> {
57+
/// let favourite = match spin_sdk::variables::get("favourite") {
58+
/// Ok(value) => value,
59+
/// Err(Error::Undefined(_)) => "not playing favourites".to_owned(),
60+
/// Err(e) => anyhow::bail!(e),
61+
/// };
62+
/// # Ok(())
63+
/// # }
64+
/// ```
65+
#[doc(inline)]
66+
pub use super::wit::v2::variables::get;
67+
68+
#[doc(inline)]
69+
pub use super::wit::v2::variables::Error;

0 commit comments

Comments
 (0)