11extern crate wslplugins_sys;
2+ #[ cfg( doc) ]
3+ use super :: Error ;
24use super :: Result ;
35use crate :: api:: errors:: require_update_error:: Result as UpReqResult ;
46use 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
2528use 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
3442impl 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 }
0 commit comments