Skip to main content

reovim_kernel/api/module/
flags.rs

1//! Registration capability flags.
2
3/// Bit position for "required" flag.
4const FLAG_REQUIRED: u8 = 1 << 0;
5/// Bit position for "deferrable" flag.
6const FLAG_DEFERRABLE: u8 = 1 << 1;
7/// Bit position for "early" flag.
8const FLAG_EARLY: u8 = 1 << 2;
9/// Bit position for "fallback" flag.
10const FLAG_FALLBACK: u8 = 1 << 3;
11
12/// Registration capability flags (Linux-inspired).
13///
14/// Like Linux kernel module flags, these control registration behavior.
15/// Used by all registration types to indicate how the runner should handle
16/// the registration.
17///
18/// Stored as a `u8` bitfield to avoid excessive boolean fields while
19/// preserving zero external dependencies in the kernel crate.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
21pub struct RegistrationFlags(u8);
22
23impl RegistrationFlags {
24    /// Create default flags (all false).
25    #[must_use]
26    pub const fn new() -> Self {
27        Self(0)
28    }
29
30    /// Create flags for required registration.
31    #[must_use]
32    pub const fn required() -> Self {
33        Self(FLAG_REQUIRED)
34    }
35
36    /// Create flags for deferrable registration.
37    #[must_use]
38    pub const fn deferrable() -> Self {
39        Self(FLAG_DEFERRABLE)
40    }
41
42    // ========================================================================
43    // Chainable Builders
44    // ========================================================================
45
46    /// Set the required flag (chainable).
47    ///
48    /// # Example
49    ///
50    /// ```
51    /// use reovim_kernel::api::v1::RegistrationFlags;
52    ///
53    /// let flags = RegistrationFlags::new().set_required().set_early();
54    /// assert!(flags.is_required());
55    /// assert!(flags.is_early());
56    /// ```
57    #[must_use]
58    pub const fn set_required(mut self) -> Self {
59        self.0 |= FLAG_REQUIRED;
60        self
61    }
62
63    /// Set the deferrable flag (chainable).
64    #[must_use]
65    pub const fn set_deferrable(mut self) -> Self {
66        self.0 |= FLAG_DEFERRABLE;
67        self
68    }
69
70    /// Set the early flag (chainable).
71    #[must_use]
72    pub const fn set_early(mut self) -> Self {
73        self.0 |= FLAG_EARLY;
74        self
75    }
76
77    /// Set the fallback flag (chainable).
78    #[must_use]
79    pub const fn set_fallback(mut self) -> Self {
80        self.0 |= FLAG_FALLBACK;
81        self
82    }
83
84    // ========================================================================
85    // Query Methods
86    // ========================================================================
87
88    /// Check if this registration is required.
89    #[must_use]
90    pub const fn is_required(self) -> bool {
91        self.0 & FLAG_REQUIRED != 0
92    }
93
94    /// Check if this registration is deferrable.
95    #[must_use]
96    pub const fn is_deferrable(self) -> bool {
97        self.0 & FLAG_DEFERRABLE != 0
98    }
99
100    /// Check if this registration should happen early.
101    #[must_use]
102    pub const fn is_early(self) -> bool {
103        self.0 & FLAG_EARLY != 0
104    }
105
106    /// Check if this registration acts as a fallback.
107    #[must_use]
108    pub const fn is_fallback(self) -> bool {
109        self.0 & FLAG_FALLBACK != 0
110    }
111}