1#[cfg(target_os = "linux")]
2use std::env;
3use std::io;
4#[cfg(target_os = "linux")]
5use std::{ffi::OsString, process::Command};
6use thiserror::Error;
7
8#[cfg(target_os = "linux")]
9use serde_json;
10#[cfg(target_os = "linux")]
11use xrandr;
12
13#[cfg(target_os = "linux")]
14#[derive(Debug, Error)]
15pub enum CommandError {
16 #[cfg(target_os = "linux")]
17 #[error("failed to execute program {0:?}: {1}")]
18 CommandIO(OsString, std::io::Error),
19
20 #[cfg(target_os = "linux")]
21 #[error("{command:?} exit with code {exit_code:?}:\n{}", String::from_utf8_lossy(.stderr))]
22 CommandStatus {
23 command: Command,
24 exit_code: Option<i32>,
25 stderr: Vec<u8>,
26 },
27}
28
29#[derive(Debug, Error)]
30#[non_exhaustive]
31pub enum WallpaperError {
32 #[cfg(target_os = "linux")]
33 #[error("unsupported Desktop Environment {0:?}")]
34 Unsuported(String),
35
36 #[cfg(target_os = "linux")]
37 #[error("can not read Environment Variable {0:?}: {1}")]
38 EnvVar(&'static str, env::VarError),
39
40 #[cfg(target_os = "linux")]
41 #[error("Dbus error: {0}")]
42 Dbus(#[from] rustbus::connection::Error),
43
44 #[cfg(target_os = "linux")]
45 #[error("failed to serialize json output: {0}")]
46 SerdeJson(#[from] serde_json::Error),
47
48 #[cfg(target_os = "linux")]
49 #[error("xrandr erro): {0}")]
50 Xrandr(#[from] xrandr::XrandrError),
51
52 #[cfg(target_os = "linux")]
53 #[error("{0}")]
54 Command(#[from] CommandError),
55
56 #[cfg(feature = "fallback")]
57 #[error("{0}")]
58 WallpaperCrate(#[from] fallback::Error),
59
60 #[error("{0:?} {1}")]
61 IOError(String, io::Error),
62
63 #[cfg(target_os = "linux")]
64 #[error("Unknow XFCE wallpaper mode {0:?}")]
65 UnknownMode(String),
66}
67
68pub(crate) trait Context<V> {
69 fn context<C>(self, context: C) -> Result<V, WallpaperError>
70 where
71 C: std::fmt::Display;
72}
73
74impl<V> Context<V> for Result<V, io::Error> {
76 fn context<C>(self, context: C) -> Result<V, WallpaperError>
77 where
78 C: std::fmt::Display,
79 {
80 self.map_err(|error| WallpaperError::IOError(context.to_string(), error))
81 }
82}
83
84#[cfg(target_os = "linux")]
85pub(crate) fn load_env_var(key: &'static str) -> Result<String, WallpaperError> {
86 std::env::var(key).map_err(|err| WallpaperError::EnvVar(key, err))
87}