battered 0.7.0

Make the most of your laptop's battery life with custom actions and informative desktop notifications.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use notify_rust::{Timeout, Urgency};
use serde::de::Error as SerdeError;
use serde::{Deserialize, Deserializer};
use serde_with::{serde_as, DurationSeconds};
use shell_words::split as shell_split;
use std::path::PathBuf;
use std::time::Duration;

#[serde_as]
#[derive(Debug, Deserialize)]
pub struct Config {
    #[serde_as(as = "DurationSeconds<u64>")]
    #[serde(default = "default_interval")]
    pub interval: Duration,
    pub action: Vec<Action>,
    pub on_ac: Option<OnAcAction>,
}

#[derive(Debug, Deserialize)]
pub struct Action {
    #[serde(deserialize_with = "deserialize_float_percentage")]
    pub percentage: f32,
    #[serde(default, deserialize_with = "deserialize_command")]
    pub command: Option<Vec<String>>,
    pub notify: Option<Notify>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct OnAcAction {
    #[serde(
        default = "default_ac_percentage",
        deserialize_with = "deserialize_float_percentage"
    )]
    pub percentage: f32,
    #[serde(default, deserialize_with = "deserialize_command")]
    pub command: Option<Vec<String>>,
    pub notify: Option<Notify>,
}

#[serde_as]
#[derive(Debug, Deserialize, Clone)]
pub struct Notify {
    pub summary: String,
    #[serde(default)]
    pub body: Option<String>,
    #[serde(default = "default_urgency", deserialize_with = "deserialize_urgency")]
    pub urgency: Urgency,
    #[serde(default = "default_icon")]
    pub icon: String,
    #[serde(default, deserialize_with = "deserialize_timeout")]
    pub timeout: Timeout,
}

fn default_urgency() -> Urgency {
    Urgency::Normal
}

fn default_icon() -> String {
    "battery-caution".to_string()
}

fn default_interval() -> Duration {
    Duration::from_secs(60)
}

fn default_ac_percentage() -> f32 {
    0.0
}

fn deserialize_float_percentage<'de, D>(deserializer: D) -> Result<f32, D::Error>
where
    D: serde::Deserializer<'de>,
{
    // Deserialize float
    let value: f32 = Deserialize::deserialize(deserializer)?;
    // Check valid range
    if !(0.0..=1.0).contains(&value) {
        return Err(D::Error::custom("value must be between 0 and 1"));
    }
    Ok(value)
}

fn deserialize_command<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
where
    D: Deserializer<'de>,
{
    // Deserialize the string
    let value: String = String::deserialize(deserializer)?;
    // Attempt to split the command
    match shell_split(&value) {
        Ok(command) => Ok(Some(command)),
        Err(e) => Err(D::Error::custom(format!("Failed to split command: {}", e))),
    }
}
fn deserialize_urgency<'de, D>(deserializer: D) -> Result<Urgency, D::Error>
where
    D: Deserializer<'de>,
{
    // Deserialize the string
    let value: String = String::deserialize(deserializer)?;
    // Attempt to parse the notification urgency
    match Urgency::try_from(value.as_str()) {
        Ok(urgency) => Ok(urgency),
        Err(e) => Err(D::Error::custom(format!(
            "Failed to parse notification urgency: {}",
            e
        ))),
    }
}

fn deserialize_timeout<'de, D>(deserializer: D) -> Result<Timeout, D::Error>
where
    D: Deserializer<'de>,
{
    // Deserialize the integer
    let value: i32 = i32::deserialize(deserializer)?;
    Ok(Timeout::from(value))
}

