refactor(ring): make the Windows ring.rs identical to the documented one and use pastey

Windows ring.rs is replaced by the KDE copy (doc comments, tests, pastey::paste!, std::collections::VecDeque path). pastey 0.2 added to the root workspace deps and komorebi/Cargo.toml (paste stays: windows_api.rs and komorebic use it). The VecDeque imports in container.rs and workspace.rs only served the old macro text and are removed.

Verified: fast-check Windows clippy --all-targets -D warnings: PASS (tests compiled, not run: Windows-only).
This commit is contained in:
LGUG2Z committed 2026-10-31 01:37:53 -07:00
1 parent 7aee2e53a9
commit b3519814c8
6 files changed
+145 -12

No files matched your search

Generated
+8 -1
View File
@@ -583,7 +583,7 @@ dependencies = [
"log",
"num-rational 0.4.2",
"num-traits",
"pastey",
"pastey 0.1.1",
"rayon",
"thiserror 2.0.21",
"v_frame",
@@ -3347,6 +3347,7 @@ dependencies = [
"os_info",
"parking_lot",
"paste",
"pastey 0.2.3",
"powershell_script",
"regex",
"reqwest",
@@ -4979,6 +4980,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec"
[[package]]
name = "pastey"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4"
[[package]]
name = "percent-encoding"
version = "2.3.2"
+1
View File
@@ -39,6 +39,7 @@ tracing-appender = "0.2"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
parking_lot = "0.12"
paste = "1"
pastey = "0.2"
sysinfo = "0.39"
uds_windows = "1"
win32-display-data = { git = "https://github.com/LGUG2Z/win32-display-data", rev = "8c42d8db257d30fe95bc98c2e5cd8f75da861021" }
+1
View File
@@ -30,6 +30,7 @@ net2 = "0.2"
os_info = "3.10"
parking_lot = { workspace = true }
paste = { workspace = true }
pastey = { workspace = true }
powershell_script = "1.0"
regex = "1"
reqwest = { version = "0.12", features = ["blocking"] }
-2
View File
@@ -1,5 +1,3 @@
use std::collections::VecDeque;
use nanoid::nanoid;
use serde::Deserialize;
use serde::Serialize;
+135 -8
View File
@@ -1,8 +1,33 @@
//! [`Ring`], a list of elements with one of them focused.
//!
//! Monitors, workspaces and containers each keep their children in a ring, so that "the focused
//! one" is the same thing everywhere, and `impl_ring_elements!` gives each of them the same
//! accessors for it.
use std::collections::VecDeque;
use serde::Deserialize;
use serde::Serialize;
/// A list of elements, one of which is focused.
///
/// The focused index is not guaranteed to be in range: [`Ring::focus`] stores what it is given, and
/// [`Ring::focused`] and [`Ring::focused_mut`] return `None` when it is out of range, as it is for
/// an empty ring.
///
/// # Example
///
/// ```rust
/// use komorebi::ring::Ring;
///
/// let mut ring = Ring::default();
/// ring.elements_mut().push_back("a");
/// ring.elements_mut().push_back("b");
/// ring.focus(1);
///
/// assert_eq!(ring.focused(), Some(&"b"));
/// assert_eq!(ring.focused_idx(), 1);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Ring<T> {
@@ -20,40 +45,53 @@ impl<T> Default for Ring<T> {
}
impl<T> Ring<T> {
/// The elements, in order.
pub const fn elements(&self) -> &VecDeque<T> {
&self.elements
}
/// The elements, in order, to add to or remove from.
pub fn elements_mut(&mut self) -> &mut VecDeque<T> {
&mut self.elements
}
/// Focuses the element at `idx`.
pub fn focus(&mut self, idx: usize) {
self.focused = idx;
}
/// The focused element, if the focused index is in range.
pub fn focused(&self) -> Option<&T> {
self.elements.get(self.focused)
}
/// The index that is focused, in range or not.
pub const fn focused_idx(&self) -> usize {
self.focused
}
/// The focused element, to change, if the focused index is in range.
pub fn focused_mut(&mut self) -> Option<&mut T> {
self.elements.get_mut(self.focused)
}
}
/// Gives a type that keeps its children in a [`Ring`] the accessors every such type has.
///
/// `impl_ring_elements!(Workspace, Container)` reads the field `containers: Ring<Container>` and
/// gives `Workspace` the methods `containers()`, `containers_mut()`, `focused_container()`,
/// `focused_container_idx()` and `focused_container_mut()`. The field is named after the element
/// type in lower case with an `s` appended, and the methods after the field, so that "the focused
/// container" is spelled the same way on every type that has one.
macro_rules! impl_ring_elements {
($name:ty, $element:ident) => {
paste::paste! {
pastey::paste! {
impl $name {
pub const fn [<$element:lower s>](&self) -> &VecDeque<$element> {
pub const fn [<$element:lower s>](&self) -> &std::collections::VecDeque<$element> {
self.[<$element:lower s>].elements()
}
pub fn [<$element:lower s_mut>](&mut self) -> &mut VecDeque<$element> {
pub fn [<$element:lower s_mut>](&mut self) -> &mut std::collections::VecDeque<$element> {
self.[<$element:lower s>].elements_mut()
}
@@ -71,21 +109,20 @@ macro_rules! impl_ring_elements {
}
}
}
};
// This allows passing a different name to be used for the functions. For instance, the
}; // This allows passing a different name to be used for the functions. For instance, the
// `floating_windows` ring calls this as:
// ```rust
// impl_ring_elements!(Workspace, Window, "floating_window");
// ```
// Which allows using the `Window` element but name the functions as `floating_window`
($name:ty, $element:ident, $el_name:literal) => {
paste::paste! {
pastey::paste! {
impl $name {
pub const fn [<$el_name:lower s>](&self) -> &VecDeque<$element> {
pub const fn [<$el_name:lower s>](&self) -> &std::collections::VecDeque<$element> {
self.[<$el_name:lower s>].elements()
}
pub fn [<$el_name:lower s_mut>](&mut self) -> &mut VecDeque<$element> {
pub fn [<$el_name:lower s_mut>](&mut self) -> &mut std::collections::VecDeque<$element> {
self.[<$el_name:lower s>].elements_mut()
}
@@ -105,3 +142,93 @@ macro_rules! impl_ring_elements {
}
};
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, PartialEq)]
struct Book(&'static str);
struct Shelf {
books: Ring<Book>,
}
impl_ring_elements!(Shelf, Book);
#[test]
fn impl_ring_elements_names_the_accessors_after_the_element() {
let mut shelf = Shelf {
books: Ring::default(),
};
shelf.books_mut().extend([Book("a"), Book("b")]);
shelf.books.focus(1);
assert_eq!(shelf.books().len(), 2);
assert_eq!(shelf.focused_book(), Some(&Book("b")));
assert_eq!(shelf.focused_book_idx(), 1);
if let Some(book) = shelf.focused_book_mut() {
*book = Book("c");
}
assert_eq!(shelf.books()[1], Book("c"));
}
#[test]
fn an_empty_ring_focuses_nothing() {
let mut ring: Ring<u8> = Ring::default();
assert_eq!(ring.focused(), None);
assert_eq!(ring.focused_mut(), None);
assert_eq!(ring.focused_idx(), 0);
}
#[test]
fn focus_selects_the_element_at_that_index() {
let mut ring = Ring::default();
ring.elements_mut().extend([10, 20, 30]);
ring.focus(2);
assert_eq!(ring.focused(), Some(&30));
assert_eq!(ring.focused_idx(), 2);
}
#[test]
fn the_focused_element_can_be_changed_in_place() {
let mut ring = Ring::default();
ring.elements_mut().extend([1, 2]);
ring.focus(1);
if let Some(element) = ring.focused_mut() {
*element = 5;
}
assert_eq!(ring.elements(), &[1, 5]);
}
#[test]
fn a_focused_index_past_the_end_focuses_nothing() {
let mut ring = Ring::default();
ring.elements_mut().push_back('a');
ring.focus(3);
assert_eq!(ring.focused(), None);
assert_eq!(ring.focused_idx(), 3);
}
#[test]
fn a_ring_round_trips_through_json() {
let mut ring = Ring::default();
ring.elements_mut()
.extend(["a".to_string(), "b".to_string()]);
ring.focus(1);
let json = serde_json::to_string(&ring).expect("serializes");
let back: Ring<String> = serde_json::from_str(&json).expect("deserializes");
assert_eq!(back, ring);
}
}
-1
View File
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::collections::VecDeque;
use std::ffi::OsStr;
use std::fmt::Display;
use std::fmt::Formatter;