Skip to content

Commit 087be20

Browse files
ids1024Drakulix
authored andcommitted
Add util function for boolean env vars
It's probably good to be consistent about what is recognized as "true" without copying the same code.
1 parent a4d875e commit 087be20

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/state.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,7 @@ pub fn client_is_privileged(client: &Client) -> bool {
454454
}
455455

456456
fn enable_wayland_security() -> bool {
457-
std::env::var("COSMIC_ENABLE_WAYLAND_SECURITY")
458-
.map(|x| {
459-
x == "1"
460-
|| x.to_lowercase() == "true"
461-
|| x.to_lowercase() == "yes"
462-
|| x.to_lowercase() == "y"
463-
})
464-
.unwrap_or(false)
457+
crate::utils::env::bool_var("COSMIC_ENABLE_WAYLAND_SECURITY").unwrap_or(false)
465458
}
466459

467460
impl State {
@@ -524,8 +517,8 @@ impl State {
524517
let idle_inhibit_manager_state = IdleInhibitManagerState::new::<State>(&dh);
525518
let idle_inhibiting_surfaces = HashSet::new();
526519

527-
let data_control_state = std::env::var("COSMIC_DATA_CONTROL_ENABLED")
528-
.is_ok_and(|value| value == "1")
520+
let data_control_state = crate::utils::env::bool_var("COSMIC_DATA_CONTROL_ENABLED")
521+
.unwrap_or(false)
529522
.then(|| {
530523
DataControlState::new::<Self, _>(dh, Some(&primary_selection_state), |_| true)
531524
});

src/utils/env.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: GPL-3.0-only
2+
3+
pub fn bool_var(name: &str) -> Option<bool> {
4+
let value = std::env::var(name).ok()?.to_lowercase();
5+
Some(["1", "true", "yes", "y"].contains(&value.as_str()))
6+
}

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22

3+
pub mod env;
34
mod ids;
45
pub(crate) use self::ids::id_gen;
56
pub mod geometry;

0 commit comments

Comments
 (0)