Skip to content

Commit

Permalink
Add util function for boolean env vars
Browse files Browse the repository at this point in the history
It's probably good to be consistent about what is recognized as "true"
without copying the same code.
  • Loading branch information
ids1024 authored and Drakulix committed Oct 15, 2024
1 parent a4d875e commit 087be20
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,7 @@ pub fn client_is_privileged(client: &Client) -> bool {
}

fn enable_wayland_security() -> bool {
std::env::var("COSMIC_ENABLE_WAYLAND_SECURITY")
.map(|x| {
x == "1"
|| x.to_lowercase() == "true"
|| x.to_lowercase() == "yes"
|| x.to_lowercase() == "y"
})
.unwrap_or(false)
crate::utils::env::bool_var("COSMIC_ENABLE_WAYLAND_SECURITY").unwrap_or(false)
}

impl State {
Expand Down Expand Up @@ -524,8 +517,8 @@ impl State {
let idle_inhibit_manager_state = IdleInhibitManagerState::new::<State>(&dh);
let idle_inhibiting_surfaces = HashSet::new();

let data_control_state = std::env::var("COSMIC_DATA_CONTROL_ENABLED")
.is_ok_and(|value| value == "1")
let data_control_state = crate::utils::env::bool_var("COSMIC_DATA_CONTROL_ENABLED")
.unwrap_or(false)
.then(|| {
DataControlState::new::<Self, _>(dh, Some(&primary_selection_state), |_| true)
});
Expand Down
6 changes: 6 additions & 0 deletions src/utils/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only

pub fn bool_var(name: &str) -> Option<bool> {
let value = std::env::var(name).ok()?.to_lowercase();
Some(["1", "true", "yes", "y"].contains(&value.as_str()))
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only

pub mod env;
mod ids;
pub(crate) use self::ids::id_gen;
pub mod geometry;
Expand Down

0 comments on commit 087be20

Please sign in to comment.