mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-20 07:31:39 +01:00
refactor(protocol): use pathext from komorebi-protocol in KDE
Delete komorebi-for-kde/komorebi/src/core/pathext.rs (identical to the crate's copy) and re-export komorebi_protocol::pathext from core, so every existing path keeps working. komorebi-protocol becomes a workspace dependency of komorebi-for-kde (schemars feature on); the crate no longer lists dirs (unused until the client moves), so the root Cargo.lock loses that edge. KDE's Cargo.lock gains komorebi-protocol and cargo re-resolved one windows-sys entry. Verified: cargo check --workspace --all-targets in komorebi-for-kde: 0 errors; cargo test -p komorebi --lib --test schemas: 305 + 5 passed, 0 failed. Full KDE workspace test run is done at the end of the stage.
This commit is contained in:
1 parent
c8f79d99d5
commit
b5f9016a12
7 files changed
+21
-233
No files matched your search
Generated
-1
@@ -3473,7 +3473,6 @@ version = "0.1.42"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"color-eyre",
|
||||
"dirs 6.0.0",
|
||||
"komorebi-layouts",
|
||||
"komorebi-themes",
|
||||
"schemars 1.2.3",
|
||||
|
||||
@@ -10,7 +10,6 @@ komorebi-themes = { path = "../../komorebi-themes" }
|
||||
|
||||
clap = { workspace = true }
|
||||
color-eyre = { workspace = true }
|
||||
dirs = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
serde_with = { version = "3.12", features = ["schemars_1"] }
|
||||
|
||||
Generated
+17
-1
@@ -2503,6 +2503,7 @@ dependencies = [
|
||||
"hotwatch",
|
||||
"komorebi-hotkeys",
|
||||
"komorebi-layouts",
|
||||
"komorebi-protocol",
|
||||
"komorebi-scrolling",
|
||||
"komorebi-themes",
|
||||
"lazy_static",
|
||||
@@ -2556,6 +2557,21 @@ dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "komorebi-protocol"
|
||||
version = "0.1.42"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"color-eyre",
|
||||
"komorebi-layouts",
|
||||
"komorebi-themes",
|
||||
"schemars 1.2.3",
|
||||
"serde",
|
||||
"serde_json_lenient",
|
||||
"serde_with",
|
||||
"strum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "komorebi-scrolling"
|
||||
version = "0.1.42"
|
||||
@@ -5580,7 +5596,7 @@ version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||
dependencies = [
|
||||
"windows-sys 0.48.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -22,6 +22,8 @@ komorebi-layouts = { path = "../komorebi-layouts", features = [
|
||||
# the model of a strip of columns that the Scrolling layout is laid out with, from the same
|
||||
# revision, whose layouts re-export it as `komorebi_layouts::scrolling::model`
|
||||
komorebi-scrolling = { path = "../komorebi-scrolling" }
|
||||
# the wire types shared with the other komorebi daemons
|
||||
komorebi-protocol = { path = "../crates/komorebi-protocol", default-features = false, features = ["schemars"] }
|
||||
# the themes (and the palettes made from pictures), from the same upstream revision
|
||||
komorebi-themes = { path = "../komorebi-themes" }
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ komorebi-hotkeys = { path = "../komorebi-hotkeys" }
|
||||
komorebi-themes = { workspace = true }
|
||||
komorebi-layouts = { workspace = true }
|
||||
komorebi-scrolling = { workspace = true }
|
||||
komorebi-protocol = { workspace = true }
|
||||
version = { path = "../version" }
|
||||
|
||||
clap = { workspace = true }
|
||||
|
||||
@@ -53,7 +53,7 @@ pub use komorebi_layouts::validate_ratios;
|
||||
pub mod animation;
|
||||
pub mod asc;
|
||||
pub mod config_generation;
|
||||
pub mod pathext;
|
||||
pub use komorebi_protocol::pathext;
|
||||
|
||||
#[derive(
|
||||
Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, Display, EnumString, ValueEnum,
|
||||
|
||||
@@ -1,229 +0,0 @@
|
||||
//! Resolving environment variables in the paths of the configuration.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::OsStr;
|
||||
use std::path::Component;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
/// Path extension trait
|
||||
pub trait PathExt {
|
||||
/// Resolve environment variable components in a path.
|
||||
///
|
||||
/// Resolves the following formats:
|
||||
/// - CMD: `%variable%`
|
||||
/// - PowerShell: `$Env:variable`
|
||||
/// - Bash: `$variable`.
|
||||
fn replace_env(&self) -> PathBuf;
|
||||
}
|
||||
|
||||
/// Blanket implementation for all types that can be converted to a `Path`.
|
||||
impl<P: AsRef<Path>> PathExt for P {
|
||||
fn replace_env(&self) -> PathBuf {
|
||||
let mut out = PathBuf::new();
|
||||
|
||||
for c in self.as_ref().components() {
|
||||
match c {
|
||||
#[allow(unused_mut)]
|
||||
Component::Normal(mut c) => {
|
||||
#[cfg(target_os = "windows")]
|
||||
// Special case for ~ and $HOME, replace with $Env:USERPROFILE
|
||||
if c == OsStr::new("~") || c.eq_ignore_ascii_case("$HOME") {
|
||||
c = OsStr::new("$Env:USERPROFILE");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
// Special case for ~, replace with $HOME (macOS and Linux)
|
||||
if c == OsStr::new("~") {
|
||||
c = OsStr::new("$HOME");
|
||||
}
|
||||
|
||||
let bytes = c.as_encoded_bytes();
|
||||
|
||||
// %LOCALAPPDATA%
|
||||
let var = if bytes[0] == b'%' && bytes[bytes.len() - 1] == b'%' {
|
||||
Some(&bytes[1..bytes.len() - 1])
|
||||
} else {
|
||||
// prefix length is 5 for $Env: and 1 for $
|
||||
// so we take the minimum of 5 and the length of the bytes
|
||||
let prefix = &bytes[..5.min(bytes.len())];
|
||||
let prefix = unsafe { OsStr::from_encoded_bytes_unchecked(prefix) };
|
||||
|
||||
// $Env:LOCALAPPDATA
|
||||
if prefix.eq_ignore_ascii_case("$Env:") {
|
||||
Some(&bytes[5..])
|
||||
} else if bytes[0] == b'$' {
|
||||
// $LOCALAPPDATA
|
||||
Some(&bytes[1..])
|
||||
} else {
|
||||
// not a variable
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
// if component is a variable, get the value from the environment
|
||||
if let Some(var) = var {
|
||||
let var = unsafe { OsStr::from_encoded_bytes_unchecked(var) };
|
||||
if let Some(value) = std::env::var_os(var) {
|
||||
out.push(value);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// if not a variable, or a value couldn't be obtained from environemnt
|
||||
// then push the component as is
|
||||
out.push(c);
|
||||
}
|
||||
|
||||
// other components are pushed as is
|
||||
_ => out.push(c),
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// Replace environment variables in a path. This is a wrapper around
|
||||
/// [`PathExt::replace_env`] to be used in Clap arguments parsing.
|
||||
pub fn replace_env_in_path(input: &str) -> Result<PathBuf, std::convert::Infallible> {
|
||||
Ok(input.replace_env())
|
||||
}
|
||||
|
||||
/// A wrapper around [`PathBuf`] that has a custom [Deserialize] implementation
|
||||
/// that uses [`PathExt::replace_env`] to resolve environment variables
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ResolvedPathBuf(PathBuf);
|
||||
|
||||
impl ResolvedPathBuf {
|
||||
/// Create a new [`ResolvedPathBuf`] from a [`PathBuf`]
|
||||
pub fn new(path: PathBuf) -> Self {
|
||||
Self(path.replace_env())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ResolvedPathBuf> for PathBuf {
|
||||
fn from(path: ResolvedPathBuf) -> Self {
|
||||
path.0
|
||||
}
|
||||
}
|
||||
|
||||
impl serde_with::SerializeAs<PathBuf> for ResolvedPathBuf {
|
||||
fn serialize_as<S>(path: &PathBuf, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
path.serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> serde_with::DeserializeAs<'de, PathBuf> for ResolvedPathBuf {
|
||||
fn deserialize_as<D>(deserializer: D) -> Result<PathBuf, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let path = PathBuf::deserialize(deserializer)?;
|
||||
Ok(path.replace_env())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "schemars")]
|
||||
impl serde_with::schemars_1::JsonSchemaAs<PathBuf> for ResolvedPathBuf {
|
||||
fn schema_name() -> std::borrow::Cow<'static, str> {
|
||||
std::borrow::Cow::Borrowed("PathBuf")
|
||||
}
|
||||
|
||||
fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
|
||||
// the description is part of the checked-in schemas, which differ per platform
|
||||
#[cfg(target_os = "windows")]
|
||||
let description = "A file system path. Environment variables like %VAR%, $Env:VAR, or $VAR are automatically resolved.";
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let description = "A file system path. Environment variables like $VAR are automatically resolved.";
|
||||
|
||||
schemars::json_schema!({
|
||||
"type": "string",
|
||||
"description": description
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Custom deserializer for [`Option<HashMap<usize, PathBuf>>`] that uses
|
||||
/// [`PathExt::replace_env`] to resolve environment variables in the paths.
|
||||
///
|
||||
/// This is used in `WorkspaceConfig` struct because we can't use
|
||||
/// #[serde_with::serde_as] as it doesn't handle [`Option<HashMap<usize, ResolvedPathBuf>>`]
|
||||
/// quite well and generated compiler errors that can't be fixed because of Rust's orphan rule.
|
||||
pub fn resolve_option_hashmap_usize_path<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<HashMap<usize, PathBuf>>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let map = Option::<HashMap<usize, PathBuf>>::deserialize(deserializer)?;
|
||||
Ok(map.map(|map| map.into_iter().map(|(k, v)| (k, v.replace_env())).collect()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// helper functions
|
||||
fn expected<P: AsRef<Path>>(p: P) -> PathBuf {
|
||||
// Ensure that the path is using the correct path separator for the OS.
|
||||
p.as_ref().components().collect::<PathBuf>()
|
||||
}
|
||||
|
||||
fn resolve<P: AsRef<Path>>(p: P) -> PathBuf {
|
||||
p.replace_env()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_env_vars() {
|
||||
// Set a variable for testing
|
||||
unsafe {
|
||||
std::env::set_var("VAR", "VALUE");
|
||||
}
|
||||
|
||||
// %VAR% format
|
||||
assert_eq!(resolve("/path/%VAR%/d"), expected("/path/VALUE/d"));
|
||||
// $env:VAR format
|
||||
assert_eq!(resolve("/path/$env:VAR/d"), expected("/path/VALUE/d"));
|
||||
// $VAR format
|
||||
assert_eq!(resolve("/path/$VAR/d"), expected("/path/VALUE/d"));
|
||||
|
||||
// non-existent variable
|
||||
assert_eq!(resolve("/path/%ASD%/to/d"), expected("/path/%ASD%/to/d"));
|
||||
assert_eq!(
|
||||
resolve("/path/$env:ASD/to/d"),
|
||||
expected("/path/$env:ASD/to/d")
|
||||
);
|
||||
assert_eq!(resolve("/path/$ASD/to/d"), expected("/path/$ASD/to/d"));
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
// Set a $env:USERPROFILE variable for testing
|
||||
unsafe {
|
||||
std::env::set_var("USERPROFILE", "C:\\Users\\user");
|
||||
}
|
||||
|
||||
// ~ and $HOME should be replaced with $Env:USERPROFILE
|
||||
assert_eq!(resolve("~"), expected("C:\\Users\\user"));
|
||||
assert_eq!(resolve("$HOME"), expected("C:\\Users\\user"));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
// Set a $env:USERPROFILE variable for testing
|
||||
unsafe {
|
||||
std::env::set_var("HOME", "/Users/user");
|
||||
}
|
||||
|
||||
// ~ and $HOME should be replaced with $Env:USERPROFILE
|
||||
assert_eq!(resolve("~"), expected("/Users/user"));
|
||||
assert_eq!(resolve("$HOME"), expected("/Users/user"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user