Skip to content

Commit 5830c86

Browse files
authored
Create rust documentation (#2)
1 parent 40c79ca commit 5830c86

24 files changed

+887
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Ensure you have the following requirements installed:
1919
- Cargo (Rust's package manager)
2020
- PowerShell (for running signing scripts)
2121
- OpenSSL (used in the signing process) [Download OpenSSL](https://slproweb.com/products/Win32OpenSSL.html)
22-
- **SignTool.exe** from the Windows SDK (for signing the plugin) [Download Windows SDK](https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/)
22+
- **SignTool.exe** from the Windows SDK (for signing the plugin) [Download Windows SDK](https://developer.microsoft.com/windows/downloads/windows-10-sdk/)
2323
- nuget.exe (for downloading [Microsoft's WSL Plugin API](https://www.nuget.org/packages/Microsoft.WSL.PluginApi))
2424

2525
### Important Notes

wslplugins-macro-core/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
pub(crate) mod generator;
2-
pub(crate) mod hooks;
3-
pub(crate) mod parser;
4-
pub(crate) mod utils;
1+
mod generator;
2+
mod hooks;
3+
mod parser;
4+
mod utils;
55

66
use generator::generate;
77
use proc_macro2::TokenStream;

wslplugins-rs/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ optional = true
2626
sys = []
2727
log-full = ["log", "log-instrument"]
2828
macro = ["dep:wslplugins-macro", "sys"]
29+
30+
[package.metadata.docs.rs]
31+
all-features = true
32+
rustdoc-args = ["--cfg", "docsrs"]

wslplugins-rs/src/api/api_v1.rs

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
extern crate wslplugins_sys;
2+
#[cfg(doc)]
3+
use super::Error;
24
use super::Result;
35
use crate::api::errors::require_update_error::Result as UpReqResult;
46
use crate::utils::{cstring_from_str, encode_wide_null_terminated};
@@ -20,23 +22,56 @@ use windows::{
2022
core::{Result as WinResult, GUID, PCSTR, PCWSTR},
2123
Win32::Foundation::BOOL,
2224
};
23-
use wslplugins_sys::WSLVersion;
25+
26+
use wslplugins_sys::{WSLPluginAPIV1, WSLVersion};
2427

2528
use super::utils::check_required_version_result;
26-
pub struct ApiV1<'a>(&'a wslplugins_sys::WSLPluginAPIV1);
2729

28-
impl<'a> From<&'a wslplugins_sys::WSLPluginAPIV1> for ApiV1<'a> {
29-
fn from(value: &'a wslplugins_sys::WSLPluginAPIV1) -> Self {
30+
/// Represents a structured interface for interacting with the WSLPluginAPIV1 API.
31+
/// This struct encapsulates the methods provided by the WSLPluginAPIV1 API, allowing
32+
/// idiomatic interaction with the Windows Subsystem for Linux (WSL).
33+
pub struct ApiV1<'a>(&'a WSLPluginAPIV1);
34+
35+
/// Converts a raw reference to `WSLPluginAPIV1` into [ApiV1].
36+
impl<'a> From<&'a WSLPluginAPIV1> for ApiV1<'a> {
37+
fn from(value: &'a WSLPluginAPIV1) -> Self {
3038
Self(value)
3139
}
3240
}
3341

3442
impl ApiV1<'_> {
43+
/// Returns the current version of the WSL API being used.
44+
///
45+
/// This is useful for checking compatibility with specific API features.
46+
///
47+
/// # Example
48+
/// ```ignore
49+
/// let api_v1: ApiV1 = ...;
50+
/// let version = api_v1.version();
51+
/// println!(
52+
/// "WSL API version: {}.{}.{}",
53+
/// version.Major, version.Minor, version.Revision
54+
/// );
3555
#[cfg_attr(feature = "log-instrument", instrument)]
3656
pub fn version(&self) -> &WSLVersion {
3757
&self.0.Version
3858
}
59+
3960
/// Create plan9 mount between Windows & Linux
61+
/// Allows sharing a folder between the Windows host and the Linux environment.
62+
///
63+
/// # Arguments
64+
/// - `session`: The current WSL session.
65+
/// - `windows_path`: The Windows path of the folder to be mounted.
66+
/// - `linux_path`: The Linux path where the folder will be mounted.
67+
/// - `read_only`: Whether the mount should be read-only.
68+
/// - `name`: A custom name for the mount.
69+
///
70+
/// # Example
71+
/// ``` rust,ignore
72+
/// api.mount_folder(&session, "C:\\path", "/mnt/path", false, "MyMount")?;
73+
/// ```
74+
#[doc(alias = "MountFolder")]
4075
#[cfg_attr(feature = "log-instrument", instrument)]
4176
pub fn mount_folder<WP: AsRef<Path>, UP: AsRef<Utf8UnixPath>>(
4277
&self,
@@ -66,7 +101,36 @@ impl ApiV1<'_> {
66101
}
67102

68103
/// Execute a program in the root namespace.
104+
///
105+
/// This method runs a program in the root namespace of the current WSL session. It connects the standard input and output
106+
/// streams of the executed process to a [`TcpStream`], allowing interaction with the process.
107+
///
108+
/// # Arguments
109+
/// - `session`: The current WSL session.
110+
/// - `path`: Path to the program to execute.
111+
/// - `args`: Arguments to pass to the program (including `arg0`).
112+
///
113+
/// # Returns
114+
/// On success, this method returns a [`TcpStream`] connected to the standard input and output streams of the executed process.
115+
/// - **Standard Input**: Data written to the stream will be sent to the process.
116+
/// - **Standard Output**: Data output by the process will be readable from the stream.
117+
///
118+
/// # Errors
119+
/// This method can return the following a [`windows::core::Error`]: If the underlying Windows API call fails.
120+
///
121+
/// # Example
122+
/// ```rust,ignore
123+
/// let stream = api.execute_binary(&session, "/bin/ls", ["/bin/ls", "-l", "/etc"])?;
124+
/// // Write to the process (stdin)
125+
/// writeln!(stream, "input data").unwrap();
126+
///
127+
/// // Read from the process (stdout)
128+
/// let mut buffer = String::new();
129+
/// stream.read_to_string(&mut buffer).unwrap();
130+
/// println!("Process output: {}", buffer);
131+
/// ```
69132
#[cfg_attr(feature = "log-instrument", instrument)]
133+
#[doc(alias = "ExecuteBinary")]
70134
pub fn execute_binary<P: AsRef<Utf8UnixPath>>(
71135
&self,
72136
session: &WSLSessionInformation,
@@ -109,8 +173,39 @@ impl ApiV1<'_> {
109173
let error_vec = encode_wide_null_terminated(error);
110174
unsafe { self.0.PluginError.unwrap_unchecked()(PCWSTR::from_raw(error_vec.as_ptr())).ok() }
111175
}
176+
112177
/// Execute a program in a user distribution
113-
/// Introduced in 2.1.2
178+
///
179+
/// # Introduced
180+
/// This requires API version 2.1.2 or later.
181+
///
182+
/// # Arguments
183+
/// - `session`: The current WSL session.
184+
/// - `distribution_id`: The ID of the target distribution.
185+
/// - `path`: Path to the program to execute.
186+
/// - `args`: Arguments to pass to the program (including arg0).
187+
///
188+
/// # Returns
189+
/// A [`TcpStream`] connected to the process's stdin and stdout.
190+
///
191+
/// # Errors
192+
/// This function may return the following errors:
193+
///
194+
/// - [`Error::RequiresUpdate`]: If the API version is lower than 2.1.2.
195+
/// - [`Error::WinError`]: If the Windows API fails during execution.
196+
///
197+
/// # Example
198+
/// ```rust,ignore
199+
/// let stream = api.execute_binary_in_distribution(&session, "/bin/ls", ["/bin/ls", "-l", "/etc"])?;
200+
/// // Write to the process (stdin)
201+
/// writeln!(stream, "input data").unwrap();
202+
///
203+
/// // Read from the process (stdout)
204+
/// let mut buffer = String::new();
205+
/// stream.read_to_string(&mut buffer).unwrap();
206+
/// println!("Process output: {}", buffer);
207+
/// ```
208+
#[doc(alias = "ExecuteBinaryInDistribution")]
114209
#[cfg_attr(feature = "log-instrument", instrument)]
115210
pub fn execute_binary_in_distribution<P: AsRef<Utf8UnixPath>>(
116211
&self,
@@ -151,6 +246,7 @@ impl ApiV1<'_> {
151246
};
152247
Ok(stream)
153248
}
249+
154250
fn check_required_version(&self, version: &WSLVersion) -> UpReqResult<()> {
155251
check_required_version_result(self.version(), version)
156252
}

wslplugins-rs/src/api/errors.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1+
//! # Error Handling for WSL Plugins
2+
//!
3+
//! This module defines a unified error type for handling errors in WSL plugins, combining
4+
//! both custom plugin-specific errors and Windows system errors. It provides seamless
5+
//! interoperability with Windows APIs using `HRESULT`.
6+
17
use thiserror::Error;
28
pub mod require_update_error;
39
pub use require_update_error::Error as RequireUpdateError;
410
use windows::core::{Error as WinError, HRESULT};
511
use wslplugins_sys::WSL_E_PLUGIN_REQUIRES_UPDATE;
612

13+
/// A comprehensive error type for WSL plugins.
14+
///
15+
/// This enum encapsulates two main error categories:
16+
/// - Plugin-specific errors (`RequireUpdateError`).
17+
/// - Errors originating from Windows APIs (`WinError`).
718
#[derive(Debug, Error)]
819
pub enum Error {
20+
/// Indicates that the current WSL version does not meet the required version.
921
#[error("Require Update Error")]
1022
RequiresUpdate(#[from] RequireUpdateError),
23+
24+
/// Represents an error from Windows APIs.
1125
#[error("Windows Error: {0}")]
1226
WinError(#[from] WinError),
1327
}
1428

1529
impl From<Error> for HRESULT {
30+
/// Converts the `Error` enum into an `HRESULT` code.
31+
///
32+
/// - Maps `Error::RequiresUpdate` to `WSL_E_PLUGIN_REQUIRES_UPDATE`.
33+
/// - Maps `Error::WinError` to its corresponding `HRESULT` code.
34+
///
35+
/// # Returns
36+
/// An `HRESULT` code representing the error.
1637
fn from(value: Error) -> Self {
1738
match value {
1839
Error::RequiresUpdate(err) => err.into(),
@@ -22,6 +43,13 @@ impl From<Error> for HRESULT {
2243
}
2344

2445
impl From<Error> for WinError {
46+
/// Converts the `Error` enum into a `WinError`.
47+
///
48+
/// - Maps `Error::RequiresUpdate` to `WSL_E_PLUGIN_REQUIRES_UPDATE`.
49+
/// - Maps `Error::WinError` directly to itself.
50+
///
51+
/// # Returns
52+
/// A `WinError` representing the error.
2553
fn from(value: Error) -> Self {
2654
match value {
2755
Error::RequiresUpdate { .. } => WSL_E_PLUGIN_REQUIRES_UPDATE.into(),
@@ -30,4 +58,8 @@ impl From<Error> for WinError {
3058
}
3159
}
3260

61+
/// A type alias for results using the custom `Error` type.
62+
///
63+
/// This alias simplifies the return type for functions that may fail, ensuring consistency
64+
/// across the codebase.
3365
pub type Result<T> = std::result::Result<T, Error>;

wslplugins-rs/src/api/errors/require_update_error.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1+
//! # WSL Plugin Error Handling
2+
//!
3+
//! This module provides an error type and result alias to handle scenarios where the current WSL version
4+
//! does not meet the required version. It integrates with Windows error codes for seamless interop
5+
//! with WSL APIs.
6+
17
use thiserror::Error;
28
use windows::core::HRESULT;
39
use wslplugins_sys::{WSLVersion, WSL_E_PLUGIN_REQUIRES_UPDATE};
410

11+
/// Represents an error when the current WSL version is unsupported.
12+
///
13+
/// This error is returned when the WSL version being used does not satisfy
14+
/// the required version for a plugin.
15+
///
16+
/// # Fields
17+
/// - `current_version`: The current WSL version.
18+
/// - `required_version`: The required WSL version.
519
#[derive(Debug, Error)]
620
#[error("WSLVersion unsupported: current version {current_version}, required version {required_version}")]
721
pub struct Error {
22+
/// The current version of WSL.
823
pub current_version: WSLVersion,
24+
/// The required version of WSL.
925
pub required_version: WSLVersion,
1026
}
1127

1228
impl Error {
29+
/// Creates a new `Error` with the specified current and required WSL versions.
30+
///
31+
/// # Arguments
32+
/// - `current_version`: The version currently in use.
33+
/// - `required_version`: The version required for compatibility.
34+
///
35+
/// # Returns
36+
/// A new instance of `Error`.
1337
pub fn new(current_version: WSLVersion, required_version: WSLVersion) -> Self {
1438
Self {
1539
current_version,
@@ -19,9 +43,19 @@ impl Error {
1943
}
2044

2145
impl From<Error> for HRESULT {
46+
/// Converts the `Error` into an `HRESULT` error code.
47+
///
48+
/// This implementation maps the custom `Error` to the `WSL_E_PLUGIN_REQUIRES_UPDATE` HRESULT.
49+
///
50+
/// # Returns
51+
/// - `[WSL_E_PLUGIN_REQUIRES_UPDATE]: Indicates the WSL version is insufficient for the plugin.
2252
fn from(_: Error) -> Self {
2353
WSL_E_PLUGIN_REQUIRES_UPDATE
2454
}
2555
}
2656

57+
/// A result type alias for operations that may return a WSL plugin error.
58+
///
59+
/// This alias provides convenience when working with operations that might fail due to unsupported
60+
/// WSL versions.
2761
pub type Result<T> = std::result::Result<T, Error>;

wslplugins-rs/src/api/mod.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
pub mod api_v1;
1+
//! # API Module
2+
//!
3+
//! This module provides the core functionality for interacting with the WSL plugin API.
4+
//! It includes abstractions for API interactions, error handling, and utility functions.
5+
6+
mod api_v1;
27
pub mod errors;
8+
9+
/// The `ApiV1` struct provides an interface to interact with version 1 of the WSL Plugin API.
10+
///
11+
/// It encapsulates low-level interactions with the API and exposes a safe and idiomatic Rust interface.
312
pub use api_v1::ApiV1;
4-
pub use errors::{Error, Result};
13+
14+
/// The `Error` type represents errors that may occur while using the WSL Plugin API.
15+
///
16+
/// This type encapsulates various error scenarios, providing a consistent and ergonomic way
17+
/// to handle failures.
18+
pub use errors::Error;
19+
20+
/// A specialized `Result` type for operations that may fail in the context of the WSL Plugin API.
21+
///
22+
/// This alias simplifies the function signatures by standardizing error handling.
23+
pub use errors::Result;
24+
25+
/// The `utils` module provides utility functions and helpers for working with the WSL Plugin API.
26+
///
27+
/// These utilities simplify common tasks, such as version checking or string manipulation.
528
pub mod utils;

wslplugins-rs/src/api/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
use crate::WSLContext;
1+
//! # WSL Version Checking Utilities
2+
//!
3+
//! This module provides utilities to verify that the current WSL version meets the required version for plugin compatibility.
4+
//! It includes functions to perform version checks and unit tests to ensure correctness.
25
36
use super::errors::require_update_error::{Error, Result};
7+
use crate::WSLContext;
48
use wslplugins_sys::WSLVersion;
59

610
pub(crate) fn check_required_version_result(
@@ -30,6 +34,7 @@ mod tests {
3034
use super::*;
3135
use wslplugins_sys::WSLVersion;
3236

37+
/// Tests that `check_required_version_result` returns `Ok` when the current version meets the requirement.
3338
#[test]
3439
fn test_check_required_version_result_ok() {
3540
let current_version = WSLVersion::new(2, 1, 3);
@@ -40,6 +45,7 @@ mod tests {
4045
assert!(result.is_ok());
4146
}
4247

48+
/// Tests that `check_required_version_result` returns `Err` when the current version is insufficient.
4349
#[test]
4450
fn test_check_required_version_result_error() {
4551
let current_version = WSLVersion::new(2, 1, 1);

0 commit comments

Comments
 (0)