diff --git a/Cargo.lock b/Cargo.lock index 67698968..1b48b1e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3473,7 +3473,6 @@ version = "0.1.42" dependencies = [ "clap", "color-eyre", - "dirs 6.0.0", "komorebi-layouts", "komorebi-themes", "schemars 1.2.3", diff --git a/crates/komorebi-protocol/Cargo.toml b/crates/komorebi-protocol/Cargo.toml index 4e50b722..59f0505e 100644 --- a/crates/komorebi-protocol/Cargo.toml +++ b/crates/komorebi-protocol/Cargo.toml @@ -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"] } diff --git a/komorebi-for-kde/Cargo.lock b/komorebi-for-kde/Cargo.lock index 0e86c79f..b4f9bf8e 100644 --- a/komorebi-for-kde/Cargo.lock +++ b/komorebi-for-kde/Cargo.lock @@ -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]] diff --git a/komorebi-for-kde/Cargo.toml b/komorebi-for-kde/Cargo.toml index b60aa20e..fc546ee5 100644 --- a/komorebi-for-kde/Cargo.toml +++ b/komorebi-for-kde/Cargo.toml @@ -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" } diff --git a/komorebi-for-kde/komorebi/Cargo.toml b/komorebi-for-kde/komorebi/Cargo.toml index 551722e6..366c929b 100644 --- a/komorebi-for-kde/komorebi/Cargo.toml +++ b/komorebi-for-kde/komorebi/Cargo.toml @@ -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 } diff --git a/komorebi-for-kde/komorebi/src/core/mod.rs b/komorebi-for-kde/komorebi/src/core/mod.rs index 93e30dc5..3fd0620a 100644 --- a/komorebi-for-kde/komorebi/src/core/mod.rs +++ b/komorebi-for-kde/komorebi/src/core/mod.rs @@ -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, diff --git a/komorebi-for-kde/komorebi/src/core/pathext.rs b/komorebi-for-kde/komorebi/src/core/pathext.rs deleted file mode 100644 index 81f61ea7..00000000 --- a/komorebi-for-kde/komorebi/src/core/pathext.rs +++ /dev/null @@ -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> 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 { - 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 for PathBuf { - fn from(path: ResolvedPathBuf) -> Self { - path.0 - } -} - -impl serde_with::SerializeAs for ResolvedPathBuf { - fn serialize_as(path: &PathBuf, serializer: S) -> Result - where - S: serde::Serializer, - { - path.serialize(serializer) - } -} - -impl<'de> serde_with::DeserializeAs<'de, PathBuf> for ResolvedPathBuf { - fn deserialize_as(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let path = PathBuf::deserialize(deserializer)?; - Ok(path.replace_env()) - } -} - -#[cfg(feature = "schemars")] -impl serde_with::schemars_1::JsonSchemaAs 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>`] 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>`] -/// 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>, D::Error> -where - D: serde::Deserializer<'de>, -{ - let map = Option::>::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: P) -> PathBuf { - // Ensure that the path is using the correct path separator for the OS. - p.as_ref().components().collect::() - } - - fn resolve>(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")); - } - } -}