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;
8mod requirement_definition;
9pub use requirement_definition::RequirementDefinition;
10use thiserror::Error;
11use windows_core::HRESULT;
12
13/// Represents an error when the current WSL version is unsupported.
14///
15/// This error is returned when the WSL version being used does not satisfy
16/// the required version or capabilities for a plugin.
17///
18/// # Fields
19/// - `current_version`: The current WSL version.
20/// - `requirement`: The version or capabilities required by the operation.
21#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
22#[error("WSLVersion unsupported: current version {current_version}, required {requirement} (minimum version {})", requirement.version())]
23pub struct Error {
24 /// The current version of WSL.
25 pub current_version: WSLVersion,
26 /// The version or capabilities required by the operation.
27 pub requirement: RequirementDefinition,
28}
29
30impl Error {
31 /// Creates a new `Error` with the specified current and required WSL versions.
32 ///
33 /// # Arguments
34 /// - `current_version`: The version currently in use.
35 /// - `required_version`: The version required for compatibility.
36 ///
37 /// # Returns
38 /// A new instance of `Error`.
39 #[must_use]
40 #[inline]
41 pub const fn new(current_version: WSLVersion, required_version: WSLVersion) -> Self {
42 Self {
43 current_version,
44 requirement: RequirementDefinition::Version(required_version),
45 }
46 }
47
48 /// Creates a new `Error` with the specified current WSL version and requirement.
49 ///
50 /// # Arguments
51 /// - `current_version`: The version currently in use.
52 /// - `requirement`: The version or capabilities required for compatibility.
53 ///
54 /// # Returns
55 /// A new instance of `Error`.
56 #[must_use]
57 #[inline]
58 pub fn from_requirement(
59 current_version: WSLVersion,
60 requirement: impl Into<RequirementDefinition>,
61 ) -> Self {
62 Self {
63 current_version,
64 requirement: requirement.into(),
65 }
66 }
67 pub const WSL_E_PLUGIN_REQUIRES_UPDATE: HRESULT =
68 HRESULT(wslpluginapi_sys::WSL_E_PLUGIN_REQUIRES_UPDATE);
69}
70
71impl From<Error> for HRESULT {
72 /// Converts the `Error` into the corresponding `HRESULT` error code.
73 ///
74 /// This implementation maps the custom `Error` to the `WSL_E_PLUGIN_REQUIRES_UPDATE` HRESULT.
75 ///
76 /// # Note
77 /// [`Error::WSL_E_PLUGIN_REQUIRES_UPDATE`]: Indicates the WSL version is insufficient for the plugin.
78 #[inline]
79 fn from(_: Error) -> Self {
80 Self(Error::WSL_E_PLUGIN_REQUIRES_UPDATE.0)
81 }
82}
83
84impl From<Error> for windows_core::Error {
85 /// Converts the `Error` into a [`windows_core::Error`].
86 ///
87 /// This implementation creates a [`windows_core::Error`] with the [`Error::WSL_E_PLUGIN_REQUIRES_UPDATE`] code.
88 #[inline]
89 fn from(value: Error) -> Self {
90 Self::from(HRESULT::from(value))
91 }
92}
93
94/// A result type alias for operations that may return a WSL plugin error.
95///
96/// This alias provides convenience when working with operations that might fail due to unsupported
97/// WSL versions.
98pub type Result<T> = std::result::Result<T, Error>;