Skip to main content

wslplugins_rs/api/errors/
require_update_error.rs

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
7use crate::WSLVersion;
8use thiserror::Error;
9use windows_core::HRESULT;
10
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.
19#[derive(Debug, Error)]
20#[error("WSLVersion unsupported: current version {current_version}, required version {required_version}")]
21pub struct Error {
22    /// The current version of WSL.
23    pub current_version: WSLVersion,
24    /// The required version of WSL.
25    pub required_version: WSLVersion,
26}
27
28impl 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`.
37    #[must_use]
38    #[inline]
39    pub const fn new(current_version: WSLVersion, required_version: WSLVersion) -> Self {
40        Self {
41            current_version,
42            required_version,
43        }
44    }
45    pub const WSL_E_PLUGIN_REQUIRES_UPDATE: HRESULT =
46        HRESULT(wslpluginapi_sys::WSL_E_PLUGIN_REQUIRES_UPDATE);
47}
48
49impl From<Error> for HRESULT {
50    /// Converts the `Error` into the corresponding `HRESULT` error code.
51    ///
52    /// This implementation maps the custom `Error` to the `WSL_E_PLUGIN_REQUIRES_UPDATE` HRESULT.
53    ///
54    /// # Note
55    /// [`Error::WSL_E_PLUGIN_REQUIRES_UPDATE`]: Indicates the WSL version is insufficient for the plugin.
56    #[inline]
57    fn from(_: Error) -> Self {
58        Self(Error::WSL_E_PLUGIN_REQUIRES_UPDATE.0)
59    }
60}
61
62impl From<Error> for windows_core::Error {
63    /// Converts the `Error` into a [`windows_core::Error`].
64    ///
65    /// This implementation creates a [`windows_core::Error`] with the [`Error::WSL_E_PLUGIN_REQUIRES_UPDATE`] code.
66    #[inline]
67    fn from(value: Error) -> Self {
68        Self::from(HRESULT::from(value))
69    }
70}
71
72/// A result type alias for operations that may return a WSL plugin error.
73///
74/// This alias provides convenience when working with operations that might fail due to unsupported
75/// WSL versions.
76pub type Result<T> = std::result::Result<T, Error>;