Skip to content

Commit 2aaf520

Browse files
authored
docs: add examples to follow rust API guidelines
Make sure cursor-icon fulfills the C-EXAMPLE requirement for the most part.
1 parent 1f2511f commit 2aaf520

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ jobs:
4545
run: cargo test --verbose
4646

4747
- name: Run tests with no default features
48-
run: cargo test --verbose --no-default-features
48+
run: cargo test --verbose --no-default-features --tests

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ serde = { version = "1.0.162", default-features = false, features = ["derive"],
1818
default = ["std"]
1919
std = ["alloc"]
2020
alloc = []
21+
22+
[dev-dependencies]
23+
wayland-client = "0.30.1"
24+
wayland-cursor = "0.30.0"

src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
1212

1313
//! The cross platform cursor icon type.
14+
//!
15+
//! This type is intended to be used as a standard interopability type between
16+
//! GUI frameworks in order to convey the cursor icon type.
17+
//!
18+
//! # Example
19+
//!
20+
//! ```
21+
//! use cursor_icon::CursorIcon;
22+
//!
23+
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
24+
//! // Parse a cursor icon from the string that describes it.
25+
//! let cursor_name = "pointer";
26+
//! let cursor_icon: CursorIcon = cursor_name.parse()?;
27+
//! println!("The cursor icon is {:?}", cursor_icon);
28+
//! # Ok(())
29+
//! # }
30+
//! ```
1431
1532
// This file contains a portion of the CSS Basic User Interface Module Level 3
1633
// specification. In particular, the names for the cursor from the #cursor
@@ -94,6 +111,15 @@ extern crate alloc as _;
94111
///
95112
/// The names are taken from the CSS W3C specification:
96113
/// <https://www.w3.org/TR/css-ui-3/#cursor>
114+
///
115+
/// # Examples
116+
///
117+
/// ```
118+
/// use cursor_icon::CursorIcon;
119+
///
120+
/// // Get the cursor icon for the default cursor.
121+
/// let cursor_icon = CursorIcon::Default;
122+
/// ```
97123
#[non_exhaustive]
98124
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
99125
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -231,6 +257,30 @@ impl CursorIcon {
231257
///
232258
/// This name most of the time could be passed as is to cursor loading
233259
/// libraries on X11/Wayland and could be used as-is on web.
260+
///
261+
/// # Examples
262+
///
263+
/// ```no_run
264+
/// use cursor_icon::CursorIcon;
265+
/// use wayland_cursor::CursorTheme;
266+
///
267+
/// # use wayland_client::Connection;
268+
/// # use wayland_client::protocol::wl_shm::WlShm;
269+
/// # fn test(conn: &Connection, shm: WlShm) -> Result<(), Box<dyn std::error::Error>> {
270+
/// // Choose a cursor to load.
271+
/// let cursor = CursorIcon::Help;
272+
///
273+
/// // Load the Wayland cursor theme.
274+
/// let mut cursor_theme = CursorTheme::load(conn, shm, 32)?;
275+
///
276+
/// // Load the cursor.
277+
/// let cursor = cursor_theme.get_cursor(cursor.name());
278+
/// if let Some(cursor) = cursor {
279+
/// println!("Total number of images: {}", cursor.image_count());
280+
/// }
281+
/// # Ok(())
282+
/// # }
283+
/// ```
234284
pub fn name(&self) -> &'static str {
235285
match self {
236286
CursorIcon::Default => "default",
@@ -316,6 +366,10 @@ impl core::str::FromStr for CursorIcon {
316366
}
317367

318368
/// An error which could be returned when parsing [`CursorIcon`].
369+
///
370+
/// This occurs when the [`FromStr`] implementation of [`CursorIcon`] fails.
371+
///
372+
/// [`FromStr`]: core::str::FromStr
319373
#[derive(Debug, PartialEq, Eq)]
320374
pub struct CursorIconParseError {
321375
_private: (),

0 commit comments

Comments
 (0)