// Taken from i3status-rust
pub fn xdg_config_home() -> PathBuf {
    // In the unlikely event that $HOME is not set, it doesn't really matter
    // what we fall back on, so use /.config.
    let config_path = std::env::var("XDG_CONFIG_HOME").unwrap_or(format!(
        "{}/.config",
        std::env::var("HOME").unwrap_or_else(|_| "".to_string())
    ));
    PathBuf::from(&config_path)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use std::sync::Mutex;
    use toml;

    static ENV_VAR_MUTEX: Mutex<()> = Mutex::new(());

    #[test]
    fn test_valid_config() {
        let toml_str = r#"
        interval = 120

        [[action]]
        percentage = 0.84
        command = "./powersave.sh profile laptop-battery-powersave"
        [action.notify]
        summary = "Battery discharging"
        urgency = "Low"
        icon = "battery-discharging"
        timeout = 10000
        "#;

        let config: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(config.interval, Duration::from_secs(120));
        assert_eq!(config.action[0].percentage, 0.84);
        assert_eq!(
            config.action[0].command,
            Some(vec![
                "./powersave.sh".to_string(),
                "profile".to_string(),
                "laptop-battery-powersave".to_string()
            ])
        );
        let notify = config.action[0].notify.as_ref().unwrap();
        assert_eq!(notify.summary, "Battery discharging");
        assert_eq!(notify.urgency, Urgency::Low);
        assert_eq!(notify.icon, "battery-discharging");
        assert_eq!(notify.timeout, Timeout::Milliseconds(10000));
    }

    #[test]
    fn test_default_values() {
        let toml_str = r#"

        # At least one actions is required
        [[action]]
        percentage = 0.99
        "#;

        let config: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(config.interval, Duration::from_secs(60));
    }

    #[test]
    fn test_default_timeout_never_value() {
        let toml_str = r#"

        # At least one actions is required
        [[action]]
        percentage = 0.99
        [action.notify]
        summary = "Never Gonna Give You Up"
        timeout = 0
        "#; // `0` means no timeout

        let config: Config = toml::from_str(toml_str).unwrap();
        let notify = config.action[0].notify.as_ref().unwrap();
        assert_eq!(notify.timeout, Timeout::Never);
    }

    #[test]
    fn test_missing_actions() {
        let toml_str = r#"
        "#;

        let result: Result<Config, toml::de::Error> = toml::from_str(toml_str);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().message(), "missing field `action`");
    }

    #[test]
    fn test_invalid_interval_type() {
        let toml_str = r#"
        # Interval has to be valid Duration
        interval = "not_a_number"

        [[action]]
        percentage = 0.99
        "#;

        let result: Result<Config, toml::de::Error> = toml::from_str(toml_str);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_percentage_values() {
        let test_values = vec![
            r#"
            [[action]]
            percentage = -0.2
            "#,
            r#"
            [[action]]
            percentage = 42
            "#,
            r#"
            [[action]]
            percentage = "0.5"
            "#,
        ]; // Thresholds have to be positive floating point numbers between 0 and 1

        for (i, value) in test_values.iter().enumerate() {
            let result: Result<Config, toml::de::Error> = toml::from_str(value);
            assert!(result.is_err());
            if i == 0 || i == 1 {
                assert_eq!(
                    result.unwrap_err().message(),
                    "value must be between 0 and 1"
                );
            } else {
                assert!(result.unwrap_err().message().starts_with("invalid type"));
            };
        }
    }

    #[test]
    fn test_invalid_urgency_values() {
        let string_test_values = vec![
            r#"
            [[action]]
            percentage = 0.9
            [action.notify]
            urgency = ""
            "#,
            r#"
            [[action]]
            percentage = 0.9
            [action.notify]
            urgency = "Whatever"
            "#,
        ];
        let other_test_values = vec![
            r#"
            [[action]]
            percentage = 0.9
            [action.notify]
            urgency = 1
            "#,
            r#"
            [[action]]
            percentage = 0.9
            [action.notify]
            urgency = ["Critical", "Normal"]
            "#,
        ];

        for value in string_test_values {
            let result: Result<Config, toml::de::Error> = toml::from_str(value);
            assert!(result.is_err());
            assert!(result
                .unwrap_err()
                .message()
                .starts_with("Failed to parse notification urgency"));
        }
        for value in other_test_values {
            let result: Result<Config, toml::de::Error> = toml::from_str(value);
            assert!(result.is_err());
            assert!(result.unwrap_err().message().starts_with("invalid type"));
        }
    }

    #[test]
    fn test_invalid_timeout_values() {
        let string_test_values = vec![
            r#"
            [[action]]
            percentage = 0.9
            [action.notify]
            timeout = "500"
            "#,
            r#"
            [[action]]
            percentage = 0.9
            [action.notify]
            # Timeout has to be i32
            timeout = 600.0
            "#,
        ];

        for value in string_test_values {
            let result: Result<Config, toml::de::Error> = toml::from_str(value);
            assert!(result.is_err());
            assert!(result.unwrap_err().message().starts_with("invalid type"));
        }

        let toml_str = r#"
        # Interval has to be valid Duration
        [[action]]
        percentage = 0.99
        [action.notify]
        summary = ""
        timeout = -5
        "#; // Negative timeout is silently converted to `Timeout::Default` by notify-rust
        let config: Config = toml::from_str(toml_str).unwrap();
        let notify = config.action[0].notify.as_ref().unwrap();
        assert_eq!(notify.timeout, Timeout::Default);
    }

    #[test]
    fn test_unparsable_command_value() {
        let toml_str = r#"
        [[action]]
        percentage = 0.99
        command = "notify-send 'Oops, I am missing my closing single quote!"
        "#; // Unbalanced quotes cannot be parsed correctly

        let result: Result<Config, toml::de::Error> = toml::from_str(toml_str);
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().message(),
            "Failed to split command: missing closing quote"
        );
    }

    #[test]
    fn test_xdg_config_home() {
        let _lock = ENV_VAR_MUTEX.lock().unwrap();
        env::set_var("XDG_CONFIG_HOME", "/home/battered/.config");
        let config_home = xdg_config_home();
        assert_eq!(config_home, PathBuf::from("/home/battered/.config"));
    }

    #[test]
    fn test_xdg_config_home_from_home_var() {
        let _lock = ENV_VAR_MUTEX.lock().unwrap();
        env::remove_var("XDG_CONFIG_HOME");
        env::set_var("HOME", "/home/battered");
        let config_home = xdg_config_home();
        assert_eq!(config_home, PathBuf::from("/home/battered/.config"));
    }

    #[test]
    fn test_xdg_config_home_from_nothing() {
        let _lock = ENV_VAR_MUTEX.lock().unwrap();
        env::remove_var("XDG_CONFIG_HOME");
        env::remove_var("HOME");
        let config_home = xdg_config_home();
        assert_eq!(config_home, PathBuf::from("/.config"));
    }

    #[test]
    fn test_valid_onac_action() {
        let toml_str = r#"
        interval = 120

        [[action]]
        percentage = 0.84
        [action.notify]
        summary = "Battery discharging"

        [on_ac]
        percentage = 0.00
        command = "./powersave.sh profile ac"
        [on_ac.notify]
        summary = "Battery charging"
        urgency = "Low"
        icon = "battery-good-charging"
        "#;

        let config: Config = toml::from_str(toml_str).unwrap();
        let on_ac = config.on_ac.clone();
        assert_eq!(config.interval, Duration::from_secs(120));
        assert_eq!(config.action[0].percentage, 0.84);
        assert_eq!(
            on_ac.as_ref().unwrap().command,
            Some(vec![
                "./powersave.sh".to_string(),
                "profile".to_string(),
                "ac".to_string()
            ])
        );
        let notify = on_ac.as_ref().unwrap().notify.as_ref().unwrap();
        assert_eq!(notify.summary, "Battery charging");
        assert_eq!(notify.urgency, Urgency::Low);
        assert_eq!(notify.icon, "battery-good-charging");
    }
}